Note that you need to get the location of JSON.lua right for this one (I'm running on a Pi) as well as create or rename the referenced variables and obviously change the url to include your creds and location. Also note that one is wunderground and the other openweathermap because of availability of the respective data.
Next 24 hours
Code: Select all
return {
active = true,
on = {
timer = {'at 00:05'}
},
execute = function(domoticz)
--json = assert(loadfile "./json.lua")()
--json = assert(loadfile "/usr/local/domoticz/var/scripts/lua/JSON.lua")() --for synology
json = assert(loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
--json = assert(loadfile "JSON.lua")() -- one-time load of the routines
local config=assert(io.popen('curl "http://api.openweathermap.org/data/2.5/forecast?q=YOURLOCATION,YOURCOUNTRYCODE&mode=json&APPID=XXXXXXXXXX&units=metric"'))
local weather = config:read('*all')
config:close()
local jsonWeather = json:decode(weather)
local totalrain = 0
local k = 1
while k <= 8 do
if type(jsonWeather.list[k].rain) ~= "nil" then
if type(jsonWeather.list[k].rain["3h"]) ~= "nil" then
totalrain = totalrain + jsonWeather.list[k].rain["3h"]
end
end
k = k + 1
end
domoticz.devices('Predicted Rainfall').update(0,totalrain)
end
}
Code: Select all
return {
active = true,
on = {
timer = {'at 00:05'}
},
execute = function(domoticz)
--json = (loadfile "./json.lua")()
--json = assert(loadfile "/usr/local/domoticz/var/scripts/lua/JSON.lua")() --for synology
json = assert(loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
--json = assert(loadfile "JSON.lua")() -- one-time load of the routines
local now = os.time()
local yesterday = now-86400
local urlYear = os.date("%Y",yesterday)
local urlMonth = os.date("%m",yesterday)
local urlDay = os.date("%d",yesterday)
undUrl = "http://api.wunderground.com/api/XXXXXXXXXX/history_" .. urlYear .. urlMonth .. urlDay .. "/q/pws:YOURLOCATION.json"
local config=assert(io.popen('curl ' .. undUrl))
local weather = config:read('*all')
config:close()
local jsonWeather = json:decode(weather)
local totalyesterdayrain = 0
if type(jsonWeather.history.dailysummary[1]) ~= "nil" then
if type(jsonWeather.history.dailysummary[1].precipm) ~= "nil" then
totalyesterdayrain = jsonWeather.history.dailysummary[1].precipm
end
end
domoticz.devices('Yesterday Rainfall').update(0,totalyesterdayrain)
end
}