Page 1 sur 1

script lua monitoring

Publié : 12 déc. 2019, 14:45
par miralant
Bonjour,

Auriez vous un modèle de script lua qui scanne par exemple les devices de température (ou autres) et notifie d'une alerte si pas de réponse ou mise à jour depuis un certains temps (ex 10 min température ou 1 j pour des capteurs). J'aimerai l'utiliser pour gérer les piles HS sur capteur PIR ou porte mais aussi sur mes sondes de température.

merci

Re: script lua monitoring

Publié : 13 déc. 2019, 07:55
par Chrominator

Re: script lua monitoring

Publié : 13 déc. 2019, 11:21
par AsKeY
Je ne sais plus ou j'avais trouvé ce script, j'avais quelques problème de fonctionnement. J'ai donc fait des modif et j'envoie les notif sur telegram.
J'ai laissé la notif par mail, mais je ne l'utilise pas ;)

Code : Tout sélectionner

--[[
Verion: 19061603

WHAT DOES THE SCRIPTS?:
It looks to the LAST SEEN date/time of a device. When threshold runs out, you get a notification to check the device.
Be sure you are adding devices that are updated in a normal situation. 
For example, devices connected to a Xiaomi gateway are not updatet, unless activated. When you add a smoke detector tru Xiaomi gateway, than the smoke detector has to give a smoke-alarm before the threshold runs out. Otherwise you get a notification. 
Advice for Xiaomi smoke-detectors is to use Zigbee2MQTT, this gateway does update the LAST SEEN date/time of Xiaomi smoke-detectors.

WHAT TO SETUP TO MAKE THIS SCRIPT WORKING
Fill in the local network setting for Domoticz.
Go to SETTINGS --> SETTINGS --> SYSTEM --> LOCAL NETWORK.
Fill in your IP adres. When it is local you could fill in 127.0.0.*

Make a dummy device named: opVakantie
Dummy device opVakantie changes the threshold timer for the devices-check
The idea is: opVakantie Device ON = longer threshold numbers, so you don't get notification on your holiday
And opVakantie Device Off = to have shorter threshold numbers.

Set telegram true is you want telegram notification, 
but therefor apiKEY and chatID has to be filled in SETTINGS --> SETTINGS --> NOTIFICATIONS --> TELEGAM

Set email true is you want to receive an e-mail, but therefor the e-mail has te be setup in SETTINGS --> SETTINGS --> E-MAIL
Fill in you e-mail address where to send to

Of course fill in the devices you would like to check.
You could choose to fill in with name or IDX number of the device.

There is a line which says: -- Don't change things undernead this line
If you have no idea how the scripts works, please lissen to this.
If you know how it works and have ideas, please post them on http://www.domoticz.com/forum/viewtopic.php?f=59&t=18566&start=20
]]--

local scriptVar = 'lastSeen'

return {
    --on = { timer =  {'every 30 minutes'}, httpResponses = { scriptVar }},
    on = { timer =  {'every minute'}, httpResponses = { scriptVar }},		-- Only use this one to test. When working disable it and activate "every 30 minutes"
    --on = { timer =  {'at 08:00', 'at 10:00'}, httpResponses = { scriptVar }},
    logging = { level   = domoticz.LOG_ERROR, marker  = scriptVar },
    
    data = { notified = { initial = {} } },
    
    execute = function(dz, item)
    
        --Telegram settings
            local telegram    = true                        -- Set true for sending telegram, set false for NOT sending telegram.
        
        --Email settings (domoticz email settings have to be filled in)
            local email       = false                        -- Set true for sending email, set false for NOT sending email.
            local mailaddress = 'Notification@domoticz.com' -- Fill in your e-mail address, which the notification needs to be send to.
        
        -- Global settings
            local notifyTime  = 'at 18:00'                  -- You can add multiple notification times, i.e. 'at 12:00, at 20:00'
	    local notifyHead  = 'Etat des capteurs :'            -- This is the start of everynotification, so you know it is this script.
	         
	    -- Devices to check NO holiday.
	    --                      Devices name or IDX number column   = Time column in seconds                 
	        local devices_No =  {
	                            ['NOMCAPTEUR1']        = 86400,  -- 1 Days
	                            ['NOMCAPTEUR2']        = 86400,  -- 1 Days
	                            }
	    
	    -- Devices to check when it is holiday.
	    --                      Devices name or IDX number column   = Time column in seconds
	        local devices_Yes = {
	                            ['NOMCAPTEUR1']        = 1209600,  -- 14 Days
                                    ['335']                             = 1209600,  -- 14 Days  IDX335=Dashboard-webpage-check
	                            }
	                          
-- Don't change things undernead this line
        local notified    = dz.data.notified -- short reference

        function Timers(sec)
            local ti
            sec=tonumber(sec)
                if sec == nil then ti='NIL'
                    elseif sec >= 86400   then ti=string.sub(tostring(sec/24/3600),1,4)..' days'  
                    elseif sec >= 3600    then ti=string.sub(tostring(sec/3600),1,4)..' hours' 
                    elseif sec >= 60      then ti=string.sub(tostring(sec/60),1,4)..' minutes'
                    else ti=sec..' seconds'
                end
                return ti 
            end    
                
        if not (item.isHTTPResponse) then
            dz.openURL({ 
                url = dz.settings['Domoticz url'] .. '/json.htm?type=devices&used=true',
                callback = scriptVar })
        else
            if dz.devices('opVakantie').state== 'On' then
                dz.log(notifyHead .." ".. 'Vakantie-On',dz.LOG_FORCE)
	            DevicesToCheck = devices_Yes
	        else
                dz.log(notifyHead .." ".. 'Vakantie-Off',dz.LOG_FORCE)
                DevicesToCheck = devices_No
            end
	
        local Time = require('Time')
            for _, node in pairs(item.json.result) do
                local toCheck = ( DevicesToCheck[node.idx] or DevicesToCheck[node.Name])
                if toCheck then
                    local lastUpdate = Time(node.LastUpdate).secondsAgo
                    local deviceName = (node.Name or node.idx)
                    local threshold = toCheck
                    	if lastUpdate < threshold then
              		        --device is alive
                            if notified[deviceName] == nil then
                                dz.log(notifyHead .." "..deviceName.." : "..Timers(lastUpdate).. ' was last response, threshold='..Timers(threshold),dz.LOG_FORCE)
                            else
                                dz.log(notifyHead .." ".. ' Signal back from sensor '..deviceName,dz.LOG_FORCE)
                                notified[deviceName] = nil  --remove from notified list
                                if telegram then
                                    dz.notify(notifyHead, notifyHead .." ".. 'Le capteur'..deviceName..'est de nouveau disponible',dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
                                end     
                                if email then
                                    dz.email('Domoticz device check', ''..notifyHead.. ' Signal back from sensor '..deviceName..'', ''..mailaddress..'')
                                end
                            end
                        else 
                            --lost signal:
                            if notified[deviceName] == nil then
                                dz.log (notifyHead .." ".. 'Le capteur '..deviceName .." n'est plus connecté depuis "..Timers(lastUpdate).. '.Batterie vide ?',dz.LOG_FORCE)
                                notified[deviceName] = 1
                                    if telegram then
                                        dz.notify(notifyHead,  notifyHead ..'Le capteur '..deviceName .." n'est plus connecté depuis "..Timers(lastUpdate).. '.Batterie vide ?',dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
                                    end     
                                    if email then
                                        dz.email('Domoticz device check', ''..notifyHead..'Le capteur '..deviceName .." n'est plus connecté depuis "..Timers(lastUpdate).. '.Batterie vide ?', ''..mailaddress..'')
                                    end
                        elseif
                            dz.time.matchesRule(notifyTime) and  
                            dz.devices('opVakantie').state == 'Off'
                        then
                            dz.log(notifyHead .." ".. 'Rappel : Le capteur '..deviceName .." n'est plus connecté depuis "..Timers(lastUpdate).. '.Batterie vide ?',dz.LOG_FORCE)
                            if telegram then
                                dz.notify(notifyHead, notifyHead .." ".. 'Rappel : Le capteur '..deviceName .." n'est plus connecté depuis "..Timers(lastUpdate).. '.Batterie vide ?',dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
                            end     
                            if email then
                                dz.email('Domoticz device check', ''..notifyHead..'Rappel : Le capteur '..deviceName .." n'est plus connecté depuis "..Timers(lastUpdate).. '.Batterie vide ?', ''..mailaddress..'')
                            end
                        end
                    end
                end
            end
        end
    end

}

--[[ 
Changes version 19061603
-Possible to fill in IDX number or device name to be checked.

Changes version 19061602
-Using dzVents: natively send a notification to telegram

Changes version 19061601
-WriteTime removed, this only gives NIL and wasn't used
-Script now works with url = dz.settings['Domoticz url'] 

Changes version 19061501
-E-mail option added
-With false/true you could actived of de-actived e-mail or telegram
-All settings on top of the script
]]--

Re: script lua monitoring

Publié : 13 déc. 2019, 11:33
par miralant
Chrominator a écrit : 13 déc. 2019, 07:55 De ce genre là ?

viewtopic.php?f=17&t=3490&p=31302&hilit ... ert#p31302
Alors l'état des piles, je dirai non. Dans l'absolu, j’aimerai plus avoir une alerte si des capteurs stratégiques ne me remontent plus de changement de statuts sur une durée fixée par type de capteur.

Re: script lua monitoring

Publié : 13 déc. 2019, 11:36
par miralant
AsKeY a écrit : 13 déc. 2019, 11:21 Je ne sais plus ou j'avais trouvé ce script, j'avais quelques problème de fonctionnement. J'ai donc fait des modif et j'envoie les notif sur telegram.
J'ai laissé la notif par mail, mais je ne l'utilise pas ;)

Code : Tout sélectionner

--[[
Verion: 19061603

WHAT DOES THE SCRIPTS?:
It looks to the LAST SEEN date/time of a device. When threshold runs out, you get a notification to check the device.
Be sure you are adding devices that are updated in a normal situation. 
For example, devices connected to a Xiaomi gateway are not updatet, unless activated. When you add a smoke detector tru Xiaomi gateway, than the smoke detector has to give a smoke-alarm before the threshold runs out. Otherwise you get a notification. 
Advice for Xiaomi smoke-detectors is to use Zigbee2MQTT, this gateway does update the LAST SEEN date/time of Xiaomi smoke-detectors.

WHAT TO SETUP TO MAKE THIS SCRIPT WORKING
Fill in the local network setting for Domoticz.
Go to SETTINGS --> SETTINGS --> SYSTEM --> LOCAL NETWORK.
Fill in your IP adres. When it is local you could fill in 127.0.0.*

Make a dummy device named: opVakantie
Dummy device opVakantie changes the threshold timer for the devices-check
The idea is: opVakantie Device ON = longer threshold numbers, so you don't get notification on your holiday
And opVakantie Device Off = to have shorter threshold numbers.

Set telegram true is you want telegram notification, 
but therefor apiKEY and chatID has to be filled in SETTINGS --> SETTINGS --> NOTIFICATIONS --> TELEGAM

Set email true is you want to receive an e-mail, but therefor the e-mail has te be setup in SETTINGS --> SETTINGS --> E-MAIL
Fill in you e-mail address where to send to

Of course fill in the devices you would like to check.
You could choose to fill in with name or IDX number of the device.

There is a line which says: -- Don't change things undernead this line
If you have no idea how the scripts works, please lissen to this.
If you know how it works and have ideas, please post them on http://www.domoticz.com/forum/viewtopic.php?f=59&t=18566&start=20
]]--

local scriptVar = 'lastSeen'

return {
    --on = { timer =  {'every 30 minutes'}, httpResponses = { scriptVar }},
    on = { timer =  {'every minute'}, httpResponses = { scriptVar }},		-- Only use this one to test. When working disable it and activate "every 30 minutes"
    --on = { timer =  {'at 08:00', 'at 10:00'}, httpResponses = { scriptVar }},
    logging = { level   = domoticz.LOG_ERROR, marker  = scriptVar },
    
    data = { notified = { initial = {} } },
    
    execute = function(dz, item)
    
        --Telegram settings
            local telegram    = true                        -- Set true for sending telegram, set false for NOT sending telegram.
        
        --Email settings (domoticz email settings have to be filled in)
            local email       = false                        -- Set true for sending email, set false for NOT sending email.
            local mailaddress = 'Notification@domoticz.com' -- Fill in your e-mail address, which the notification needs to be send to.
        
        -- Global settings
            local notifyTime  = 'at 18:00'                  -- You can add multiple notification times, i.e. 'at 12:00, at 20:00'
	    local notifyHead  = 'Etat des capteurs :'            -- This is the start of everynotification, so you know it is this script.
	         
	    -- Devices to check NO holiday.
	    --                      Devices name or IDX number column   = Time column in seconds                 
	        local devices_No =  {
	                            ['NOMCAPTEUR1']        = 86400,  -- 1 Days
	                            ['NOMCAPTEUR2']        = 86400,  -- 1 Days
	                            }
	    
	    -- Devices to check when it is holiday.
	    --                      Devices name or IDX number column   = Time column in seconds
	        local devices_Yes = {
	                            ['NOMCAPTEUR1']        = 1209600,  -- 14 Days
                                    ['335']                             = 1209600,  -- 14 Days  IDX335=Dashboard-webpage-check
	                            }
	                          
-- Don't change things undernead this line
        local notified    = dz.data.notified -- short reference

        function Timers(sec)
            local ti
            sec=tonumber(sec)
                if sec == nil then ti='NIL'
                    elseif sec >= 86400   then ti=string.sub(tostring(sec/24/3600),1,4)..' days'  
                    elseif sec >= 3600    then ti=string.sub(tostring(sec/3600),1,4)..' hours' 
                    elseif sec >= 60      then ti=string.sub(tostring(sec/60),1,4)..' minutes'
                    else ti=sec..' seconds'
                end
                return ti 
            end    
                
        if not (item.isHTTPResponse) then
            dz.openURL({ 
                url = dz.settings['Domoticz url'] .. '/json.htm?type=devices&used=true',
                callback = scriptVar })
        else
            if dz.devices('opVakantie').state== 'On' then
                dz.log(notifyHead .." ".. 'Vakantie-On',dz.LOG_FORCE)
	            DevicesToCheck = devices_Yes
	        else
                dz.log(notifyHead .." ".. 'Vakantie-Off',dz.LOG_FORCE)
                DevicesToCheck = devices_No
            end
	
        local Time = require('Time')
            for _, node in pairs(item.json.result) do
                local toCheck = ( DevicesToCheck[node.idx] or DevicesToCheck[node.Name])
                if toCheck then
                    local lastUpdate = Time(node.LastUpdate).secondsAgo
                    local deviceName = (node.Name or node.idx)
                    local threshold = toCheck
                    	if lastUpdate < threshold then
              		        --device is alive
                            if notified[deviceName] == nil then
                                dz.log(notifyHead .." "..deviceName.." : "..Timers(lastUpdate).. ' was last response, threshold='..Timers(threshold),dz.LOG_FORCE)
                            else
                                dz.log(notifyHead .." ".. ' Signal back from sensor '..deviceName,dz.LOG_FORCE)
                                notified[deviceName] = nil  --remove from notified list
                                if telegram then
                                    dz.notify(notifyHead, notifyHead .." ".. 'Le capteur'..deviceName..'est de nouveau disponible',dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
                                end     
                                if email then
                                    dz.email('Domoticz device check', ''..notifyHead.. ' Signal back from sensor '..deviceName..'', ''..mailaddress..'')
                                end
                            end
                        else 
                            --lost signal:
                            if notified[deviceName] == nil then
                                dz.log (notifyHead .." ".. 'Le capteur '..deviceName .." n'est plus connecté depuis "..Timers(lastUpdate).. '.Batterie vide ?',dz.LOG_FORCE)
                                notified[deviceName] = 1
                                    if telegram then
                                        dz.notify(notifyHead,  notifyHead ..'Le capteur '..deviceName .." n'est plus connecté depuis "..Timers(lastUpdate).. '.Batterie vide ?',dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
                                    end     
                                    if email then
                                        dz.email('Domoticz device check', ''..notifyHead..'Le capteur '..deviceName .." n'est plus connecté depuis "..Timers(lastUpdate).. '.Batterie vide ?', ''..mailaddress..'')
                                    end
                        elseif
                            dz.time.matchesRule(notifyTime) and  
                            dz.devices('opVakantie').state == 'Off'
                        then
                            dz.log(notifyHead .." ".. 'Rappel : Le capteur '..deviceName .." n'est plus connecté depuis "..Timers(lastUpdate).. '.Batterie vide ?',dz.LOG_FORCE)
                            if telegram then
                                dz.notify(notifyHead, notifyHead .." ".. 'Rappel : Le capteur '..deviceName .." n'est plus connecté depuis "..Timers(lastUpdate).. '.Batterie vide ?',dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
                            end     
                            if email then
                                dz.email('Domoticz device check', ''..notifyHead..'Rappel : Le capteur '..deviceName .." n'est plus connecté depuis "..Timers(lastUpdate).. '.Batterie vide ?', ''..mailaddress..'')
                            end
                        end
                    end
                end
            end
        end
    end

}

--[[ 
Changes version 19061603
-Possible to fill in IDX number or device name to be checked.

Changes version 19061602
-Using dzVents: natively send a notification to telegram

Changes version 19061601
-WriteTime removed, this only gives NIL and wasn't used
-Script now works with url = dz.settings['Domoticz url'] 

Changes version 19061501
-E-mail option added
-With false/true you could actived of de-actived e-mail or telegram
-All settings on top of the script
]]--
ça me semble pas mal ça !!!!! je regarde ça :)

Re: script lua monitoring

Publié : 13 déc. 2019, 17:38
par denis_brasseur

Re: script lua monitoring

Publié : 13 déc. 2019, 20:25
par Kyrille
denis_brasseur a écrit :Regarde ceci, script de papoo :!:

[DZVents] Script de notifications ultime (mais pas que)
Le lien ne fonctionne pas j'ai l'impression.

Envoyé depuis mon téléphone en utilisant Tapatalk


Re: script lua monitoring

Publié : 19 déc. 2019, 15:05
par miralant
denis_brasseur a écrit : 13 déc. 2019, 17:38 Regarde ceci, script de papoo :!:

[DZVents] Script de notifications ultime (mais pas que)
J'aime bien le principe de ce script mais je ne vois pas d'exemple avec le type de déclencheur que je souhaite.

ex : si pas de réponse ou mise à jour depuis X min > Email

Re: script lua monitoring

Publié : 20 déc. 2019, 05:53
par denis_brasseur
Tu peux tester avec

Code : Tout sélectionner

Example 7 : be notified if device is out since x minutes
    with notifications frequency in minutes and quiet hours notification
Exemple 7 : être averti si le périphérique est hors service depuis x minutes
    avec fréquence de notifications en minutes et notification des heures calmes

 {
"timeout_notification": 1440,
"frequency_notifications": 60,
"quiet_hours":"23:00-07:15"
  }
Mais il est vrai que le script utilise des notifications, pas d'envoi de mail.

Il y a également ceci
détection changement d'état