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.
les logs du device text donnent ça
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
--[[ 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 ("<font color='#f3031d'>".. s .."</font>"); 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", "<font color='#999'>Pas de précipitations</font>") str = string.gsub (str, "Précipitations faibles", "<font color='#fbda21'>Précipitations faibles</font>") str = string.gsub (str, "Précipitations modérées", "<font color='#fb8a21'>Précipitations modérées</font>") str = string.gsub (str, "Précipitations fortes", "<font color='#f3031d'>Précipitations fortes</font>") 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