Page 2 of 3
Re: Rain prediction - weather underground
Posted: Monday 16 April 2018 20:03
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
Re: Rain prediction - weather underground
Posted: Saturday 05 May 2018 13:18
by elmortero
Should work.
Please post the error you get, and your script.
Re: Rain prediction - weather underground
Posted: Friday 25 May 2018 8:41
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
}
Re: Rain prediction - weather underground
Posted: Tuesday 29 May 2018 20:46
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
}
Re: Rain prediction - weather underground
Posted: Wednesday 30 May 2018 13:45
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...
Re: Rain prediction - weather underground
Posted: Wednesday 30 May 2018 14:51
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
Re: Rain prediction - weather underground
Posted: Wednesday 30 May 2018 17:52
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
}
Re: Rain prediction - weather underground
Posted: Wednesday 30 May 2018 19:44
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
}
Re: Rain prediction - weather underground
Posted: Thursday 31 May 2018 14:26
by arek156
Everything works, thanks to the elmortero.
Wysłane z Huawei P9
Re: Rain prediction - weather underground
Posted: Thursday 26 July 2018 17:39
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 (51.64 KiB) Viewed 2397 times
Re: Rain prediction - weather underground
Posted: Thursday 26 July 2018 18:14
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.
Re: Rain prediction - weather underground
Posted: Thursday 26 July 2018 21:39
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.
Re: Rain prediction - weather underground
Posted: Sunday 05 August 2018 9:09
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?
Re: Rain prediction - weather underground
Posted: Sunday 05 August 2018 14:19
by elmortero
Re: Rain prediction - weather underground
Posted: Monday 06 August 2018 16:34
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.
Re: Rain prediction - weather underground
Posted: Saturday 29 December 2018 15:55
by EdwinK
Now that the access to the Weather Underground API is almost over, does anyone know of a replacement for this?
Re: Rain prediction - weather underground
Posted: Tuesday 22 January 2019 9:42
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)
Re: Rain prediction - weather underground
Posted: Thursday 25 April 2019 13:41
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.
Re: Rain prediction - weather underground
Posted: Thursday 25 April 2019 20:16
by EdwinK
Maybe if you are willing to share that script

Re: Rain prediction - weather underground
Posted: Thursday 25 April 2019 21:35
by papoo