darksky.lua :
Code : Tout sélectionner
local scriptName = 'Darksky forecast'
local scriptVersion = ' 1.21'
local proba_pluie_h = {}
local prev_wind_h = {}
local MoonPhaseSelect = {}
proba_pluie_h[1] = 'Proba Pluie 1h'
proba_pluie_h[2] = 'Proba Pluie 2h'
proba_pluie_h[3] = nil
proba_pluie_h[4] = 'Proba Pluie 4h'
proba_pluie_h[5] = nil
proba_pluie_h[6] = 'Proba Pluie 6h'
proba_pluie_h[12] = 'Proba Pluie 12h'
proba_pluie_h[24] = 'Proba Pluie 24h'
proba_pluie_h[36] = nil
proba_pluie_h[48] = nil
prev_wind_h[1] = 'Meteo 1h'
prev_wind_h[2] = 'Meteo 2h'
prev_wind_h[3] = nil
prev_wind_h[4] = 'Meteo 4h'
prev_wind_h[5] = nil
prev_wind_h[6] = 'Meteo 6h'
prev_wind_h[12] = 'Meteo 12h'
prev_wind_h[24] = 'Meteo 24h'
prev_wind_h[36] = nil
prev_wind_h[48] = nil
MoonPhaseSelect[1] = 'Phase Lune' --moonPhase day 1 selector switch
MoonPhaseSelect[2] = nil --moonPhase day 2 selector switch
MoonPhaseSelect[3] = nil --moonPhase day 3 selector switch
MoonPhaseSelect[4] = nil --moonPhase day 4 selector switch
MoonPhaseSelect[5] = nil --moonPhase day 5 selector switch
MoonPhaseSelect[6] = nil --moonPhase day 6 selector switch
MoonPhaseSelect[7] = nil --moonPhase day 7 selector switch
MoonPhaseSelect[8] = nil --moonPhase day 8 selector switch
return {
active = true,
on = { timer = { 'every 15 minutes' }, -- remember only 1000 requests by day, 30mn = 48 requests, 5mn = 288 requests
httpResponses = { 'DarkSky_Trigger' } -- Trigger the handle Json part
},
logging = { level = domoticz.LOG_DEBUG, -- Seulement un niveau peut être actif; commenter les autres
-- level = domoticz.LOG_INFO, -- Only one level can be active; comment others
-- level = domoticz.LOG_ERROR,
-- level = domoticz.LOG_MODULE_EXEC_INFO,
marker = scriptName..' v'..scriptVersion
},
data = { Forecast = {initial = {} }, -- Keep a copy of last json just in case
},
execute = function(domoticz, item)
local function logWrite(str,level) -- Support function for shorthand debug log statements
domoticz.log(tostring(str),level or domoticz.LOG_DEBUG)
end
local function quadrants(degrees)
local quadrants = {"NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"}
if degrees > 348.75 or degrees <= 11.25 then
return "N"
else
local index = tonumber(math.floor((degrees - 11.25) / 22.5)+1)
return quadrants[index]
end
end
local function levelMoonhase(moonphase)
local level = 10 -- level correspondant à Nouvelle lune
if(moonphase > 0.05 and moonphase < 0.245) then
level = 20 -- level correspondant à Premier croissant
elseif(moonphase > 0.244 and moonphase < 0.255) then
level = 30 -- level correspondant à Premier quartier
elseif(moonphase > 0.255 and moonphase < 0.495) then
level = 40 -- level correspondant à Gibbeuse croissante
elseif(moonphase > 0.495 and moonphase < 0.55) then
level = 50 -- level correspondant à Pleine lune
elseif(moonphase > 0.55 and moonphase < 0.745) then
level = 60 -- level correspondant à Gibbeuse décroissante
elseif(moonphase > 0.745 and moonphase < 0.755) then
level = 70 -- level correspondant à Dernier quartier
elseif(moonphase > 0.755 and moonphase < 1.00) then
level = 80 -- level correspondant à Dernier croissant
end
return level--moonPhase,
end
--local DarkSkyAPIkey = domoticz.variables('api_forecast_io').value
local DarkSkyAPIkey = "79befc923d072ad0f0520bdb13632551"
--local geolocalisation = domoticz.settings.location.latitude..','..domoticz.settings.location.longitude
local geolocalisation = "47.617433,-0.891835" -- latitude,longitude
logWrite('geolocalisation : '..geolocalisation,domoticz.LOG_INFO)
local Forecast_url = "https://api.darksky.net/forecast/" -- url
local extraData = "?units=si&exclude=flags"--currently,minutely,daily,alerts,
if (item.isTimer) then
domoticz.openURL({
url = Forecast_url..DarkSkyAPIkey.."/"..geolocalisation..extraData,
callback = 'DarkSky_Trigger'
})
end
if (item.isHTTPResponse and item.ok) then
-- we know it is json but dzVents cannot detect this
-- convert to Lua
local json = domoticz.utils.fromJSON(item.data)
-- json is now a Lua table
if #item.data > 0 then
domoticz.data.Forecast = domoticz.utils.fromJSON(item.data)
rt = domoticz.utils.fromJSON(item.data)
else
domoticz.log("Problem with response from DarkSky (no data) using data from earlier run",domoticz.LOG_ERROR)
rt = domoticz.data.Forecast -- json empty. Get last valid from domoticz.data
if #rt < 1 then -- No valid data in domoticz.data either
domoticz.log("No previous data. are DarkSkyAPIkey and geolocalisation ok?",domoticz.LOG_ERROR)
return false
end
end
logWrite('heure systeme')
local now = domoticz.time.dDate
logWrite('now : '..now)
j = 1
for i = 1,48 do
logWrite('j : '..j)
if now > tonumber(json.hourly.data[j].time) then -- si maintenant est supérieur au timestamp lu
local h = domoticz.utils.round((now - tonumber(json.hourly.data[j].time)) / 3600,0)
logWrite("h : "..h)
j = j+h
logWrite("j : "..j)
end
logWrite('date :'.. os.date('%Y-%m-%d %H:%M:%S', json.hourly.data[j].time),domoticz.LOG_INFO)
logWrite('precipProbability :' ..json.hourly.data[j].precipProbability,domoticz.LOG_INFO)
if (proba_pluie_h[i]) then
domoticz.devices(proba_pluie_h[i]).updatePercentage(json.hourly.data[j].precipProbability*100)
end
local windSpeed = json.hourly.data[j].windSpeed
local windGust = json.hourly.data[j].windGust
local windBearing = json.hourly.data[j].windBearing
local temperature = json.hourly.data[j].temperature
local windChill = json.hourly.data[j].apparentTemperature
local direction = quadrants(windBearing)
logWrite('windSpeed : '..tostring(windSpeed)..' windGust : '..tostring(windGust)..' windBearing : '..tostring(windBearing)..' direction :'..tostring(direction)..' temperature : '..tostring(temperature)..' windChill : '..tostring(windChill),domoticz.LOG_INFO)
if (prev_wind_h[i]) then
domoticz.devices(prev_wind_h[i]).updateWind(windBearing,tostring(direction),windSpeed,windGust,temperature,windChill)
logWrite('mise à jour du device '..prev_wind_h[i],domoticz.LOG_INFO)
end
if j == 48 then break end
j = j +1
end
k = 1
for k = 1,8 do
local moonphase = json.daily.data[k].moonPhase
logWrite('moonPhase day ' ..k..' : '..tostring(moonphase))
logWrite(levelMoonhase(moonphase))
if(MoonPhaseSelect[k]) then
domoticz.devices(MoonPhaseSelect[k]).switchSelector(levelMoonhase(moonphase))
logWrite("update selector device")
end
if j == 8 then break end
j = j +1
end
end
end
}
moonSelectorIcons.lua :
Code : Tout sélectionner
local scriptName = 'moonSelectorIcons'
local scriptVersion = ' 1.0'
return {
active = true,
on = {
devices = {66}, -- change to your device(s) separated by a comma like {2479,2480}
},
logging = { level = domoticz.LOG_DEBUG, -- Seulement un niveau peut être actif; commenter les autres
-- level = domoticz.LOG_INFO, -- Only one level can be active; comment others
-- level = domoticz.LOG_ERROR,
-- level = domoticz.LOG_MODULE_EXEC_INFO,
marker = scriptName..' v'..scriptVersion
},
execute = function(domoticz, item)
local function logWrite(str,level)
domoticz.log(tostring(str),level or domoticz.LOG_DEBUG)
end
local icons = {
[10] = 101,-- level correspondant à Nouvelle lune
[20] = 102,-- level correspondant à Premier croissant
[30] = 103,-- level correspondant à Premier quartier
[40] = 104,-- level correspondant à Gibbeuse croissante
[50] = 105,-- level correspondant à Pleine lune
[60] = 106,-- level correspondant à Gibbeuse décroissante
[70] = 107,-- level correspondant à Dernier quartier
[80] = 108,-- level correspondant à Dernier croissant
}
local function setIcon(iconNumber)
local url = domoticz.settings['Domoticz url'] .. '/json.htm?type=setused&used=true&name=' .. domoticz.utils.urlEncode(item.name) ..
'&description=' .. domoticz.utils.urlEncode(item.description) .. -- Required. If not set it will be blanked out.
'&idx=' .. item.id ..
'&switchtype=' .. item.switchTypeValue ..
'&customimage=' .. iconNumber
logWrite(url)
return domoticz.openURL(url)
end
logWrite('lastLevel'.. tostring(item.lastLevel))
logWrite('level'.. tostring(item.level))
if item.level ~= item.lastLevel then
setIcon(icons[item.level])
domoticz.log('Changement icone Phase Lune : changé' .. item.id,domoticz.LOG_DEBUG)
else
domoticz.log('Changement icone Phase Lune : pas necessaire' .. item.id,domoticz.LOG_DEBUG)
end
end
}
pour les 2 scripts j'ai essayé de décommenter ces 3 niveau de logs un par un et un seul à la fois, j'ai toujours les mêmes messages d'erreur, seul le numéro de ligne change.
level = domoticz.LOG_DEBUG,
-- level = domoticz.LOG_INFO,
-- level = domoticz.LOG_ERROR,