Re: Prévision de pluie en lua V2
Publié : 20 nov. 2016, 14:57
oups...mon script
--[[
script_time_meteofrance_pluie.lua
Download JSON.lua : http://regex.info/blog/lua/json
auteur : papoo
version : 2.00
maj : 31/10/2016
date : 11/05/2016
Principe : V1 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. viewtopic.php?f=10&t=1991
V2 en plus des fonctions de la V1, Afficher via les variables utilisateurs, les prédictions de précipitations sur une heure par pas de 5 mn sur la custom page
avec création automatiques des variables nécessaire à l'execution de ce script.
Actualisation des données toutes les 5 minutes, couleurs et texte directement récupérés via l'api
pour trouver le code de votre commune => http://www.insee.fr/fr/methodes/nomenclatures/cog/
]]--
--------------------------------------------
------------ Variables à éditer ------------
--------------------------------------------
local debugging = true -- true pour voir les logs dans la console log Dz ou false pour ne pas les voir
local ip = "192.168.1.16:8082";
local CityCode = 621080 -- le code de votre commune augmenté d'un 0 à la fin
local text_idx = 764 -- renseigner l'id du device text associé si souhaité, sinon nil
local rain_alert_idx = 765 -- renseigner l'id du device alert associé si souhaité, sinon nil
local send_notification = 0 -- 0: aucune notification, 1: toutes, 2: précipitations faibles, modérées et fortes, 3: modérées et fortes, 4: seulement fortes
local system = "linux" -- Systeme d'exploitation sur lequel est éxécuté ce script : linux, windows, synology pour le chemin vers JSON.LUA
local CustomPage = "oui" -- pour afficher les prévisions de pluie à une heure sur la custom page via des variables, valeurs acceptées : oui ou nil
--------------------------------------------
----------- Fin variables à éditer ---------
--------------------------------------------
local VariablesPluie={}
VariablesPluie[1]={nom = "Pluie à 5mn",Id = "1"}
VariablesPluie[2]={nom = "Pluie à 10mn",Id = "2"}
VariablesPluie[3]={nom = "Pluie à 15mn",Id = "3"}
VariablesPluie[4]={nom = "Pluie à 20mn",Id = "4"}
VariablesPluie[5]={nom = "Pluie à 25mn",Id = "5"}
VariablesPluie[6]={nom = "Pluie à 30mn",Id = "6"}
VariablesPluie[7]={nom = "Pluie à 35mn",Id = "7"}
VariablesPluie[8]={nom = "Pluie à 40mn",Id = "8"}
VariablesPluie[9]={nom = "Pluie à 45mn",Id = "9"}
VariablesPluie[10]={nom = "Pluie à 50mn",Id = "10"}
VariablesPluie[11]={nom = "Pluie à 55mn",Id = "11"}
VariablesPluie[12]={nom = "Pluie à 60mn",Id = "12"}
commandArray = {}
--------------------------------------------
---------------- Fonctions -----------------
--------------------------------------------
function voir_les_logs (s, debugging)
if (debugging) then
if s ~= nil then
print ("<font color='#f3031d'>".. s .."</font>")
else
print ("<font color='#f3031d'>aucune valeur affichable</font>")
end
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
function urlencode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
--------------------------------------------
---------------------------------------------------------------------------
time = os.date("*t")
if time.min % 2 == 0 then -- toutes les 5 minutes
--if time.hour % 1 == 0 then -- toutes les heures
voir_les_logs("--- --- ---script_time_meteofrance_pluie_V2.lua --- --- --- ",debugging)
local delay = os.time()
if system == "windows" then
json = (loadfile "D:\\Domoticz\\scripts\\lua\\json.lua")() -- For Windows
elseif system == "synology" then
json = (loadfile "/volume1/@appstore/domoticz/var/scripts/lua/JSON.lua")() -- For Synology
else
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
end
local indexArray=0
local config=assert(io.popen('curl http://www.meteofrance.com/mf3-rpc-port ... de..'.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 = ''
if niveauPluieText ~= nil then
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)
end
if CustomPage ~= nil then
--Récuperation des informations toutes les 5mn pour rafraichir les données des variables
local dataCadran={}
dataCadran = jsonLocation.dataCadran
local InfoNiveauPluieText = {}
local InfoNiveauPluie = {}
local InfoColor = {}
if dataCadran ~= nil then
for i, Result in ipairs( dataCadran ) do
InfoNiveauPluieText = Result.niveauPluieText
InfoNiveauPluie = Result.niveauPluie
InfoColor = Result.color
voir_les_logs("--- --- --- index : ".. i .. " Info : ".. InfoNiveauPluieText.. " Niveau : ".. InfoNiveauPluie .. " couleur :" .. InfoColor .. " --- --- ---",debugging)
end
end
for key, valeur in pairs(VariablesPluie) do
if(uservariables[valeur.nom] == nil) then
commandArray[indexArray]={['OpenURL']=ip..'/json.htm?type=command¶m=saveuservariable&vname='..urlencode(valeur.nom)..'&vtype=2&vvalue='..urlencode(otherdevices_lastupdate[valeur.nom])}
indexArray=indexArray+1
voir_les_logs("--- --- --- creation variable manquante "..valeur.nom.." --- --- --- ",debugging)
else
voir_les_logs("--- --- --- key :" .. key .." - "..valeur.nom.." --- --- --- ",debugging)
commandArray['Variable:'..valeur.nom] = tostring("<font color='#".. InfoColor[key] .."'>".. InfoNiveauPluieText[key] .."</font>")
end
end
end
if text_idx ~= nil and PluieText ~= 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
delay = os.time() - delay
voir_les_logs("--- --- --- Delai d'execution du script : " .. delay .."ms --- --- ---",debugging)
end --if time
return commandArray