I made a script to grab a few more information from darksky than the darksky hardware plugin of domoticz:
- Cloud cover
- Visibility
- UV Index
- Atmospheric pressure
- Tomorrow forecast, min and max temperature (useful to warn one day in advance of adverse weather)
It's pretty straitforward and can be customized easily with more or less information. Forecast widget is of type "text"
Script LUA Time
Code: Select all
--[[ Script to get some values from darksky API
]]--
local dev_Patm ="Pression atmosphérique" -- Atmospheric pressure
local dev_UV ="Index UV" -- UV Index
local dev_Vis ="Visibilité" -- Visibility
local dev_Cloud="Couverture nuageuse" -- Cloud layer
local dev_Tmin ="Température min J+1" -- Tomorrow temperature min forecast
local dev_Tmax ="Température max J+1" -- Tomorrow temperature max forecast
local dev_Prev ="Previsions J+1" -- Forecast d+1
local api_key="your_api_key" -- darksky secret key
local coord="45.0,4.0" -- latitude,longitude
local debug=0
function UpdateDev(device,nvalue,svalues)
--Met à jour un device numérique Domoticz
commandArray[#commandArray+1] = {['UpdateDevice'] = otherdevices_idx[device]..'|'..tostring(nvalue)..'|'..tostring(svalues)}
end
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
commandArray = {}
time = os.date("*t")
if ((time.min+2)%5)==0 then -- Run every 5 minutes
-- Querying Darksky
json = (loadfile "/opt/domoticz/scripts/lua/JSON.lua")() -- For Linux
result=os.capture('curl -s "https://api.darksky.net/forecast/'..api_key..'/'..coord..'?lang=fr&units=ca&exclude=hourly,flags"')
--print(result)
-- Reading values from json
local jsonValeur = json:decode(result)
val_Patm =jsonValeur.currently.pressure
val_UV =jsonValeur.currently.uvIndex
val_Vis =jsonValeur.currently.visibility
val_Cloud=jsonValeur.currently.cloudCover*100
val_Tmin =round(jsonValeur.daily.data[1].temperatureLow,1)
val_Tmax =round(jsonValeur.daily.data[1].temperatureHigh,1)
icon =jsonValeur.daily.data[1].icon
summary =jsonValeur.daily.data[1].summary
-- Converting darksky icon into domoticz forecast code
if icon=="clear-day" or icon=="clear-night" then prev=1 --Sunny
elseif icon=="rain" or icon=="snow" or rain=="sleet" then prev=6 --Cloudy/Rain
elseif icon=="wind" or icon=="fog" then prev=0 --Stable
elseif icon=="cloudy" then prev=2 --Cloudy
elseif icon=="partly-cloudy-day" or icon=="partly-cloudy-night" then prev=3 --Unstable
else prev=5 --Unknown
end
forecast=summary.."<br>Min : "..val_Tmin.."°C Max : "..val_Tmax.."°C"
if debug==1 then
print("Pression : "..val_Patm)
print("Index UV : "..val_UV)
print("Visibilité : "..val_Vis)
print("Couverture nuageuse : "..val_Cloud)
print("Température min J+1 : "..val_Tmin)
print("Température max J+1 : "..val_Tmax)
print("Icon : "..icon.." "..prev)
print("Summary : "..summary)
print("Prévisions : "..forecast)
end
-- Updating domoticz devices
UpdateDev(dev_Patm,0,val_Patm..";"..prev)
UpdateDev(dev_UV,0,val_UV..";0")
UpdateDev(dev_Vis,0,val_Vis)
UpdateDev(dev_Cloud,0,val_Cloud)
UpdateDev(dev_Tmin,0,val_Tmin)
UpdateDev(dev_Tmax,0,val_Tmax)
if otherdevices_svalues[dev_Prev] ~= forecast then UpdateDev(dev_Prev,0,forecast) end
end
return commandArray
Comments welcome !