Beginning this year I was looking for something to get precipitation prediction. Since the "buienradar" script does not work for me because there is not data for locations with a latitude below 44 I started on something myself.
Below I will post my script that is based on Weather Underground which provides this information world wide.
Maybe I am a "dirty coder" (lack of training and experience) and you see something that could be cleaner/easier/better so don't hesitate to point this out.
The script version I am posting here has been stripped of the notification that I send. It gets the "pop" --percentage of precipitation-- field per hour and per day and updates 2 virtual switches if the percentage is lower/higher than 30%.
Also there is a virtual Percentage sensor that gets updated with the POP.
I salute you.
olivier
Code: Select all
-- 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 20 minuteS'}
},
execute = function(domoticz)
-- Variables to customize ------------------------------------------------
local city = "XXXXXX" -- Your city for Wunderground API
local countryCode = "XXXXX" -- Your country code for Wunderground API
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
--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
}
If someone is interested in that part, let me know!!