Code : Tout sélectionner
--[[ --[[ ~/home/pi/domoticz/scripts/dzVents/scripts/weathervisualcrossing.lua
author : gso
: modifié pour weathervisualcrossing par BernardC
MAJ : 11/04/2023
création : 24/03/2023
Principe :
lien pour inscription à weather visual crossing:
https://www.visualcrossing.com/sign-up
lien pour récupérer la clé:
https://www.visualcrossing.com/account
requête type pour remplacer DarkSky API pour La localitée "Le Mans" dans Sarthe
https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/Le%20Mans/today?unitGroup=metric&include=fcst%2Ccurrent&key=<votre cle API>&options=nonulls&contentType=json
}
]]--
local scriptName = 'weather.visualcrossing'
local scriptVersion = '1.1'
--[[**********************************************
-- les 2 variables à renseigner
-- suivant utilisateur
--**********************************************]]--
local Localite = 'morigny%20champigny'
local apiKey = 'ma clé'
--**********************************************
--local uri = 'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/'..Localite..'?unitGroup=metric&include=events%2Cdays%2Ccurrent%2Chours%2Calerts&key='..apiKey..'&contentType=json'
local uri = 'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/'..Localite..'/today?unitGroup=metric&include=fcst%2Ccurrent&key='..apiKey..'&options=nonulls&contentType=json'
local prefix = 'WVC'
local StMeteo = { -- devices idx (à modifier en conséquence), fonction de maj donnée devices domoticz
-- {idx = 0, cde = function (dz, idx, v) dz.devices(idx).updateTempHum(v.currentConditions.temp, v.currentConditions.humidity, dz.HUM_COMPUTE) end },
-- Dew Point (°C) point rosée
-- {idx = 0, cde = function (dz, idx, v) dz.devices(idx).updateTemperature(v.currentConditions.dew) end },
-- Pression amosphérique
--{idx = 0, cde = function (dz, idx, v) dz.devices(idx).updatePressure(v.currentConditions.pressure) end },
--THB Barometre
{idx = 'Barometre', cde = function (dz, idx, v)
local precipprobr= v.currentConditions.precipprob -- probability of precipitation for tomorrow
local clouds = v.currentConditions.cloudcover
local baro = dz.BARO_NOINFO
if v.currentConditions.precipprob >= 0.3 then baro = dz.BARO_RAIN
elseif clouds >= 75 then baro = dz.BARO_CLOUDY
elseif clouds >= 25 then baro = dz.BARO_PARTLYCLOUDY -- et pas BARO_PARTLY_CLOUDY comme dans wiki ??
else baro = dz.BARO_SUNNY
end
dz.devices(idx).updateTempHumBaro(v.currentConditions.temp,v.currentConditions.humidity,dz.HUM_COMPUTE,v.currentConditions.pressure,baro)
end },
-- UV
{idx = 'UV', cde = function (dz, idx, v) dz.devices(idx).updateUV(v.currentConditions.uvindex) end },
-- Couverture nuageuse
{idx = 'Couverture nuageuse', cde = function (dz, idx, v) dz.devices(idx).updatePercentage(v.currentConditions.cloudcover) end },
-- Visibilité en (km)
{idx = 'Visibilite', cde = function (dz, idx, v) dz.devices(idx).updateVisibility(v.currentConditions.visibility) end },
-- Pluie MAJ le 11/04/2023
{idx = 'Pluie', cde = function (dz, idx, v)
if v.currentConditions.precip == nil then v.currentConditions.precip = 0 end
if v.days[1].precip == nil then v.v.days[1].precip = 0 end
--(hauteur en mm * 100 par heure, le compteur est total en mm)
dz.devices(idx).updateRain(v.currentConditions.precip*100, v.days[1].precip)
end },
-- Vent en m/s MAJ le 11/04/2023
{idx = 'Vent', cde = function (dz, idx, v)
if v.currentConditions.windgust == nil then v.currentConditions.windsgust = v.days[1].windgust end
degrees = v.currentConditions.winddir
local quadrants = {"NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"}
local quad = 'N'
if degrees <= 348.75 and degrees > 11.25 then quad = quadrants[tonumber(math.floor((degrees - 11.25) / 22.5)+1)] end
dz.devices(idx).updateWind(degrees, quad, v.currentConditions.windspeed/3.6, v.currentConditions.windgust/3.6, v.currentConditions.temp, v.currentConditions.feelslike)
-- bearing(°), direction(N S NNW..), speed(m/s), gust(m/s), temperature(°C), chill(°C)
-- vitesse vent/3,6 pour adapter l'affichage, les données reçu sont en Km/H
end },
--[[ -- devices à modifier pour l'ajout
-- Snow
{idx = 'Neige', cde = function (dz, idx, v)
if v.current.snow ~= nil then dz.devices(idx).updateRain(v.current.snow['1h']*100, v.daily[1].snow)
elseif v.daily[1] ~= nil then dz.devices(idx).updateRain(0, v.daily[1].snow)
else dz.devices(idx).updateRain(0, 0) end
end }, --rate in mm * 100 per hour, counter is total in mm)
-- Meteo
{idx = Meteo, cde = function (dz, idx, v)
local txt = v.current.weather[1].description..' ('..v.current.weather[1].main..' - '..v.current.weather[1].id..')'
dz.devices(idx).updateText(txt)
end },
-- Moon
{idx = 'Lune', cde = function (dz, idx, v)
-- local dt = os.date("%a %d %b à %X", v.daily[1].dt) -- UTC
local moonrise = os.date("%Hh%M", v.daily[1].moonrise) -- UTC
local moonset = os.date("%Hh%M", v.daily[1].moonset) -- UTC
local p = v.daily[1].moon_phase*100 -- 0 and 1 are 'new moon', 0.25 is 'first quarter moon', 0.5 is 'full moon' and 0.75 is 'last quarter moon'.
if p==0 or p==100 then phase = "Nouvelle Lune ?"
elseif p<25 then phase = "Premier Croissant ?"
elseif p==25 then phase = "Premier Quartier ?"
elseif p<50 then phase = "Gibeuse Croissante ?"
elseif p==50 then phase = "Pleine Lune ?"
elseif p<75 then phase = "Gibeuse Décroissante ?"
elseif p==75 then phase = "Dernier Quartier ?"
elseif p<100 then phase = "Dernier Croissant ?"
end
-- luminosity
p = 2*p
if p>100 then p = 200-p end
local txt = "Lever à "..moonrise.."\nCoucher à "..moonset.."\nPhase : "..phase.." ("..p.."%)"
dz.devices(idx).updateText(txt)
end },
-- Moon Phase (%)
{idx = 'Lune phase', cde = function (dz, idx, v)
dz.devices(idx).updatePercentage(v.daily[1].moon_phase*100)
end },
-- Alerts
--,"alerts":[{"sender_name":"METEO-FRANCE","event":"Moderate thunderstorm warning","start":1680321640,"end":1680386400,"description":"Moderate damages may occur, especially in vulnerable or in exposed areas and to people who carry out weather-related activities.","tags":["Thunderstorm"]}]}
{idx = 'Alerte', cde = function (dz, idx, v)
if v.alerts ~= nil then
local txt = ''
for k, alert in ipairs(v.alerts) do
local from = os.date("%a %d %b à %X", alert['start'])
local to = os.date("%a %d %b à %X", alert['end'])
txt = txt..k..'. '..alert.event.."\n"
txt = txt..' du '..from.."\n"
txt = txt..' au '..to.."\n"
txt = txt..alert.description.."\n"
-- txt = txt..alert.sender_name -- Meteo France
txt = txt..' tags : '..table.concat(alert.tags, ', ')..'\n\n'
end
-- updateAlertSensor(level, text): Function. Level can be domoticz.ALERTLEVEL_GREY, ALERTLEVEL_GREEN, ALERTLEVEL_YELLOW, ALERTLEVEL_ORANGE, ALERTLEVEL_RED. Supports command options.
dz.devices(idx).updateAlertSensor(dz.ALERTLEVEL_RED, txt)
dz.notify('Alerte Météo', txt, dz.PRIORITY_HIGH) -- notify(subject, message, priority, sound) domoticz.PRIORITY_LOW, PRIORITY_MODERATE, PRIORITY_NORMAL, PRIORITY_HIGH, PRIORITY_EMERGENCY
else
dz.devices(idx).updateAlertSensor(dz.ALERTLEVEL_GREEN, "Pas d'alerte")
end
end },
]]--
}
-- LOOP
return {
active = true, -- false,
on = {
timer = { "every 5 minutes" },
httpResponses = { "OWMcallback" }, -- correspond au caractère générique de rappel
},
logging = {
level = domoticz.LOG_ERROR, domoticz.LOG_INFO, --, domoticz.LOG_DEBUG, domoticz.LOG_MODULE_EXEC_INFO
marker = '-- '..scriptName..' v'..scriptVersion
},
-- LOOP
execute = function(dz, triggerItem)
local function errorMessage(message,notify) -- Ajouter une entrée au journal et notifier à tous les sous-systèmes
dz.log(message, dz.LOG_ERROR)
if notify then
-- dz.notify(message) -- notify(subject, message, priority, sound): Fonction. Envoi d’une notification (Prowl). La priorité sera choisie entre domoticz.PRIORITY_LOW, PRIORITY_MODERATE, PRIORITY_NORMAL, PRIORITY_HIGH, PRIORITY_EMERGENCY
end
end
-- MAIN
if triggerItem.isTimer then
local url = uri
dz.openURL({
url = url,
method = "GET",
callback= 'OWMcallback'
}).afterSec(1)
end
if triggerItem.isHTTPResponse then
if triggerItem.ok and triggerItem.isJSON then
local resultsTable = triggerItem.json
print('reception de weathervisualcrossing')
if resultsTable ~= nil and resultsTable.currentConditions ~= nil then
for k, d in ipairs(StMeteo) do
if d['idx'] ~= 0 then --0
d.cde(dz, d['idx'], resultsTable)
end
end
end
else
errorMessage("Probleme avec la reponse de weathervisualcrossing", false)
end
end
end --execute
}
Et voici la derniere erreur quand je recréer un widget barometre (j'en ai bien que 1 seul avec ce nom) :