Page 1 sur 1

[DzVents] Version Cartes Vigilance Météo 2025

Publié : 14 mars 2025, 09:36
par jackslayter
Bonjour,
voici le tuto pour avoir les vigilances de phénomènes visible sur la carte https://vigilance.meteofrance.fr/fr dans un capteur alerte dans Domoticz
vigi0.png
vigi0.png (8.97 Kio) Consulté 2855 fois

Création de compte sur l'API météofrance
https://portail-api.meteofrance.fr/web/fr/
rechercher "DonneesPubliqueVigilance" dans la barre de recherche
puis cliquer sur "Souscrire à l'API gratuitement"
vigi1.png
vigi1.png (60.83 Kio) Consulté 2855 fois
Ensuite cliquer sur votre nom d'utilisateur et ensuite sur "MES API"
vigi2.png
vigi2.png (17.29 Kio) Consulté 2855 fois
Et cliquer sur "Générer Token"
vigi3.png
vigi3.png (41.33 Kio) Consulté 2855 fois
Vérifier que le "OAuth2" est coché et copier la clé entre "Basic" et le dernier " (guillemet) ATTENTION ne pas le copier
vigi4.png
vigi4.png (53.35 Kio) Consulté 2855 fois

Créer un script DzVents (all) dans Domoticz et coller celui-ci en remplaçant par vos variables

Code : Tout sélectionner

------------ Variables à éditer ------------
local departement  = "75"       -- Votre numéro de département
local alert_device = "vigilance"   -- Capteur d'alerte
local key          = "copier le token ici" -- Votre token (client credentials)
----------- Fin des variables à éditer ---------

-- Callback utilisé pour la récupération du token et de la vigilance
return {
  on = {
    timer = { "every 6 hours" },
    httpResponses = { "token", "vigilance" }
  },
  
  execute = function(domoticz, item)
    if item.isHTTPResponse then
      -- Traitement de la réponse pour le token
      if item.callback == "token" then
        if item.ok then
          local tokenData = item.json
          if tokenData and tokenData.access_token then
            local token = tokenData.access_token
            domoticz.log("Token récupéré", domoticz.LOG_INFO)
            local url2 = "https://public-api.meteofrance.fr/public/DPVigilance/v1/cartevigilance/encours"
            local headers2 = { ["accept"] = "*/*", ["Authorization"] = "Bearer " .. token }
            domoticz.openURL({
              url = url2,
              method = "GET",
              headers = headers2,
              callback = "vigilance"
            })
          else
            domoticz.log("Token introuvable dans la réponse", domoticz.LOG_ERROR)
          end
        else
          domoticz.log("Erreur lors de la récupération du token", domoticz.LOG_ERROR)
        end

      -- Traitement de la réponse de DPVigilance
      elseif item.callback == "vigilance" then
        if item.ok then
          local json = item.json
          if json and json.product and json.product.periods then
            local periods = json.product.periods
            -- Supprimer les périodes dont l'échéance vaut "J1"
            for i = #periods, 1, -1 do
              if periods[i].echeance == "J1" then
                table.remove(periods, i)
              end
            end
            
            -- Recherche dans les périodes du domaine correspondant au département
            local donnees_bloc = nil
            local dep_str = tostring(departement)
            for _, periode in ipairs(periods) do
              if periode.timelaps and periode.timelaps.domain_ids then
                for _, domaine in ipairs(periode.timelaps.domain_ids) do
                  if tostring(domaine.domain_id) == dep_str then
                    donnees_bloc = domaine
                    break
                  end
                end
              end
              if donnees_bloc then break end
            end
            
            if not donnees_bloc then
              domoticz.log("Aucun domaine trouvé pour le département " .. dep_str, domoticz.LOG_ERROR)
              return {}
            end

            -- Détermination du niveau d'alerte global et construction des libellés de risque
            local riskTable = {}
            local maxRisk = 0
            if donnees_bloc.phenomenon_items then
              for _, itemPhen in ipairs(donnees_bloc.phenomenon_items) do
                local phenomenon_id = tonumber(itemPhen.phenomenon_id)
                local riskValue = tonumber(itemPhen.phenomenon_max_color_id) or 0
                if riskValue > maxRisk then
                  maxRisk = riskValue
                end
                local risque = ""
                if riskValue > 1 then
                  if phenomenon_id == 1 then
                    risque = "vent"
                  elseif phenomenon_id == 2 then
                    risque = "pluie"
                  elseif phenomenon_id == 3 then
                    risque = "orages"
                  elseif phenomenon_id == 4 then
                    risque = "crues"
                  elseif phenomenon_id == 5 then
                    risque = "neige-verglas"
                  elseif phenomenon_id == 6 then
                    risque = "canicule"
                  elseif phenomenon_id == 7 then
                    risque = "grand froid"
                  elseif phenomenon_id == 8 then
                    risque = "avalanches"
                  elseif phenomenon_id == 9 then
                    risque = "vagues submersion"
                  end
                  if risque ~= "" then
                    table.insert(riskTable, risque)
                  end
                end
              end
            end
            
            local risquef = table.concat(riskTable, ",")
            
            -- Définition du niveau d'alerte selon maxRisk
            local alertLevel = domoticz.ALERTLEVEL_GREY
            if maxRisk == 1 then
              alertLevel = domoticz.ALERTLEVEL_GREEN
            elseif maxRisk == 2 then
              alertLevel = domoticz.ALERTLEVEL_YELLOW
            elseif maxRisk == 3 then
              alertLevel = domoticz.ALERTLEVEL_ORANGE
            elseif maxRisk == 4 then
              alertLevel = domoticz.ALERTLEVEL_RED
            end
            
            -- Récupérer l'ancien texte du capteur et comparer
            local dev = domoticz.devices(alert_device)
            if dev then
              local oldText = dev.text or ""
              if oldText ~= risquef then
                domoticz.log("Mise à jour de " .. alert_device .. " avec : " .. risquef, domoticz.LOG_INFO)
                dev.updateAlertSensor(alertLevel, risquef)
              else
                domoticz.log("Aucune mise à jour, texte identique", domoticz.LOG_INFO)
              end
            else
              domoticz.log("Device " .. alert_device .. " introuvable", domoticz.LOG_ERROR)
            end
          else
            domoticz.log("JSON invalide : clé product.periods non trouvée", domoticz.LOG_ERROR)
          end
        else
          domoticz.log("Réponse HTTP incorrecte pour DPVigilance", domoticz.LOG_ERROR)
        end
        return {}
      end
    else
      -- Si ce n'est pas une réponse HTTP, on lance la requête pour obtenir le token
      local token_url = "https://portail-api.meteofrance.fr/token"
      local data = "grant_type=client_credentials"
      local headers = { ["Authorization"] = "Basic " .. key }
      domoticz.openURL({
        url = token_url,
        method = "POST",
        data = data,
        headers = headers,
        callback = "token"
      })
    end
  end
}

Re: [DzVents] Version Cartes Vigilance Météo 2025

Publié : 16 mars 2025, 20:31
par val
Bonjour, j'avais des problèmes avec la version python celle-ci fonctionne parfaitement, merci à toi.