--[[
name : script_time_pollution.lua
auteur : papoo
version : 1.12
date : 17/05/2016
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 57)
sur domoticz et de nous alerter le cas écheant selon le niveau de notification choisi
L'indice de pollution remonte sur un widget Alert
]]--
-- ========================================================================
-- Variables à éditer
-- ========================================================================
local ville='ROANNE' --[[ 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 = nil -- renseigner l'id du device alert pollution associé si souhaité, sinon nil (dummy - alert)
local dz_indice_log = 53 -- renseigner l'id du device indice de pollution associé si souhaité, sinon nil (pour le log des données avec graphique)
local dz_o3 = 57 -- 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 = 54 -- renseigner l'id du device taux de particules associé si souhaité, sinon nil
local dz_so2 = 55 -- 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 = 4 -- 0: aucune notification, 1: toutes, 2: (2> Pollution <4), 3: (4> Pollution <6), 4: (6> Pollution <8), 5: (Pollution >8)
local debugging = false -- true pour voir les logs dans la console log Dz ou false pour ne pas les voir
-- ========================================================================
-- 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)
if (debugging) then
print ("<font color='#f3031d'>".. s .."</font>");
end
end
---------------------------------------------------------------------------
-- Fin Fonctions
---------------------------------------------------------------------------
commandArray = {}
now=os.date("*t")
if (now.min == 43 and ((now.hour == 7) or (now.hour == 13) or (now.hour == 19))) then -- 3 éxecutions du script par jour à 7H10, 13h10 et 18H10
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 wget = 'C:\\wget\\wget.exe '
--local fname ="C:\\pollution_air.xml"
-- os.execute("wget -q -O " .. fname .. "
http://www.lcsqa.org/surveillance/indic ... s/jour/xml")
-- fin methode wget
-- début methode curl
--local fname ="/tmp/pollution_air.xml"
--os.execute("curl
http://www.lcsqa.org/surveillance/indic ... s/jour/xml > ".. fname .." && chmod 777 " .. fname)
local curl = 'c:\\curl-7.50.3-win32-mingw\\bin\\curl.exe '
local fname = 'c:\\tmp\\pollution_air.xml'
os.execute(curl.."
http://www.lcsqa.org/surveillance/indic ... s/jour/xml > ".. 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[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 = '<a href="'.. sourceAASQA ..'">'.. sourceAASQA ..'</a>'
commandArray[indexArray] = {['UpdateDevice'] = dz_source..'|0|'.. tostring(sourceAASQAhtml)}
indexArray=indexArray+1
end
end
end
return commandArray