Allthough it's a mix of Dutch and English, I'm sure you will be able to understand it.
Functions:
1)check every 5 minutes if last signal time exceeds limit
2) time limit per sensor adjustable (some sensors update every minute, others once a day); simply adjust the table in the code
3) special 'On holiday' modus . Make sure you create a string variable 'opVakantie' with values 'ja' or 'nee'
4) receive a notification if a sensor passes out. (just once, no mail bomb as seen in other CheckSensor scripts)
5) receive a reminder once a day of passed out sensors. Just to make sure you don't forget to put in new batteries
6) receive a notification when the sensor is up again
Log example:
2017-07-27 19:00:00.528 dzVents: Info: ------ Start internal script: dzCheckSensors:, trigger: every 5 minutes
2017-07-27 19:00:00.533 dzVents: Info: dzCheckSensors: start
2017-07-27 19:00:00.719 dzVents: Info: dzCheckSensors: Device PIR_woonkamer is 9.75 uren niet gezien. Batterij leeg?
2017-07-27 19:00:00.733 dzVents: Info: dzCheckSensors: OKAY: Stroom: 2 seconden geen reactie, grenswaarde=5 minuten
2017-07-27 19:00:00.740 dzVents: Info: dzCheckSensors: OKAY: badkamer thermo hygro sensor: 26 seconden geen reactie, grenswaarde=5 minuten
2017-07-27 19:00:00.760 dzVents: Info: dzCheckSensors: OKAY: regenmeter: 29 seconden geen reactie, grenswaarde=5 minuten
2017-07-27 19:00:00.767 dzVents: Info: ------ Finished dzCheckSensors
Update version 3, 2019-01-19
Code: Select all
--version 3
--http://www.domoticz.com/forum/viewtopic.php?f=23&t=18566
--thanks to dannybloe
return {
active = true,
logging = {
level = domoticz.LOG_DEBUG, -- comment to use the dzVents global logging setting
marker = 'dzChecksensors'
},
on = {
timer = {'every 15 minutes'}
--,devices = {'triggerhulp'}
},
data = {
notified = {initial={}}
},
execute = function(dz)
function schrijfTijd(secs)
local t
secs=tonumber(secs)
if secs == nil then t='NIL'
elseif secs >= 259200 then t=string.sub(tostring(secs/24/3600),1,4)..' days'
elseif secs >= 10800 then t=string.sub(tostring(secs/3600),1,4)..' hours' -- meer dan 3 uur?
elseif secs >= 180 then t=string.sub(tostring(secs/60),1,4)..' minutes'
else t=secs..' seconds'
end
return t
end
local notifyTime='at 18:30' --you can add multiple notification times, i.e. 'at 12:00, at 20:00'
local notifyHead='dzCheckSensors3'
local notifyMessageType=30 --log + pushover
local notified = dz.data.notified -- short reference
meld ('start', 99)
if dz.devices('opVakantie').level==30 then
-- timeout for each device, in secondsAgo (during holiday)
--holiday
toCheck = { -- timeout for each device, in seconde.
['PIR_woonkamer'] = 1209600, -- 2 weken
['PIR_keuken']=1206600, -- 2 weken
['buitentemperatuur'] = 14400, -- 4 uur
['Stroom'] = 300, -- 5 minuten
['schemerschakelaar'] = 172800, -- 2dagen
['LUX'] = 54000 -- 15 uur
}
else
toCheck = {
-- timeout for each device, in secondsAgo (no holiday)
['PIR_woonkamer'] =172800, -- 2 dagen
['PIR badkamer'] =172800, -- 2 dagen
['pirTuin'] =340000, -- ca 3 dagen
['PIR_garage'] =172800, -- 2 dagen
--['PIR_zolder'] =172800, -- 2 dagen
['PIR_keuken'] =172800, -- 2 dagen
['buitentemperatuur'] = 3600, -- 1 uur
['schemerschakelaar'] = 172800, -- 2dagen
['LUX'] = 54000, -- 15 uur
['LUXtuin'] = 54000, -- 15 uur
['keukenLux'] = 54000, -- 15 uur
['Woonkamer Temperature'] = 7200 --2 uur; iets ruimer gezet, want sensor geeft geen update bij constante temperatuur
}
end
--purge notified table, in case there was a switch from holiday variable to a "toCheck" table with less entrie
for deviceName, lastUpdate in pairs(notified) do
if toCheck[deviceName]==nil then
notified[deviceName]=nil
meld (deviceName.. ' was removed from the notified table, '..schrijfTijd(treshold),99)
dz.helpers.managedNotify(dz, notifyHead, deviceName..' was removed from the notified table, ', 0, 2, 0) --write to LOG
end
end
--the main routine:
for deviceName, treshold in pairs(toCheck) do
local lastUpdate = dz.devices(deviceName).lastUpdate.secondsAgo
if lastUpdate < treshold then
--device is alive
if notified[deviceName] == nil then
dz.helpers.managedNotify(dz, notifyHead,deviceName ..": "..schrijfTijd(lastUpdate).. ' no response, treshold='..schrijfTijd(treshold)..'--> ok', 0, 2, 0) --write to LOG:
else
dz.helpers.managedNotify(dz, notifyHead,'Signal back from sensor'..deviceName, 0, notifyMessageType, 0)
notified[deviceName] = nil --remove from notified list
end
--'opVakantie': 0=werk / 10 = vrij thuis, laat wakker / 20=vrij thuis, vroeg wakker/ 30=op vakantie / 40=babysit
else --lost signal:
if
notified[deviceName] == nil
then
--immediate notification after signal lost:
dz.helpers.managedNotify(dz, notifyHead,'Signal lost: '..deviceName .."sent "..schrijfTijd(lastUpdate).. ' the last signal, treshold='..schrijfTijd(treshold)..'. Battery empty?', 0, notifyMessageType, 1)
elseif
--sent reminders on daily basis:
dz.time.matchesRule(notifyTime) and --time slot to notify
dz.devices('opVakantie').level~=30 -- not on holiday
then
dz.helpers.managedNotify(dz, notifyHead,'Reminder, '..deviceName .." sent "..schrijfTijd(lastUpdate).. ' the last signal. Battery empty?', 0, notifyMessageType, 1)
end
end
end
end
}
Code: Select all
managedNotify = function (dz, header, message, prio, messageType, muteTimeMin)
--thanks to Waaren, https://www.domoticz.com/forum/viewtopic.php?f=59&t=26542&p=203675#p203630
--messageType 2=log, 3=pushover, 5=speak, 7=siren; multiply values for combinations i.e. 15=pushover and speak
local now = os.time(os.date('*t')) -- seconds since 1/1/1970 00:00:01
if ( not dz.globalData.managedNotifications[message] ) or
(( dz.globalData.managedNotifications[message] + muteTimeMin * 60 ) <= now )
then
if messageType % 2 == 0 then dz.log(message ,dz.LOG_FORCE) end
if messageType % 3 == 0 then dz.notify(header..'#'..message..'#'..prio) end
if messageType % 5 == 0 then
dz.openURL ('http://192.168.0.xxx:xxxx/jarvis?jKey=s829kuifd77tpe35ab&action=execute&type=TTS&data={"text":"'..dz.utils.urlEncode(message,'%20')..'","volume":"50"}')
end
if messageType % 7 == 0 then domoticz.devices('Sirene').switchOn().forSec(3) end
--dz.notify("managedNotification", message,nil,nil,nil,messageType)
dz.globalData.managedNotifications[message] = now
end
end,