I was looking for a way to add a delayed off action when I turn on a light, which is pretty simple, but I also wanted to be able to tap on multiple times and add time to the dealyed off.
I use this in conjunction with and sometime instead of motion sensors. ie, the bathroom, the kids always have problems of the light turing off on them when they are in the shower, but I didn't just want to delay the off time, now they can hit the button twice.
I use this in conjunction with and sometime instead of motion sensors. ie, the bathroom, the kids always have problems of the light turing off on them when they are in the shower, but I didn't just want to delay the off time, now they can hit the button twice.
Code:
'call this script with optional parameters of ("PushOn"|"PushOff",device code ie "A11") 'I set this script to run from 2 seperate events: ' - one with a trigger of Device Set to On and parameters of ("PushOn",device code) ' - the second with a trigger of Device Set to Off and parameters of ("PushOff",device code) dim speak = True 'when true Speak an announcement that the device was turned off Sub main (parm as object) end sub Sub PushOn (ByVal dv as string) dim oldEvent dim newEvent dim nn = 900 'seconds to add for delay off, 900 seconds equals 15 minutes dim ev dim eventTime dim dvRef dim dvReturn dim dvName dvRef = hs.GetDeviceRef(dv) oldEvent=hs.GetEventRefByName("DelayOffEvent " & dv) if oldEvent<0 then 'event does not exist, create a new one eventTime = DateAdd ("s", nn, Now) newEvent=hs.NewEventGetRef("DelayOffEvent " & dv) ev=hs.GetEventByRef(newEvent) ev.ev_abs_time = 0 'Time ev.ev_time = eventTime ev.misc = &h10 '&h10 is delete after trigger &h20 1 is disabled hs.AddAction(newEvent, 1, dvRef, "Off",,,) if speak=True then dvRef=hs.GetDeviceRef(dv) dvReturn=hs.GetDeviceByRef(dvRef) dvName = dvReturn.Location2 & " " & dvReturn.Location & " " & dvReturn.name hs.AddAction(newEvent, 3, "Turning off " & dvName,,) end if hs.writelog("DelayOffEvent","Creating delayed off for " & dv & " in " & nn & " seconds.") else 'event does exist, add nn seconds to it ev=hs.GetEventByRef(oldEvent) eventTime = DateAdd("s", nn, ev.ev_time) ev.ev_time = eventTime hs.writelog("DelayOffEvent","Adding " & nn & " seconds to delayed off event for " & dv & ".") end if End Sub Sub PushOff (ByVal dv as string) dim oldEvent dim dvRef dim dvReturn dim dvName oldEvent=hs.GetEventRefByName("DelayOffEvent " & dv) if oldEvent>0 then 'event does exist, let's delete it hs.DeleteEvent("DelayOffEvent " & dv) hs.writelog("DelayOffEvent","Cancelling delayed off for " & dv & ".") end if End Sub