Qualité de l’air en Province

Bonjour,
un autre script LUA basé sur l’article sur le blog easydomoticz et le commentaire de nam
Ce script a pour but de remonter les informations de pollution du site http://www.lcsqa.org 3 fois par jour à 07H10 et 18H10 (éditable ligne 72)
sur domoticz en LUA et de nous alerter le cas échéant selon le niveau de notification choisi
seules les villes suivantes ont des informations sur la pollution! Certaines n’ont que l’indice de pollution disponible, d’autres
ont toutes ou parties des différents sous indices. Pensez à vérifier sur le site de http://www.lcsqa.org/surveillance/indices/liste/jour les informations disponibles. Ces données ne sont pas disponible 365 jours /an , il se peut que certains jours les données de votre commune ne soient pas présente dans le fichier xml.
2016-05-18 22_49_06-Domoticz
Liste des villes (98):
FORT-DE-FRANCE, AIRVAULT, AIX-EN-PROVENCE, AJACCIO, ALBERTVILLE, ALENCON, AMIENS, ANGOULEME, ANNECY, ANNEMASSE, ARMENTIERES, ARRAS, AUBAGNE, AURILLAC, AUXERRE,
AVIGNON, BASTIA, BETHUNE, BOURG-EN-BRESSE, BOURGOIN-JALLIEU, BRIGNOLES, BRIVE-LA-GAILLARDE, CAEN, CALAIS, CANNES, CHALONS-EN-CHAMPAGNE, CHAMBERY,
CHAMONIX-MONT-BLANC, CHARLEVILLE-MEZIERES, CHERBOURG-OCTEVILLE, CLERMONT-FERRAND, COGNAC, COLMAR, CREIL, DIJON, DOUAI, DUNKERQUE, EVREUX, FORBACH, GAP,
GRANDFONTAINE, GRENOBLE, GUERET, HAVRE, HYERES, ISSOIRE, LENS, LILLE, LIMOGES, LISIEUX, LYON, MACON, MARSEILLE, MAUBEUGE, METZ, MONTCEAU-LES-MINES, MONTLUCON,
MOULINS, MULHOUSE, MUNCHHAUSEN, NANCY, NEVERS, NICE, NIORT, PARIS, PASSY, PETITE-PIERRE, POITIERS, PUY-EN-VELAY, REIMS, RIOM, ROANNE, ROCHELLE, ROMANS-SUR-ISERE,
ROUEN, SAINT-DENIS, SAINT-DIZIER, SAINT-ETIENNE, SAINT-JUNIEN, SAINT-OMER, SAINT-PAUL, SAINT-PIERRE, SAINT-QUENTIN, SENS, STRASBOURG, THIONVILLE, TOULON, TROYES,
TULLE, VALENCE, VALENCIENNES, VENACO, VIENNE, VILLAGE-NEUF, BOULOGNE-SUR-MER, CHALON-SUR-SAONE, SAINT-JEAN-DE-MAURIENNE, SAINT-LO

--[[
name : script_time_pollution.lua
auteur : papoo
version : 1.13
Création : 17/05/2016
Mise à jour : 12/01/2017
basé sur l'article http://easydomoticz.com/domoticz-integrer-les-donnees-qualite-de-lair-en-dehors-de-paris/ et le commentaire de nam
 Ce script a pour but de remonter les informations de pollution du site http://www.lcsqa.org 2 fois par jour à 07H10 et 18H10 (éditable ligne 72)
 sur domoticz et de nous alerter le cas écheant selon le niveau de notification choisi
 L'indice de pollution remonte sur un widget Alert
 version 1.13 suite à l'indisponibilité des données du jour, explotation des données de la veille et suppression de la récuperation des commentaires (indisponible)
 Présentation de ce script : /qualite-de-lair-en-province/
]]--
--------------------------------------------
------------ Variables à éditer ------------
-------------------------------------------- 

local debugging = false  	-- true pour voir les logs dans la console log Dz ou false pour ne pas les voir
local ville='LIMOGES'  --[[  seules les villes suivantes ont des informations sur la pollution! Certaines n'ont que l'indice de pollution disponible, d'autres 
ont toutes ou parties des différents sous indices. Pensez à verifier sur le site de http://www.lcsqa.org/surveillance/indices/liste/jour les informations disponibles.
Liste des villes (98): 
FORT-DE-FRANCE, AIRVAULT, AIX-EN-PROVENCE, AJACCIO, ALBERTVILLE, ALENCON, AMIENS, ANGOULEME, ANNECY, ANNEMASSE, ARMENTIERES, ARRAS, AUBAGNE, AURILLAC, AUXERRE,
AVIGNON, BASTIA, BETHUNE, BOURG-EN-BRESSE, BOURGOIN-JALLIEU, BRIGNOLES, BRIVE-LA-GAILLARDE, CAEN, CALAIS, CANNES, CHALONS-EN-CHAMPAGNE, CHAMBERY, 
CHAMONIX-MONT-BLANC, CHARLEVILLE-MEZIERES, CHERBOURG-OCTEVILLE, CLERMONT-FERRAND, COGNAC, COLMAR, CREIL, DIJON, DOUAI, DUNKERQUE, EVREUX, FORBACH, GAP, 
GRANDFONTAINE, GRENOBLE, GUERET, HAVRE, HYERES, ISSOIRE, LENS, LILLE, LIMOGES, LISIEUX, LYON, MACON, MARSEILLE, MAUBEUGE, METZ, MONTCEAU-LES-MINES, MONTLUCON, 
MOULINS, MULHOUSE, MUNCHHAUSEN, NANCY, NEVERS, NICE, NIORT, PARIS, PASSY, PETITE-PIERRE, POITIERS, PUY-EN-VELAY, REIMS, RIOM, ROANNE, ROCHELLE, ROMANS-SUR-ISERE, 
ROUEN, SAINT-DENIS, SAINT-DIZIER, SAINT-ETIENNE, SAINT-JUNIEN, SAINT-OMER, SAINT-PAUL, SAINT-PIERRE, SAINT-QUENTIN, SENS, STRASBOURG, THIONVILLE, TOULON, TROYES, 
TULLE, VALENCE, VALENCIENNES, VENACO, VIENNE, VILLAGE-NEUF, BOULOGNE-SUR-MER, CHALON-SUR-SAONE, SAINT-JEAN-DE-MAURIENNE, SAINT-LO
]]--

local dz_indice_alert = 723	-- renseigner l'id du device alert pollution associé si souhaité, sinon nil (dummy - alert)
local dz_indice_log = 724	-- renseigner l'id du device indice de pollution associé si souhaité, sinon nil (pour le log des données avec graphique)
local dz_o3 = nil			-- renseigner l'id du device Ozone associé si souhaité, sinon nil
local dz_no2 = nil			-- renseigner l'id du device dioxyde d'azote associé si souhaité, sinon nil
local dz_PM10 = nil			-- renseigner l'id du device taux de particules associé si souhaité, sinon nil
local dz_so2 = nil			-- renseigner l'id du device dioxyde de soufre associé si souhaité, sinon nil
--local dz_com = nil			-- renseigner l'id du device texte commentaire associé si souhaité, sinon nil
local dz_source = nil 		-- renseigner l'id du device texte lien vers la source des mesures associé si souhaité, sinon nil
local send_notification = 3 -- 0: aucune notification, 1: toutes, 2: (2> Pollution <4), 3: (4> Pollution <6), 4: (6> Pollution <8), 5: (Pollution >8)

--------------------------------------------
----------- Fin variables à éditer ---------
--------------------------------------------
local dateIndice= nil
local CodeInsee= nil
local valeurIndice= nil
local SousIndiceO3= nil 
local SousIndiceNO2= nil
local SousIndicePM10= nil
local SousIndiceSO2= nil
local Commentaire= nil
local sourceAASQA= nil
local indexArray=0	
--------------------------------------------
---------------- Fonctions -----------------
-------------------------------------------- 
function voir_les_logs (s, debugging)
    if (debugging) then 
		if s ~= nil then
        print ("".. s .."")
		else
		print ("aucune valeur affichable")
		end
    end
end	
--------------------------------------------
-------------- Fin Fonctions ---------------
--------------------------------------------
commandArray = {}
now=os.date("*t")
--if (now.min == 10 and ((now.hour == 8) or (now.hour == 15) or (now.hour == 20))) then -- 3 éxecutions du script par jour à 8H10, 15h10 et 20H10
if (now.min == 10 and now.hour == 20) then -- 1 éxecution du script par jour à 21H10
--if now.min % 2 == 0 then  -- execution toutes les 2 minutes
voir_les_logs("script_time_pollution.lua",debugging)


-- deux méthodes pour récupérer les informations xml,  WGET ou CURL si vous dé-commentez la méthode curl pensez à commenter la méthode wget
		-- début methode wget
		local fname ="/media/Freebox/Trend/pollution_air.xml"
--		local fname ="pollution_air.xml"		
--		os.execute("wget -q -O " .. fname .. " http://www.lcsqa.org/surveillance/indices/prevus/jour/xml") 
		os.execute("wget -q -O " .. fname .. " http://www.lcsqa.org/indices-qualite-air/xml/")		
		-- fin methode wget
		
		-- début methode curl
		 --local fname ="/tmp/pollution_air.xml"
		 --os.execute("curl http://www.lcsqa.org/surveillance/indices/prevus/jour/xml > ".. fname .." && chmod 777 " .. fname)
		-- fin methode curl
		
		
		 voir_les_logs("Fichier local : " ..fname,debugging)
		local f = io.open(fname, "r")

			 if f == nil then
			 print("Error opening file '" .. fname .. "'.")
			 os.exit(1)
			 end
 
		local xml = f:read("*all")
					f:close()

	
	for instance in xml:gmatch("(.-)") do 
	agglomeration=instance:match('(.-)')
			if agglomeration == ville then
	 dateIndice=instance:match('(.-)')
	CodeInsee=instance:match('(.-)')
	valeurIndice=instance:match('(.-)')
	SousIndiceO3=instance:match('(.-)')
	SousIndiceNO2=instance:match('(.-)')
	SousIndicePM10=instance:match('(.-)')
	SousIndiceSO2=instance:match('(.-)')
	--Commentaire=instance:match('(.-)')
	sourceAASQA=instance:match('(.-)')
			end
	end

		 
		 
		if valeurIndice == nil and dz_indice_alert ~= nil then
			 commandArray[indexArray] = {['UpdateDevice'] = dz_indice_alert..'|0|Aucune donnée disponible'}
			voir_les_logs("--- --- --- Aucune donnée disponible pour la ville : ".. ville,debugging) 
			 indexArray=indexArray+1
		end	


	if valeurIndice	~= nil then	
		voir_les_logs("--- --- --- dateIndice : ".. dateIndice,debugging)
		voir_les_logs("--- --- --- CodeInsee : ".. CodeInsee,debugging)
		voir_les_logs("--- --- --- Agglomeration : ".. ville,debugging)
		voir_les_logs("--- --- --- valeurIndice : ".. valeurIndice,debugging)
		voir_les_logs("--- --- --- SousIndiceO3 : ".. SousIndiceO3,debugging)
		voir_les_logs("--- --- --- SousIndiceNO2 : ".. SousIndiceNO2,debugging)
		voir_les_logs("--- --- --- SousIndicePM10 : ".. SousIndicePM10,debugging)
		voir_les_logs("--- --- --- SousIndiceSO2 : ".. SousIndiceSO2,debugging)
		--voir_les_logs("--- --- --- Commentaire : ".. Commentaire,debugging)
		voir_les_logs("--- --- --- sourceAASQA : ".. sourceAASQA,debugging)

		 

	   if tonumber(valeurIndice) <= 4   then -- niveau 2
		  if dz_indice_alert ~= nil then
			 commandArray[indexArray] = {['UpdateDevice'] = dz_indice_alert..'|1|Pas de Pollution'}
			 indexArray=indexArray+1
		  end
		  if send_notification > 0 and send_notification < 2 then
			 commandArray[indexArray] = {['SendNotification'] = 'Alerte Pollution#Pas de Pollution!'}
			 indexArray=indexArray+1
		  end
		  voir_les_logs("--- --- --- Pas de pollution --- --- ---",debugging)

		  
	   elseif tonumber(valeurIndice) <= 6   then -- niveau 3
		  if dz_indice_alert ~= nil then
			 commandArray[indexArray] = {['UpdateDevice'] = dz_indice_alert..'|2|Polution Faible'}
			 indexArray=indexArray+1
		  end
		  if send_notification > 0 and send_notification < 3 then
			 commandArray[indexArray] = {['SendNotification'] = 'Alerte Pollution#Pollution Faible!'}
			 indexArray=indexArray+1
		  end
		  voir_les_logs("--- --- --- Pollution Faible --- --- ---",debugging)   


	   elseif tonumber(valeurIndice) <= 8   then -- niveau 4
		  if dz_indice_alert ~= nil then
			 commandArray[indexArray] = {['UpdateDevice'] = dz_indice_alert..'|3|Pollution Forte'}
			 indexArray=indexArray+1
		  end
		  if send_notification > 0 and send_notification < 4 then
			 commandArray[indexArray] = {['SendNotification'] = 'Alerte Pollution#Pollution Forte!'}
			 indexArray=indexArray+1
		  end
		  voir_les_logs("--- --- --- Pollution Forte --- --- ---",debugging)      


	   elseif tonumber(valeurIndice) > 8  then -- niveau 5
		  if dz_indice_alert ~= nil then
			 commandArray[indexArray] = {['UpdateDevice'] = dz_indice_alert..'|4|Pollution trés forte'}
			 indexArray=indexArray+1
		  end
		  if send_notification > 0 and send_notification < 5 then
			 commandArray[indexArray] = {['SendNotification'] = 'Alerte Pollution#Pollution trés forte!'}
			 indexArray=indexArray+1
		  end
		  voir_les_logs("--- --- --- Pollution très forte --- --- ---",debugging)

	   else
		  print("niveau non defini")
	   end		
			
		  if dz_indice_log ~= nil then -- Mise à jour du devise O3 si il existe
			 commandArray[indexArray] = {['UpdateDevice'] = dz_indice_log..'|0|'.. tostring(valeurIndice)}
			 indexArray=indexArray+1
		  end	
		  if dz_no2 ~= nil then -- Mise à jour du devise NO3 si il existe
			 commandArray[indexArray] = {['UpdateDevice'] = dz_no2..'|0|'.. tostring(SousIndiceNO2)}
			 indexArray=indexArray+1
		  end	
		  if dz_PM10 ~= nil then -- Mise à jour du devise PM10 si il existe
			 commandArray[indexArray] = {['UpdateDevice'] = dz_PM10..'|0|'.. tostring(SousIndicePM10)}
			 indexArray=indexArray+1
		  end
		  if dz_so2 ~= nil then -- Mise à jour du devise SO2 si il existe
			 commandArray[indexArray] = {['UpdateDevice'] = dz_so2..'|0|'.. tostring(SousIndiceSO2)}
			 indexArray=indexArray+1
		  end
		  -- if dz_com ~= nil then -- Mise à jour du devise Commentaire si il existe
			 -- commandArray[indexArray] = {['UpdateDevice'] = dz_com..'|0|'.. tostring(Commentaire)}
			 -- indexArray=indexArray+1
		  -- end
		  if dz_source ~= nil then -- Mise à jour du devise Source si il existe
			sourceAASQAhtml = ''.. sourceAASQA ..''
			 commandArray[indexArray] = {['UpdateDevice'] = dz_source..'|0|'.. tostring(sourceAASQAhtml)}
			 indexArray=indexArray+1
		  end	  
	end		

end
return commandArray

Laisser un commentaire