Rain prediction - weather underground

Easy to use, 100% Lua-based event scripting framework.
kingofsnake74
Posts: 1
Joined: Monday 16 April 2018 19:57
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Rain prediction - weather underground

Post by kingofsnake74 » Monday 16 April 2018 20:03

Hello all,

Tried to use this script but is not functioning. Before debugging i would like to know if this script can be used in Domoticz stable 3.8153. If so can someone post the complete script. Thanx all. Greetings Jerry

elmortero
Posts: 201
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Rain prediction - weather underground

Post by elmortero » Saturday 05 May 2018 13:18

Should work.
Please post the error you get, and your script.

arek156
Posts: 7
Joined: Wednesday 10 January 2018 20:24
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: Poland
Contact:

Re: Rain prediction - weather underground

Post by arek156 » Friday 25 May 2018 8:41

Hello
Very interesting project, but don't work with my domoticz. I put in a log and script, what could be a problem?

2018-05-25 08:40:01.448 Error: dzVents: Error: An error occured when calling event handler weather_rain
2018-05-25 08:40:01.448 Error: dzVents: Error: ...oticz/scripts/dzVents/generated_scripts/weather_rain.lua:23: attempt to index field 'hourly_forecast' (a nil value)

Spoiler: show
return {
active = true,
on = {
timer = {'every 2 minuteS'}
},

execute = function(domoticz)
-- Variables to customize ------------------------------------------------
local city = "Poznan" -- Your city for Wunderground API
local countryCode = "pl" -- Your country code for Wunderground API
local wuAPIkey = "xxxxxxxxxxxxx" -- Your Weather Underground API Key
local rainswitch1 = domoticz.devices('Rain-1-hour') -- The switch for rain in the next hour
local rainswitch24 = domoticz.devices('Rain-today') -- the switch for rain Today
local popDay = domoticz.devices('popday') -- The percentage virtual sensor
------------------------------------------------------------------------------------ pop24
--begin of the part to get hourly rain chances
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
local linker1 = tostring('curl http://api.wunderground.com/api/' .. wuAPIkey .. '/hourly/q/' .. countryCode .. '/' .. city .. '.json')
local config=assert(io.popen(linker1))
local loc = config:read('*all')
config:close()
local jsonLocation = json:decode(loc)
local pop1 = tonumber(jsonLocation.hourly_forecast[1].pop)
popDay.updatePercentage(pop1)

if pop1 >= 30 and rainswitch1.state ~= 'On' --switch will be turned on if probability is higher than 30%
then rainswitch1.switchOn()
end

if pop1 < 30 and rainswitch1.state ~= 'Off' --if the probability is lower than 30% switch will be turned off
then
rainswitch1.switchOff()
end
--end of the part to get hourly rain chances
--begin of the part to get daily rain chances
local linker2 = tostring('curl http://api.wunderground.com/api/' .. wuAPIkey .. '/forecast/q/' .. countryCode .. '/' .. city .. '.json')
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
local config=assert(io.popen(linker2))
local loc = config:read('*all')
config:close()
local jsonLocation = json:decode(loc)
local pop24 = tonumber(jsonLocation.forecast.txt_forecast.forecastday[1].pop)
if pop24 >= 30 and rainswitch24.state ~= 'On'
then rainswitch24.switchOn()
end
if pop24 < 30 and rainswitch24.state ~= 'Off'
then rainswitch24.switchOff()
end
end
}

elmortero
Posts: 201
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Rain prediction - weather underground

Post by elmortero » Tuesday 29 May 2018 20:46

Hi arek156

If you use the countrycode + citycode there are 2 results. WUnderground shows the 2 posible locations and no weather results.
Because of that the data you try to get is non-existing(nil)

With these changes it should be ok:
Replace ' .. countryCode .. '/' .. city .. '.json'
with
zmw:00000.168.12490.json' so that the it will result in :

'curl http://api.wunderground.com/api/' .. wuAPIkey .. '/forecast/q/zmw:00000.168.12490.json'

On the other hand, dzvents has given us better ways since the original post (thank you Dannybloe!!)

I include an adapted version of your script in order to use asynchonous http requests --> so domoticz does not get blocked/lagged if there is an issue with getting the data.
For this your dzvents version must be higher than 2.3

Code: Select all

return {
	on = {
		timer = { 'every 15 minutes'},
		httpResponses = { 'wUnderG' } -- matches callback string below
	},

execute = function(domoticz, triggerItem)
-- Variables to customize ------------------------------------------------
local wuAPIkey = "xxxxxx" -- Your Weather Underground API Key
local rainswitch1 = domoticz.devices('Rain-1-hour')	-- The switch for rain in the next hour
local rainswitch24 = domoticz.devices('Rain-today')	-- the switch for rain Today
local popDay = domoticz.devices('popday')	-- The percentage virtual sensor
------------------------------------------------------------------------------------ pop24

local wUrl = 'http://api.wunderground.com/api/' .. wuAPIkey .. '/forecast/hourly/q/zmw:00000.168.12490.json'

		if (triggerItem.isTimer) then
			domoticz.openURL({
				url = wUrl,
				method = 'GET',
				callback = 'wUnderG'
			})
			
		elseif (triggerItem.isHTTPResponse) then
		local response = triggerItem
			if (response.ok and response.isJSON) then
					--hear goes the check for the next hour
					local pop1 = tonumber(response.json.hourly_forecast[1].pop)
					popDay.updatePercentage(pop1)
						if pop1 >= 30 then --switch will be turned on if probability is higher than 30%
						 rainswitch1.switchOn().checkFirst()
						else
						rainswitch1.switchOff().checkFirst()
						end
					--hear goes the check for today
					local pop24 = tonumber(response.json.forecast.txt_forecast.forecastday[1].pop)
						if pop24 >= 30 then
						 rainswitch24.switchOn().checkFirst()
						else
						 rainswitch24.switchOff().checkFirst()
						end	
					end
				else
					print('**wUnderG failed to fetch info')
			end				
		end	
}

devros
Posts: 175
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Rain prediction - weather underground

Post by devros » Wednesday 30 May 2018 13:45

elmortero wrote:
Tuesday 29 May 2018 20:46
Hi arek156

If you use the countrycode + citycode there are 2 results. WUnderground shows the 2 posible locations and no weather results.
Because of that the data you try to get is non-existing(nil)

With these changes it should be ok:
Replace ' .. countryCode .. '/' .. city .. '.json'
with
zmw:00000.168.12490.json' so that the it will result in :

'curl http://api.wunderground.com/api/' .. wuAPIkey .. '/forecast/q/zmw:00000.168.12490.json'

On the other hand, dzvents has given us better ways since the original post (thank you Dannybloe!!)

I include an adapted version of your script in order to use asynchonous http requests --> so domoticz does not get blocked/lagged if there is an issue with getting the data.
For this your dzvents version must be higher than 2.3
thanks, but how to find b]zmw:00000.168.12490.json'[/b] for my location...

elmortero
Posts: 201
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Rain prediction - weather underground

Post by elmortero » Wednesday 30 May 2018 14:51

If you enter the URL you had in a browser you will get a JSON response.
In that there will be all the stations for PL/Poznan each with their zmw
The one I used in above script is the first one of the results

arek156
Posts: 7
Joined: Wednesday 10 January 2018 20:24
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: Poland
Contact:

Re: Rain prediction - weather underground

Post by arek156 » Wednesday 30 May 2018 17:52

Hello
I'm using dzVents 2.2.0 and unfortunately adding a line of code does not help, I'm throwing in again the error that appeared in the log and the entire script. I will be grateful for your help :)

2018-05-30 17:47:03.139 Error: dzVents: Error: An error occured when calling event handler weather_rain
2018-05-30 17:47:03.139 Error: dzVents: Error: ...oticz/scripts/dzVents/generated_scripts/weather_rain.lua:25: attempt to index field 'hourly_forecast' (a nil value)
Spoiler: show
-- required:
-- 2 virtual switches: one for next hour prediction and one for Today prediction
-- 1 virtual sensor Percentage: for Probability Of precipitation
-- a weatherunderground API key. Instructions to get one are on the Domoticz Wiki
return {
active = true,
on = {
timer = {'every 1 minutes'}
},

execute = function(domoticz)
-- Variables to customize ------------------------------------------------
local wuAPIkey = "000000" -- Your Weather Underground API Key
local rainswitch1 = domoticz.devices('Rain-1-hour') -- The switch for rain in the next hour
local rainswitch24 = domoticz.devices('Rain-today') -- the switch for rain Today
local popDay = domoticz.devices('popday') -- The percentage virtual sensor
------------------------------------------------------------------------------------ pop24
--begin of the part to get hourly rain chances
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
local linker1 = tostring('curl http://api.wunderground.com/api/' .. wuAPIkey .. '/forecast/q/zmw:00000.168.12490.json')
local config=assert(io.popen(linker1))
local loc = config:read('*all')
config:close()
local jsonLocation = json:decode(loc)
local pop1 = tonumber(jsonLocation.hourly_forecast[1].pop)
popDay.updatePercentage(pop1)

if pop1 >= 30 and rainswitch1.state ~= 'On' --switch will be turned on if probability is higher than 30%
then rainswitch1.switchOn()
end

if pop1 < 30 and rainswitch1.state ~= 'Off' --if the probability is lower than 30% switch will be turned off
then
rainswitch1.switchOff()
end
--end of the part to get hourly rain chances
--begin of the part to get daily rain chances
local linker2 = tostring('curl http://api.wunderground.com/api/' .. wuAPIkey .. '/forecast/q/' .. countryCode .. '/' .. city .. '.json')
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
local config=assert(io.popen(linker2))
local loc = config:read('*all')
config:close()
local jsonLocation = json:decode(loc)
local pop24 = tonumber(jsonLocation.forecast.txt_forecast.forecastday[1].pop)
if pop24 >= 30 and rainswitch24.state ~= 'On'
then rainswitch24.switchOn()
end
if pop24 < 30 and rainswitch24.state ~= 'Off'
then rainswitch24.switchOff()
end
end
}

elmortero
Posts: 201
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Rain prediction - weather underground

Post by elmortero » Wednesday 30 May 2018 19:44

It is not working because in your URL you only get forecast but try to read hourly_forecast.
This works (tested it here). Also I joined the 2 URLs into one. (forecast and hourly)

Code: Select all

return {
active = true, 
on = {
timer = {'every 15 minutes'}
},

execute = function(domoticz)
-- Variables to customize ------------------------------------------------
local wuAPIkey = "xxxxxxxxx" -- Your Weather Underground API Key
local rainswitch1 = domoticz.devices('Rain-1-hour')	-- The switch for rain in the next hour
local rainswitch24 = domoticz.devices('Rain-today')	-- the switch for rain Today
local popDay = domoticz.devices('popday')	-- The percentage virtual sensor
------------------------------------------------------------------------------------ pop24
--begin of the part to get hourly rain chances
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
local linker1 = tostring('curl http://api.wunderground.com/api/' .. wuAPIkey .. '/forecast/hourly/q/zmw:00000.168.12490.json')
	local config=assert(io.popen(linker1))
			local loc = config:read('*all')
			config:close()
			local jsonLocation = json:decode(loc)
local pop1 = tonumber(jsonLocation.hourly_forecast[1].pop)
popDay.updatePercentage(pop1)

	if pop1 >= 30 and rainswitch1.state ~= 'On' --switch will be turned on if probability is higher than 30%
	then rainswitch1.switchOn()
	end

	if pop1 < 30 and rainswitch1.state ~= 'Off'	--if the probability is lower than 30% switch will be turned off
	then rainswitch1.switchOff()
	end

   local pop24 = tonumber(jsonLocation.forecast.txt_forecast.forecastday[1].pop)
	if pop24 >= 30 and rainswitch24.state ~= 'On' 
	then rainswitch24.switchOn()
	end
	if pop24 < 30 and rainswitch24.state ~= 'Off'
	then rainswitch24.switchOff()
	end	
end
}

arek156
Posts: 7
Joined: Wednesday 10 January 2018 20:24
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: Poland
Contact:

Re: Rain prediction - weather underground

Post by arek156 » Thursday 31 May 2018 14:26

Everything works, thanks to the elmortero.

Wysłane z Huawei P9


Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests