L'accès gratuit à l'api 3.0 ne fonctionne plus depuis qq temps pour moi. Je suis donc repassé sur l'api 2.5 qui est malheureusement moins riche en informations.
voici mon petit code DzVents correspondant.
Je n'ai pas vérifié l'exactitude la partie pluie ou il faut peut etre revoir la partie sur le cumul.
Code : Tout sélectionner
--[[ ~/home/pi/domoticz/scripts/dzVents/scripts/OpenWeatherMap.lua
author : gso
MAJ : 15/04/2023
création : 24/03/2023
Principe : Version gratuite sans prépaiement (juste inscription gratuite pour récupérer la clé api)
https://api.openweathermap.org/data/2.5/weather?lat=48.8&lon=2.3&appid=[votre clé api]&units=metric&lang=fr
{"coord":{"lon":2.3,"lat":48.8},
"weather":[{"id":800,"main":"Clear","description":"ciel dégagé","icon":"01n"}],
"base":"stations",
"main":{"temp":13.67,"feels_like":12.69,"temp_min":10.32,"temp_max":14.65,"pressure":1020,"humidity":61},
"visibility":10000,
"wind":{"speed":3.6,"deg":110},
"rain":{"1h":0.25},
"clouds":{"all":0},
"dt":1681071766,
"sys":{"type":2,"id":2041230,"country":"FR","sunrise":1681017147,"sunset":1681065103},
"timezone":7200,"id":2988507,"name":"Paris","cod":200
}
apiKey = to get on https://openweathermap.org/api and set in domoticz.variables with name "apikey_openweathermap" or set it in the code
]]--
-- local
local scriptName = 'OpenWeatherMap25'
local scriptVersion = '1.0'
local uri = 'https://api.openweathermap.org/data/2.5/weather'
local units = 'metric'
local lang = 'fr'
local rainCounter = 0
local devices = { -- devices idx (to be changed accordingly), dz update device function
-- Temperature (°C if metric)
-- {idx = 0, cde = function (dz, idx, v) dz.devices(idx).updateTempHum(v.current.temp, v.current.humidity, dz.HUM_COMPUTE) end },
-- Dew Point (°C)
-- {idx = 0, cde = function (dz, idx, v) dz.devices(idx).updateTemperature(v.current.dew_point) end },
-- Pressure
{idx = 986, cde = function (dz, idx, v) dz.devices(idx).updatePressure(v.main.pressure) end }, -- pressure
--THB
{idx = 982, cde = function (dz, idx, v)
-- local pop = v.daily[2].pop -- probability of precipitation for tomorrow
local clouds = v.clouds.all
local baro = dz.BARO_NOINFO
if v.rain ~= nil and v.rain['1h'] ~= nil then baro = dz.BARO_RAIN
elseif clouds >= 75 then baro = dz.BARO_CLOUDY
elseif clouds >= 25 then baro = dz.BARO_PARTLY_CLOUDY
else baro = dz.BARO_SUNNY
end
dz.devices(idx).updateTempHumBaro(v.main.temp, v.main.humidity, dz.HUM_COMPUTE, v.main.pressure, baro)
end },
-- Clouds (%)
{idx = 977, cde = function (dz, idx, v) dz.devices(idx).updatePercentage(v.clouds.all) end },
-- Visibility (km)
{idx = 983, cde = function (dz, idx, v) dz.devices(idx).updateVisibility(v.visibility/1000) end },
-- Wind
{idx = 984, cde = function (dz, idx, v)
if v.wind.gust == nil then v.wind.gust = v.wind.speed end
degrees = v.wind.deg
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.wind.speed, v.wind.gust, v.main.temp, v.main.feels_like)-- bearing(°), direction(N S NNW..), speed(m/s), gust(m/s), temperature(°C), chill(°C)
end },
-- Rain (rate in mm * 100 per hour, counter is total in mm) : to be checked
{idx = 985, cde = function (dz, idx, v)
if v.rain ~= nil and v.rain['1h'] ~= nil then
rainCounter = rainCounter + v.rain['1h']/6 -- /6 because every 10mn
dz.devices(idx).updateRain(v.rain['1h']*100, rainCounter)
--elseif v.daily[1] ~= nil then dz.devices(idx).updateRain(0, v.daily[1].rain)
else dz.devices(idx).updateRain(0, rainCounter) end
end },
-- Snow
{idx = 0, cde = function (dz, idx, v)
if v.snow ~= nil and v.snow['1h'] ~= nil then dz.devices(idx).updateRain(v.snow['1h']*100, nill)
-- elseif v.daily[1] ~= nil then dz.devices(idx).updateRain(0, v.daily[1].snow)
else dz.devices(idx).updateRain(0, 0) end
end },
-- Meteo
{idx = 979, cde = function (dz, idx, v)
local txt = v.weather[1].description..' ('..v.weather[1].main..' - '..v.weather[1].id..')'
dz.devices(idx).updateText(txt)
end },
}
-- LOOP
return {
active = true, -- false,
on = {
timer = { "every 10 minutes" },
httpResponses = { "OWM25callback" }, -- matches callback wildcard
},
-- data = {
-- safedMessage = { history = true, maxItems = 100 , maxHours = 168 }
-- },
logging = {
level = domoticz.LOG_ERROR, domoticz.LOG_INFO, --, domoticz.LOG_DEBUG, domoticz.LOG_MODULE_EXEC_INFO
marker = '-- '..scriptName..' v'..scriptVersion
},
-- EXEC
execute = function(dz, triggerItem)
local apiKey = dz.variables('apikey_openweathermap25').value
local function errorMessage(message,notify) -- Add entry to log and notify to all subsystems
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 pos = 'lat=' .. dz.settings.location.latitude .. "&lon=" .. dz.settings.location.longitude
local url = uri .."?"..pos..'&appid='..apiKey..'&units='..units..'&lang='..lang
-- errorMessage("DBG URL="..url)
dz.openURL({
url = url,
method = "GET",
callback= 'OWM25callback'
}).afterSec(1)
end
if triggerItem.isHTTPResponse then
if triggerItem.ok and triggerItem.isJSON then
local resultsTable = triggerItem.json
if resultsTable ~= nil and resultsTable.main ~= nil then
for k, d in ipairs(devices) do
if d['idx'] ~= 0 then d.cde(dz, d['idx'], resultsTable) end
end
end
else
errorMessage("Problem with response from OWM", false)
end
end
end --execute
}