[DzVents] Prévision météo avec open-meteo.com

Vous avez créé un script LUA dont vous êtes fier, un .sh génial, un programme Python hors du commun, un Tuto, c'est ici que vous pouvez les partager.
boum
Messages : 233
Inscription : 18 janv. 2019, 11:34
Localisation : France

[DzVents] Prévision météo avec open-meteo.com

Message par boum »

Sous 2022.2, le hardware openweather ne marche plus. L’info est pratique et pour ne pas attendre une future mise à jour pour que ça remarche ou utiliser visual crossing, le plus simple était d’écrire un petit scrip.
https://open-meteo.com/en/docs permet 10000 appels par jour gratuitement. Je n’ai pas besoin d’autant surtout que je ne récupère pas de données heure-par-heure.

Il manque encore certaines infos à renseigner comme les précipitations, la couverture nuageuse et un texte de description plus complet. Peut-être dans une prochaine version. L’API est facile à utiliser, ce script a été écrit en une soirée (plus qq fignolages le lendemain).

Pour l’utiliser il faut 8 dispositifs virtuels. La moitié pour la météo du jour, l’autre pour le lendemain:
Capture des dispositifs météo
Capture des dispositifs météo
open-meteo-dzvents.jpg (94.97 Kio) Consulté 3767 fois
Pour chaque jour, il faut un dispositif Baromètre, un Indice UV et 2 Vent+Temp+Ressenti

Code : Tout sélectionner

local devices = { 
    { baro='Météo Aujourd’hui', max='Maxima Aujourd’hui', min='Minima Aujourd’hui', uv='UV Aujourd’hui', }, 
    { baro='Météo Demain',      max='Maxima Demain',      min='Minima Demain',      uv='UV Demain', }
}

local directions = { 'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N' }
local nomDirection = function(bearing) return directions[ math.floor( (bearing + 33.75) / 22.5 ) ] end
local prevision = function(domoticz, weather_code)
    -- 0	Clear sky
    -- 1, 2, 3	Mainly clear, partly cloudy, and overcast
    if weather_code <= 1 then return domoticz.BARO_SUNNY
    -- 45, 48	Fog and depositing rime fog
    elseif weather_code <= 48 then return domoticz.BARO_CLOUDY
    -- 51, 53, 55	Drizzle: Light, moderate, and dense intensity
    -- 56, 57	Freezing Drizzle: Light and dense intensity
    -- 61, 63, 65	Rain: Slight, moderate and heavy intensity
    -- 66, 67	Freezing Rain: Light and heavy intensity
    -- 71, 73, 75	Snow fall: Slight, moderate, and heavy intensity
    -- 77	Snow grains
    -- 80, 81, 82	Rain showers: Slight, moderate, and violent
    -- 85, 86	Snow showers slight and heavy
    elseif weather_code <= 86 then return domoticz.BARO_UNSTABLE -- pas encore dispo BARO_CLOUDY_RAIN
    -- 95 *	Thunderstorm: Slight or moderate
    -- 96, 99 *	Thunderstorm with slight and heavy hail
    elseif weather_code <= 99 then return domoticz.BARO_THUNDERSTORM
    else return domoticz.BARO_NOINFO
    end
end

return {
	active = { true },
	on = {
		-- device triggers: Test uniquement, lance une requête ou affiche les paramètres
		-- devices = { 'TestPushButton', },

		-- timer triggers
		timer = { 'at 2:32', 'at 8:05', },

		-- http responses
		httpResponses = { 'OpenMeteoAPI' },
	},

	logging = {
        level = domoticz.LOG_INFO,
        marker = "Open-Meteo"
    },

	execute = function(domoticz, item, info)
	   local json
       --if (item.isDevice and item.active) or (item.isTimer) then
       if item.isTimer then
            local url = "https://api.open-meteo.com/v1/forecast?" .. table.concat({
                "latitude=" .. tostring(domoticz.settings.location.latitude),
                "longitude=" .. tostring(domoticz.settings.location.longitude),
                "timezone=Europe%2FBerlin",
                "forecast_days=2",
                "wind_speed_unit=ms",
                "daily=" .. table.concat({'weather_code','uv_index_max','cloud_cover_mean','pressure_msl_mean','precipitation_hours',
                    'temperature_2m_max','apparent_temperature_max','temperature_2m_min','apparent_temperature_min',
                    'wind_speed_10m_max','wind_gusts_10m_max','wind_speed_10m_min','wind_gusts_10m_min','wind_direction_10m_dominant',}, ','),
                }, '&')
            domoticz.openURL({ url = url, method = 'GET', callback = 'OpenMeteoAPI' })
            return
        elseif (item.isDevice and item.active) then
            print("Current location: latitude=" .. tostring(domoticz.settings.location.latitude) .. ", longitude=" .. tostring(domoticz.settings.location.longitude))
            return
        elseif item.isHTTPResponse then
            if item.ok and item.isJSON then -- statusCode == 2xx
                json = item.json
            end
        end
        
        if not json then
            domoticz.log('No JSON', domoticz.LOG_ERROR)
            return 
        end

        for i, d in ipairs(devices) do
            local uvIndexMax = json.daily.uv_index_max[i]
            domoticz.devices(d.uv).updateUV(uvIndexMax)
            
            local bearing = json.daily.wind_direction_10m_dominant[i]
            local direction = nomDirection(bearing)
            local speed = json.daily.wind_speed_10m_max[i]
            local gust = json.daily.wind_gusts_10m_max[i]
            local temperature = json.daily.temperature_2m_max[i]
            local chill = json.daily.apparent_temperature_max[i]
            domoticz.devices(d.max).updateWind(bearing, direction, speed, gust, temperature, chill)

            local speedMin = json.daily.wind_speed_10m_min[i]
            local gustMin = json.daily.wind_gusts_10m_min[i]
            local temperatureMin = json.daily.temperature_2m_min[i]
            local chillMin = json.daily.apparent_temperature_min[i]
            domoticz.devices(d.min).updateWind(bearing, direction, speedMin, gustMin, temperatureMin, chillMin)
            
            local pressure = json.daily.pressure_msl_mean[i]
            local forecast = prevision(domoticz, json.daily.weather_code[i])
            domoticz.devices(d.baro).updateBarometer(pressure, forecast)
        end
        
end
}
Domoticz v2022.2 sur linux-mint / Z-Wave-JS / RFXCom / Zigbee2MQTT
Keros
Messages : 6638
Inscription : 23 juil. 2019, 20:57

Re: [DzVents] Prévision météo avec open-meteo.com

Message par Keros »

Merci boum

J'avais repéré cette API il y a quelques semaines et je comptais en parlé quand j'aurai eu un peu de temps !!

Allez voir le lien vers la documentation : leur site est super bien fait pour définir les infos que vous souhaitez dans votre Json.
Après, un petit script et vous remontez toutes les infos que vous voulez vers DomoticZ :mrgreen:
Comment bien utiliser le forum : Poser une question, Mettre un script, un fichier, une image ou des logs
Mes petits guides : Débuter en programmation, Le débogage, Le choix de matériel, Les sauvegardes
Ma présentation - Mes Tutos
Répondre