Prévision de pluie en lua

Après la découverte de l’api météo France et son scrit bash proposé par easydomoticz
j’ai souhaité récupérer les informations de l’API non documentée de météo France via un script Lua avec possibilité de mise à jour d’un device text et/ou alert et/ou notifications.

Prévision de pluie

les logs du device text donnent ça
logs pluie

--[[   
script_time_meteofrance_pluie.lua
Download JSON.lua : http://regex.info/blog/lua/json 
  
auteur : papoo
version : 1.02
date : 11/05/2016
Principe : récupérer via l'API non documentée de météo France 
les informations de précipitation de votre commune sur un device text et/ou alert et/ou notifications
pour trouver le code de votre commune => http://www.insee.fr/fr/methodes/nomenclatures/cog/
]]--
-- --------------------------------
------ Tableau à éditer ------
--------------------------------
local debugging = false  -- true pour voir les logs dans la console log Dz ou false pour ne pas les voir
local CityCode = 870850 -- le code de votre commune augmenté d'un 0 à la fin
local text_idx = 343 -- renseigner l'id du device text associé si souhaité, sinon nil
local rain_alert_idx = 699  -- renseigner l'id du device alert associé si souhaité, sinon nil
local send_notification = 3 -- 0: aucune notification, 1: toutes, 2: précipitations faibles, modérées et fortes, 3: modérées et fortes, 4: seulement fortes
---------------------------------------------------------------------------
commandArray = {}
--------------------------------------------------------------------------- 
--Fonctions
---------------------------------------------------------------------------
function voir_les_logs (s)
    if (debugging) then 
        print ("".. s .."");
    end
end	

function format(str)
   if (str) then
      str = string.gsub (str, "De", "De ")
      str = string.gsub (str, " ", " ")
      str = string.gsub (str, "Pas de précipitations", "Pas de précipitations")
      str = string.gsub (str, "Précipitations faibles", "Précipitations faibles")
      str = string.gsub (str, "Précipitations modérées", "Précipitations modérées")
      str = string.gsub (str, "Précipitations fortes", "Précipitations fortes")
   end
   return str   
end
---------------------------------------------------------------------------	
now=os.date("*t")
if now.min % 56 == 0 then 
print('script_time_meteofrance_pluie.lua')
  json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
   --json = (loadfile "D:\\Domoticz\\scripts\\lua\\json.lua")()  -- For Windows

   
   local indexArray=0


   local config=assert(io.popen('curl http://www.meteofrance.com/mf3-rpc-portlet/rest/pluie/'..CityCode..'.json'))
   local location = config:read('*all')
   config:close()
   local jsonLocation = json:decode(location)


   local niveauPluieText={}   
   niveauPluieText = jsonLocation.niveauPluieText

   -- concaténation des entrées de la table et formatage
   local PluieText = ''
   for Index, Value in pairs( niveauPluieText ) do
      PluieText = PluieText..format(Value)..' '
	  
      voir_les_logs("--- --- --- niveauPluieText["..Index.."] : ".. Value,debugging)  
   end

   voir_les_logs("--- --- --- PluieText : ".. PluieText,debugging)  

   if text_idx ~= nil then
      commandArray[indexArray] = {['UpdateDevice'] = text_idx .. '|0| ' .. PluieText}
      indexArray=indexArray+1
   end
        
   if string.find(niveauPluieText[1], "Pas de précipitations")  then
      if rain_alert_idx ~= nil then
         commandArray[indexArray] = {['UpdateDevice'] = rain_alert_idx..'|1|Pas de précipitations'}
         indexArray=indexArray+1
      end
      if send_notification > 0 and send_notification < 2 then
         commandArray[indexArray] = {['SendNotification'] = 'Alerte Météo#Pas de précipitations prévue!'}
         indexArray=indexArray+1
      end
      voir_les_logs("--- --- --- Pas de précipitations --- --- ---",debugging)

   elseif string.find(niveauPluieText[1], "faibles")  then
      if rain_alert_idx ~= nil then
         commandArray[indexArray] = {['UpdateDevice'] = rain_alert_idx..'|2|Précipitations Faibles'}
         indexArray=indexArray+1
      end
      if send_notification > 0 and send_notification < 3 then
         commandArray[indexArray] = {['SendNotification'] = 'Alerte Météo#Précipitations Faibles!'}
         indexArray=indexArray+1
      end
      voir_les_logs("--- --- --- Précipitations Faibles --- --- ---",debugging)   


   elseif string.find(niveauPluieText[1], "modérées")  then
      if rain_alert_idx ~= nil then
         commandArray[indexArray] = {['UpdateDevice'] = rain_alert_idx..'|3|Précipitations modérées'}
         indexArray=indexArray+1
      end
      if send_notification > 0 and send_notification < 4 then
         commandArray[indexArray] = {['SendNotification'] = 'Alerte Météo#Précipitations modérées!'}
         indexArray=indexArray+1
      end
      voir_les_logs("--- --- --- Précipitations modérées --- --- ---",debugging)      


   elseif string.find(niveauPluieText[1], "fortes")  then
      if rain_alert_idx ~= nil then
         commandArray[indexArray] = {['UpdateDevice'] = rain_alert_idx..'|4|Précipitations fortes'}
         indexArray=indexArray+1
      end
      if send_notification > 0 and send_notification < 5 then
         commandArray[indexArray] = {['SendNotification'] = 'Alerte Météo#Précipitations fortes!'}
         indexArray=indexArray+1
      end
      voir_les_logs("--- --- --- Précipitations fortes --- --- ---",debugging)

   else
      print("niveau non defini")
   end
-----------------------------------------------------------------
   -- local dataCadran={}   
   -- dataCadran = jsonLocation.dataCadran   
   -- for Index, Value in pairs( dataCadran ) do
   -- local colors = ''
      -- colors = colors.. Value.color..' '   
   -- local niveauPluie = ''
	-- niveauPluie = niveauPluie .. Value.niveauPluie..' ' 
  
      -- voir_les_logs("--- --- --- Couleurs : ".. colors,debugging) 
	  -- voir_les_logs("--- --- --- niveauPluie : ".. niveauPluie,debugging)
    -- end  -- for dataCadran 
end --if now.min
return commandArray

si des fois cela peut vous servir

Laisser un commentaire