Prévision de pluie en lua

Forum dédié aux problématiques concernant les scripts pour DomoticZ.
Entourez votre code et les logs avec les balises nommées code grâce au bouton <\>.
papoo
Messages : 2029
Inscription : 10 janv. 2016, 11:29
Localisation : Limoges (87)
Contact :

Prévision de pluie en lua

Message par papoo »

après la découverte de l'api météo france et son scrit bash proposé par Patrice (merci à lui) j'ai souhaité
avoir l'info via un script lua avec posibilité de mise à jour d'un device text et/ou alert et/ou notifications
si des fois cela peut vous servir

Code : Tout sélectionner

--[[   
~/domoticz/scripts/lua/script_time_meteofrance_pluie.lua
Download JSON.lua : http://regex.info/blog/lua/json 
]]--  
-- --------------------------------
------ 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
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, " ", "&nbsp;")
   end
   return str   
end
---------------------------------------------------------------------------	
now=os.date("*t")
if now.min % 50 == 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)   -- fonctionne
   end

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

   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

end --if now.min
return commandArray
Dernière modification par papoo le 11 mai 2016, 18:15, modifié 2 fois.
domoticz beta/RaspBerry PI3 stretch
https://pon.fr github
vil1driver
Messages : 5661
Inscription : 30 janv. 2015, 11:07
Localisation : Rennes (35)

Re: Prévision de pluie en lua

Message par vil1driver »

salut,

merci beaucoup

pour JSON.lua c'est ici http://regex.info/blog/lua/json


j'aime bien le.. voir_les_logs ;) et également le.. nil, je réutiliserai. (on sent que tu as l’habitude du code)
MAJ = VIDER LE CACHE(<-Clicable)
/!\Les mises à jour de Domoticz sont souvent sources de difficultés, ne sautez pas dessus
modules.lua

Un ex domoticzien
papoo
Messages : 2029
Inscription : 10 janv. 2016, 11:29
Localisation : Limoges (87)
Contact :

Re: Prévision de pluie en lua

Message par papoo »

vil1driver a écrit :(on sent que tu as l’habitude du code)
Pas vraiment, quelques relents de Php 4
mais je m'éclate comme un fou en découvrant le Lua et je m'inspire beaucoup de ce qui a déjà été fait
domoticz beta/RaspBerry PI3 stretch
https://pon.fr github
vil1driver
Messages : 5661
Inscription : 30 janv. 2015, 11:07
Localisation : Rennes (35)

Re: Prévision de pluie en lua

Message par vil1driver »

;)

en tout cas le script est proprement écrit et fonctionne impec, encore merci

par curiosité, ton script interroge météo france toutes les heures moins 10min, une raison précise à cela ?
MAJ = VIDER LE CACHE(<-Clicable)
/!\Les mises à jour de Domoticz sont souvent sources de difficultés, ne sautez pas dessus
modules.lua

Un ex domoticzien
papoo
Messages : 2029
Inscription : 10 janv. 2016, 11:29
Localisation : Limoges (87)
Contact :

Re: Prévision de pluie en lua

Message par papoo »

J'ai remarqué que mon pi était très occupé à faire les sauvegardes de Bdd toutes les heures à h00 du coup je lance les scripts un peu avant ou après h00
Je réfléchi par contre a afficher sur la custom page un graphique donut reprenant les 12 couleurs du risque de pluie délivrées par cette API mais j'ai pas encore trouvé comment faire l'API ne supportant pas le cross domain
domoticz beta/RaspBerry PI3 stretch
https://pon.fr github
vil1driver
Messages : 5661
Inscription : 30 janv. 2015, 11:07
Localisation : Rennes (35)

Re: Prévision de pluie en lua

Message par vil1driver »

ce qui serait top ce serait d'avoir le texte complet.

exemple,

manuellement en affichant le json j'obtiens ceci
"niveauPluieText" : [ "De15h40 à 15h45 : Précipitations faibles", "De15h45 à 16h40 : Pas de précipitations" ],
mais le capteur texte n'est renseigné qu'avec la première partie
De15h40 à 15h45 : Précipitations faibles
encore plus top serait de n'avoir un espace véritable qu'entre ces 2 parties (pour faire un retour chariot sur la custom page) les autres espaces étant remplacés par des

Code : Tout sélectionner

&nbsp;
MAJ = VIDER LE CACHE(<-Clicable)
/!\Les mises à jour de Domoticz sont souvent sources de difficultés, ne sautez pas dessus
modules.lua

Un ex domoticzien
papoo
Messages : 2029
Inscription : 10 janv. 2016, 11:29
Localisation : Limoges (87)
Contact :

Re: Prévision de pluie en lua

Message par papoo »

Ce doit être faisable
Je regarde ce soir
domoticz beta/RaspBerry PI3 stretch
https://pon.fr github
vil1driver
Messages : 5661
Inscription : 30 janv. 2015, 11:07
Localisation : Rennes (35)

Re: Prévision de pluie en lua

Message par vil1driver »

voili voiloo ;) et le capteur texte ne s'en porte pas plus mal

Code : Tout sélectionner

--[[   
~/domoticz/scripts/lua/script_time_meteofrance_pluie.lua

Download JSON.lua : http://regex.info/blog/lua/json
 
]]-- 
-- --------------------------------
------ Tableau à éditer ------
--------------------------------
local debugging = true  -- true pour voir les logs dans la console log Dz ou false pour ne pas les voir
local CityCode = 351490
local text_idx = 398 -- renseigner l'id du device text associé si souhaité, sinon nil
local rain_alert_idx = nil  -- 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
---------------------------------------------------------------------------
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, " ", "&nbsp;")
	end
	return str	
end
---------------------------------------------------------------------------   
now=os.date("*t")
if now.min % 50 == 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)   -- fonctionne
	end

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

	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

end --if now.min
return commandArray
sur la custom page, j'affiche donc ces infos dans la case meteo1 tout en haut à gauche (par exemple)
grâce à la modification des espaces le texte déborde allègrement sur la cellule meteo3, dans laquelle du coups je n'y affiche rien (Hide)
et dès que l'espace réel est détecté on change de ligne..

seulement l'écart entre 2 lignes est important, trop à mon goût (cela sert à l'origine à centrer verticalement le texte dans la case)
il faut donc modifier le css de la page en question et réduire cet inter-ligne comme ceci (à placer à la fin du fichier)

Code : Tout sélectionner

#meteo1
{ 
	line-height: 25px; 		
}
ps: j'en ai profité pour supprimer les ';' inutiles en lua ;)
MAJ = VIDER LE CACHE(<-Clicable)
/!\Les mises à jour de Domoticz sont souvent sources de difficultés, ne sautez pas dessus
modules.lua

Un ex domoticzien
papoo
Messages : 2029
Inscription : 10 janv. 2016, 11:29
Localisation : Limoges (87)
Contact :

Re: Prévision de pluie en lua

Message par papoo »

Merci
domoticz beta/RaspBerry PI3 stretch
https://pon.fr github
totof60
Messages : 373
Inscription : 29 avr. 2016, 09:31

Re: Prévision de pluie en lua

Message par totof60 »

merci pour ce code , ça va résoudre mon problème de crontab !
Domoticz V2022.2
sous Buster, Homebridge,RfLink
Raspberry pi3, Google home mini
plein de truc wifi !! de l'esp-01 jusque le nodemcu en passant par le wemos et même du DIY
Répondre