[LUA] Qualité de l’air en province et à Paris
Publié : 15 mai 2016, 18:36
basé sur l'article http://easydomoticz.com/domoticz-integr ... -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 63)
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.
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
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 63)
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.
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
Code : Tout sélectionner
--[[
name : script_time_pollution.lua
auteur : papoo
version : 1.14
Création : 17/05/2016
Mise à jour : 22/11/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 : http://pon.fr/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'idx du device alert pollution associé si souhaité, sinon nil (dummy - alert)
local dz_indice_log = 724 -- renseigner l'idx du device indice de pollution associé si souhaité, sinon nil (pour le log des données avec graphique)
local dz_o3 = nil -- renseigner l'idx du device Ozone associé si souhaité, sinon nil
local dz_no2 = nil -- renseigner l'idx du device dioxyde d'azote associé si souhaité, sinon nil
local dz_PM10 = nil -- renseigner l'idx du device taux de particules associé si souhaité, sinon nil
local dz_so2 = nil -- renseigner l'idx du device dioxyde de soufre associé si souhaité, sinon nil
local dz_com = nil -- renseigner l'idx du device texte commentaire associé si souhaité, sinon nil
local dz_source = nil -- renseigner l'idx 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
--------------------------------------------
---------------- 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 TronquerTexte(texte, nb) -- texte à tronquer, Nb maximum de caractère
local sep ="[;!?.]"
local DernierIndex = nil
texte = string.sub(texte, 1, nb)
local p = string.find(texte, sep, 1)
DernierIndex = p
while p do
p = string.find(texte, sep, p + 1)
if p then
DernierIndex = p
end
end
return(string.sub(texte, 1, DernierIndex))
end
--------------------------------------------
-------------- Fin Fonctions ---------------
--------------------------------------------
commandArray = {}
time=os.date("*t")
--if (time.min == 10 and ((time.hour == 8) or (time.hour == 15) or (time.hour == 20))) then -- 3 éxecutions du script par jour à 8H10, 15h10 et 20H10
if (time.min == 10 and time.hour == 20) then -- 1 éxecution du script par jour à 21H10
--if time.min % 2 == 0 then -- execution toutes les 2 minutes
voir_les_logs("script_time_pollution.lua",debugging)
-- deux methodes pour récuperer les informations xml, WGET ou CURL si vous décommentez la methode curl pensez à commenter la methode 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("<node>(.-)</node>") do
agglomeration=instance:match('<agglomeration>(.-)</agglomeration>')
if agglomeration == ville then
dateIndice=instance:match('<dateIndice>(.-)</dateIndice>')
CodeInsee=instance:match('<agglomerationCodeInsee>(.-)</agglomerationCodeInsee>')
valeurIndice=instance:match('<valeurIndice>(.-)</valeurIndice>')
SousIndiceO3=instance:match('<SousIndiceO3>(.-)</SousIndiceO3>')
SousIndiceNO2=instance:match('<SousIndiceNO2>(.-)</SousIndiceNO2>')
SousIndicePM10=instance:match('<SousIndicePM10>(.-)</SousIndicePM10>')
SousIndiceSO2=instance:match('<SousIndiceSO2>(.-)</SousIndiceSO2>')
Commentaire=instance:match('<Commentaire>(.-)</Commentaire>')
sourceAASQA=instance:match('<sourceAASQA>(.-)</sourceAASQA>')
end
end
if valeurIndice == nil and dz_indice_alert ~= nil then
commandArray[#commandArray+1] = {['UpdateDevice'] = dz_indice_alert..'|0|Aucune donnée disponible'}
voir_les_logs("--- --- --- Aucune donnée disponible pour la ville : ".. ville,debugging)
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[#commandArray+1] = {['UpdateDevice'] = dz_indice_alert..'|1|Pas de Pollution'}
end
if send_notification > 0 and send_notification < 2 then
commandArray[#commandArray+1] = {['SendNotification'] = 'Alerte Pollution#Pas de Pollution!'}
end
voir_les_logs("--- --- --- Pas de pollution --- --- ---",debugging)
elseif tonumber(valeurIndice) <= 6 then -- niveau 3
if dz_indice_alert ~= nil then
commandArray[#commandArray+1] = {['UpdateDevice'] = dz_indice_alert..'|2|Polution Faible'}
end
if send_notification > 0 and send_notification < 3 then
commandArray[#commandArray+1] = {['SendNotification'] = 'Alerte Pollution#Pollution Faible!'}
end
voir_les_logs("--- --- --- Pollution Faible --- --- ---",debugging)
elseif tonumber(valeurIndice) <= 8 then -- niveau 4
if dz_indice_alert ~= nil then
commandArray[#commandArray+1] = {['UpdateDevice'] = dz_indice_alert..'|3|Pollution Forte'}
end
if send_notification > 0 and send_notification < 4 then
commandArray[#commandArray+1] = {['SendNotification'] = 'Alerte Pollution#Pollution Forte!'}
end
voir_les_logs("--- --- --- Pollution Forte --- --- ---",debugging)
elseif tonumber(valeurIndice) > 8 then -- niveau 5
if dz_indice_alert ~= nil then
commandArray[#commandArray+1] = {['UpdateDevice'] = dz_indice_alert..'|4|Pollution trés forte'}
end
if send_notification > 0 and send_notification < 5 then
commandArray[#commandArray+1] = {['SendNotification'] = 'Alerte Pollution#Pollution trés forte!'}
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[#commandArray+1] = {['UpdateDevice'] = dz_indice_log..'|0|'.. tostring(valeurIndice)}
end
if dz_no2 ~= nil then -- Mise à jour du devise NO3 si il existe
commandArray[#commandArray+1] = {['UpdateDevice'] = dz_no2..'|0|'.. tostring(SousIndiceNO2)}
end
if dz_PM10 ~= nil then -- Mise à jour du devise PM10 si il existe
commandArray[#commandArray+1] = {['UpdateDevice'] = dz_PM10..'|0|'.. tostring(SousIndicePM10)}
end
if dz_so2 ~= nil then -- Mise à jour du devise SO2 si il existe
commandArray[#commandArray+1] = {['UpdateDevice'] = dz_so2..'|0|'.. tostring(SousIndiceSO2)}
end
if dz_com ~= nil then -- Mise à jour du devise Commentaire si il existe
commandArray[#commandArray+1] = {['UpdateDevice'] = dz_com..'|0|'.. tostring(TronquerTexte(Commentaire,240))}
end
if dz_source ~= nil then -- Mise à jour du devise Source si il existe
sourceAASQAhtml = '<a href="'.. sourceAASQA ..'">'.. sourceAASQA ..'</a>'
commandArray[#commandArray+1] = {['UpdateDevice'] = dz_source..'|0|'.. tostring(sourceAASQAhtml)}
end
end
end
return commandArray