Did some one come in or leave the house? script.

I want to know if some one “came in” or “left” the house. To do this I have a motion sensor in the room and a door sensor.
The idea is if the motion sensor was triggered before the door sensor, some one left the room and vice versa.
The problem is my script does not work and I dont know why. Some times its right some time its wrong.

– script
function who_is()
local SS_SID = “urn:micasaverde-com:serviceId:SecuritySensor1” – Security Sensor Service ID
local DEVICE_NO_SENS_P = 9 – Door sensor
local DEVICE_NO_SENS_E = 31 – Motion sensor

local sensor_door = luup.variable_get (SS_SID, “LastTrip”, DEVICE_NO_SENS_P) or os.time()
local sensor_motion = luup.variable_get (SS_SID, “LastTrip”, DEVICE_NO_SENS_E) or os.time()
sensor_door = tonumber (sensor_door)
sensor_motion = tonumber(sensor_motion)

if (os.difftime (sensor_motion, sensor_door) > 0) then
luup.call_action(“urn:richardgreen:serviceId:VeraAlert1”, “SendAlert”, {Message = “Some one left”, Recipients = “Mobil”}, 35)
else
luup.call_action(“urn:richardgreen:serviceId:VeraAlert1”, “SendAlert”, {Message = “Some one entered”, Recipients = “Mobil”}, 35)
end
end

luup.call_delay (“who_is”, 60)
return true

How is your script triggered?

Thanks - Joel

[quote=“idatum, post:2, topic:179753”]How is your script triggered?

Thanks - Joel[/quote]

Hey @idatum.

My trigger is the door sensor.

The trouble with SecuritySensor devices is that Vera sometimes updates Tripped and LastTrip variables even when the status has not changed. This will defeat your time-based logic.

You need the time when the Tripped status actually changed from 0 to 1. This is not so easy to accomplish in scene Lua. You could have two additional scenes that are triggered by the sensors and, with the aid of global variables, note the time when they changed state - and didn’t just re-trigger with the same state.

or you could just use PLEG which takes care of that for you.

@RexBeckett

Thanks for the advice. Is PLEG all powerfull! :slight_smile: Ive been avoiding it, but I guess Its inevitable, I see it mentioned everywhere.