Page 1 of 1
Check if switch has been on for x seconds
Posted: Wednesday 21 February 2018 21:54
by tapaz
Hello,
is there a way to check if a switch has been on for over 30 seconds and after that send a message? Sounds pretty simple but I can't figure it out

I am trying to alert if a door was left open for too long. Of course, if the door is shut before 30 seconds, no alert should be sent.
Re: Check if switch has been on for x seconds
Posted: Wednesday 21 February 2018 22:05
by htilburgs
I use this for checking if frontdoor (and same for backdoor) is open longer than 5 minutes.
After 5 minutes it sends a message every minute for 5 minutes and write a message to the Domoticz log.
difference > 300 : check after 5 minutes
difference < 600 : check until 10 minutes open (you get a message after 5 minutes. Repeats every minute until 10 minutes)
change these intervals to you're own settings
Code: Select all
-- script_time_dooropen.lua
t1 = os.time()
s = otherdevices_lastupdate['Sensor - Voordeur']
-- returns a date time like 2013-07-11 17:23:12
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
commandArray = {}
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))
if (otherdevices['Sensor - Voordeur'] == 'Open' and difference > 300 and difference < 600) then
commandArray['SendNotification']='Voordeur melding#Voordeur staat langer dan 5 minuten open!'
print("Voordeur open")
end
return commandArray
Re: Check if switch has been on for x seconds
Posted: Wednesday 21 February 2018 22:14
by tapaz
Many thanks htilburgs! So it seems I have to do it with lua, no problem. I was just wondering if it's possible to do with blockly at all, maybe not

Re: Check if switch has been on for x seconds
Posted: Wednesday 21 February 2018 23:25
by htilburgs
In blockly it’s not so easy to use the time difference, so doing it in lua ( or dzVents) is more easy.
Re: Check if switch has been on for x seconds
Posted: Tuesday 27 November 2018 22:39
by astrapowerrr
hi,
im looking for some solutions here.
i copied and edit you lua to this below
but i cant get i to work.
this is my first time working with a script so i hope i can fix this with some help.
what i want to achieve is that when my tapwater switch is active(on) longer then 5 minutes it sets my dummy switch "douche actief"to on.
if tapwater is off before 5 minutes then do nothing...
the next step will be that when tapwater is off i also want to set the switch"douche actief"to off(but that i can do with blocky)
-- script_tapwater douche actief.lua
t1 = os.time()
s = otherdevices_lastupdate['Tapwater']
-- returns a date time like 2013-07-11 17:23:12
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
commandArray = {}
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))
if (otherdevices['Tapwater'] == 'on' and difference > 300 and difference < 600) then
commandArray['Douche actief']='on'
end
return commandArray
i hope someone can help me with this.....
greets marco