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: 233
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: 233
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: 177
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: 233
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: 233
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


User avatar
EdwinK
Posts: 1285
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Rain prediction - weather underground

Post by EdwinK » Thursday 26 July 2018 17:39

The attachment Screen Shot 2018-07-26 at 17.37.14.png is no longer available
Getting this error in the events-box
Screen Shot 2018-07-26 at 17.37.14.png
Screen Shot 2018-07-26 at 17.37.14.png (51.64 KiB) Viewed 463 times
Running latest BETA on a Pi-3 | Toon Thermostat | RFxcom | Dashticz V2 |

elmortero
Posts: 233
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 » Thursday 26 July 2018 18:14

EdwinK, maybe also post the log where it tells more about the error.
Or tell what the balloon says when you move the mouse over the red cross.

User avatar
EdwinK
Posts: 1285
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Rain prediction - weather underground

Post by EdwinK » Thursday 26 July 2018 21:39

O.. I thought I had done that.

The error is: expected ']' near 'local'

(it's in the severe weather part of the script).

Because of the error I removed that part for now.
Running latest BETA on a Pi-3 | Toon Thermostat | RFxcom | Dashticz V2 |

User avatar
rbisschops
Posts: 80
Joined: Monday 12 May 2014 11:20
Target OS: Raspberry Pi
Domoticz version: 2.2563
Location: Schiedam, Netherlands
Contact:

Re: Rain prediction - weather underground

Post by rbisschops » Sunday 05 August 2018 9:09

Hi all, you can not get a new api key anymore for WU. Also the API description seems not visible anymore.Anyone has a link to the API description that works? Or maybee an ofline copy?
RPi 3B (master): RFXtrx433, Harmony Hub, Z-Wave devices, CoCo units, Hue, Xiaomi Aquara
RPi 2B (slave): P1 connection, MySensors with custom built watermeter sensors on Aquadis+
RPi’s with: Mosquitto MQTT, Home Bridge, Home Assistant (for evaluation only)

elmortero
Posts: 233
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 » Sunday 05 August 2018 14:19


User avatar
rbisschops
Posts: 80
Joined: Monday 12 May 2014 11:20
Target OS: Raspberry Pi
Domoticz version: 2.2563
Location: Schiedam, Netherlands
Contact:

Re: Rain prediction - weather underground

Post by rbisschops » Monday 06 August 2018 16:34

@elmortero: thanks, that helps. Script is working like a charm, it can use minor updates for version 2.4.X of dzVents :-). Will test and submit my latest version here as well.
RPi 3B (master): RFXtrx433, Harmony Hub, Z-Wave devices, CoCo units, Hue, Xiaomi Aquara
RPi 2B (slave): P1 connection, MySensors with custom built watermeter sensors on Aquadis+
RPi’s with: Mosquitto MQTT, Home Bridge, Home Assistant (for evaluation only)

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests