Is there any way to determine what time a light switch was turned on?
Sensors have a lastTrip timestamp but I don’t see any variable on my light switches for the time they were turned on or off.
I’m trying to implement a fail safe on my motion sensor timeout so that if the motion sensor fails for any reason, the lights don’t keep turning off every few minutes.
I’m using the code below as an occupancy sensor to turn the lights off if nobody is in the room.
It’s generally pretty reliable but I’ve had sensors get out of sync when they fail to transmit the state change due to bad reception or the sensor glitches and needs to be re-set.
In these cases the lights turn off every five minutes since the script returns true every time it runs and it turns the lights off again. Of course this really annoys my family.
I figure I could fail safe by comparing the time the light was turned on against the motion timeout. The lights would go out once, but if the light was turned again on AFTER the timeout then it must have been manually turned on again and I can set the script to return false.
Unfortunately, it doesn’t look like Vera keeps track of the last time a light was turned on, so any suggestions are appreciated.
I’m using this code running on a 5 minute schedule to turn the lights off. I want to abort if the light was turned back on after (lastTrip / 60) + period
Or any easy way for a family member to keep the lights from turning off every 5 minutes when the sensor is not being tripped.
Or just a better method of doing this that fails safe.
local deviceNo = 202 – (motion sensor device number)
local period = 20 – (time in minutes for no-motion to return true)
local SS_SID = “urn:micasaverde-com:serviceId:SecuritySensor1”
local tripped = luup.variable_get (SS_SID, “Tripped”, deviceNo) – Check sensor current state since the lastTrip timestamp is updated when tripped and when un-tripped.
if (tripped == “0”) then
local lastTrip = luup.variable_get (SS_SID, “LastTrip”, deviceNo) or os.time()
lastTrip = tonumber (lastTrip)
if ((os.difftime (os.time(), lastTrip) / 60) >= period) then
return true
end
end
return false
I’m trying to think of a way to use the lastTrip variable so that if the timestamp hasn’t changed since the last time we turned the light off we can abort.
This might work. I’ve implemented this and done some basic testing.
if ((os.difftime (os.time(), lastTrip) / 60) >= period) and ((os.difftime (os.time(), lastTrip) / 60) < (period+5)) then
This should only return true during a 5 minute window after the timeout period. Since I’m running on a 5 minute timer, this should only allow it to run once.
So far it’s working but there might be an issue with timing that could allow it to run twice.