Rain prediction - weather underground

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

kingofsnake74
Posts: 1
Joined: Monday 16 April 2018 19:57
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Rain prediction - weather underground

Post by kingofsnake74 »

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: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Rain prediction - weather underground

Post by elmortero »

Should work.
Please post the error you get, and your script.
arek156
Posts: 7
Joined: Wednesday 10 January 2018 19:24
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Poland
Contact:

Re: Rain prediction - weather underground

Post by arek156 »

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: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Rain prediction - weather underground

Post by elmortero »

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: 183
Joined: Saturday 29 October 2016 20:55
Target OS: -
Domoticz version:
Contact:

Re: Rain prediction - weather underground

Post by devros »

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: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Rain prediction - weather underground

Post by elmortero »

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 19:24
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Poland
Contact:

Re: Rain prediction - weather underground

Post by arek156 »

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: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Rain prediction - weather underground

Post by elmortero »

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 19:24
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Poland
Contact:

Re: Rain prediction - weather underground

Post by arek156 »

Everything works, thanks to the elmortero.

Wysłane z Huawei P9

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

Re: Rain prediction - weather underground

Post by EdwinK »

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 2330 times
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Rain prediction - weather underground

Post by elmortero »

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: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Rain prediction - weather underground

Post by EdwinK »

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 (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
rbisschops
Posts: 63
Joined: Monday 12 May 2014 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2563
Location: Schiedam, Netherlands
Contact:

Re: Rain prediction - weather underground

Post by rbisschops »

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: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Rain prediction - weather underground

Post by elmortero »

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

Re: Rain prediction - weather underground

Post by rbisschops »

@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)
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Rain prediction - weather underground

Post by EdwinK »

Now that the access to the Weather Underground API is almost over, does anyone know of a replacement for this?
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Rain prediction - weather underground

Post by EdwinK »

Too bad... I really need that rain-percentage for the sunscreen script (now it's time to check for those. It's starting to snow)
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
Steef
Posts: 14
Joined: Monday 07 January 2019 6:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Rain prediction - weather underground

Post by Steef »

I am using a script that is using the data from Darksky, maybe that is an option for you? You can make up to 1000 requests per day for free.
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Rain prediction - weather underground

Post by EdwinK »

Maybe if you are willing to share that script :)
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: Rain prediction - weather underground

Post by papoo »

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest