Turn On lights if movement and turn off if dont detect movement for a while

Hi, newbie here ;D

I am trying to configure this scene to turn on the lights and TV when it detects movement and turn off the lights and TV when it doesn’t detect movement for a while.

Im using this luup code

local sensorDeviceNo = 23 – Motion Sensor device number
local lCacifos = 12 – Luzes Cacifos device number
local tTv = 11 – Tomada TV device number
local period = 300 – Seconds

local SS_SID = “urn:micasaverde-com:serviceId:SecuritySensor1” – Security Sensor Service ID
local SP_SID = “urn:upnp-org:serviceId:SwitchPower1” – Switch Power Service ID

function checkLastTrip()
local lastTrip = luup.variable_get (SS_SID, “LastTrip”, sensorDeviceNo) or os.time()
if (os.difftime (os.time(), tonumber (lastTrip)) >= period) then
luup.call_action (SP_SID, “SetTarget”, {[“newTargetValue”] = 0}, lCacifos) – Turn off Luzes Cacifos.
luup.call_action (SP_SID, “SetTarget”, {[“newTargetValue”] = 0}, tTv) – Turn off Tomada TV.
else
luup.call_delay (“checkLastTrip”, period) – Check when the sensor was last tripped every seconds.
end
end

luup.call_delay (“checkLastTrip”, period)

return true

The problem is while the sensor its detecting movement the lights and the tv keep turning off and they don’t turn on after that.

What i am doing wrong?

Thanks in advance :wink:

Hi @sillywalks,

I recognized that from the wiki Scripts for Scene examples. I also tried that code but it didn’t work correctly for me either.

The simplest way I’ve found with UI7 is to set up two scenes:

[ol][li]Motion On: Whenever {Device} detects motion whether is armed or disarmed → turn light/tv on[/li]
[li]Motion Off: Whenever {Device} stops detecting motion whether is armed or disarmed → turn light/tv off[/li][/ol]

The behavior I noticed from my motion sensors is that the “LastTrip” timestamp will update when motion has been detected AND when motion has stopped being detected (after some internal time ~ 4 mins).

When motion has been first detected, the variable “Tripped” will be “1” and the “LastTrip” timestamp will be now. If any motion has continued to be detected within the 4 min window, “Tripped” will remain at “1”, the “LastTrip” timestamp will not update, and the 4 min window will be extended.

When no motion has been detected at all during the 4 min window, then “Tripped” will be “0” and the “LastTrip” timestamp will be updated to the time when the 4 min window expired.

If you’re set on using the lua code for this instead of the UI7 scene wizard, you might need to factor in the “Tripped” variable. Also, it might be a good idea to ensure the scene only runs if the call_delay recursion is not active, otherwise you might trigger multiple timers with undesirable effects. Some kind of variable lock maybe, like:

-- first thing at the top: bail out of this scene early if already running
if _G.myMotionSceneActive then return false end
-- set the lock
_G.myMotionSceneActive = true

-- ... then when checkLastTrip() completes and turns off the lights/tv, reset the lock:
    _G.myMotionSceneActive = nil

Hope this helps

Here?s some working luup that I have in my startup for turning on and off an upstairs light registered on a remote Vera when motion is seen after sunset. You should be able to add your TV as part of that process.

However I would personally be reluctant to risk turning my tv off if I was just sitting still, so maybe consider something else or additional as the turn off trigger.

[code] local period = 60 – Seconds
local motion = 354 – motion sensor device ID
local daynight = 204 – daynight sensor device ID
local daynightstatus = luup.variable_get(“urn:rts-services-com:serviceId:DayTime”,“Status”,daynight)
local status1, power = luup.inet.wget(“http://192.168.1.183:3480/data_request?id=variableget&DeviceNum=90&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status”)
– print (status)
– print (power)

function checkLastTrip()
local lastTrip = luup.variable_get (“urn:micasaverde-com:serviceId:SecuritySensor1”, “LastTrip”, motion) or os.time()
if (os.difftime (os.time(), tonumber (lastTrip)) >= period) then
luup.inet.wget(“http://192.168.1.183:3480/data_request?id=lu_action&DeviceNum=90&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0”)
else
luup.call_delay (“checkLastTrip”, period) – Check when the sensor was last tripped every seconds.
end
end

function turn_remote_light_on_with_motion()
local daynightstatus = luup.variable_get(“urn:rts-services-com:serviceId:DayTime”,“Status”,daynight)
local status1, power = luup.inet.wget(“http://192.168.1.183:3480/data_request?id=variableget&DeviceNum=90&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status”)

-- first check if the light is already on 
if power == "1" then   -- the light is already on, so instead set up a call back to monitor it so it can be turned off
    luup.call_delay ("checkLastTrip", period) 
    luup.log ("light is already on")
elseif daynightstatus == "0" then -- it's night time so the light can be turned on, and set up a call back to monitor it so it can be turned off.
    luup.inet.wget("http://192.168.1.183:3480/data_request?id=lu_action&DeviceNum=90&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1")
    luup.call_delay ("checkLastTrip", period)
    luup.log ("light is not on, and it is night time so you can turn it on")
elseif daynightstatus == "1" then -- check if it's night time to know if the light needs to be on.
    luup.log ("Its daytime so no need to turn it on")
    luup.log ("" .. period)

end
end

luup.variable_watch(“turn_remote_light_on_with_motion”, “urn:micasaverde-com:serviceId:SecuritySensor1”, “Tripped”, motion)[/code]

Hello-

i think i’m running into the same issue as mentioned above where my last trip is not updating. Is there any other alternative to running 2 separate scenes? I was doing that previously and was still encountering issues where the lights would turn off on me. Here’s the code i have gathered so far:

local sensorDeviceNo = 80
local lightDeviceNo = 24
local period = 180

local SS_SID = “urn:micasaverde-com:serviceId:SecuritySensor1” – Security Sensor Service ID
local SP_SID = “urn:upnp-org:serviceId:SwitchPower1” – Switch Power Service ID

function checkLastTrip()
local lastTrip = luup.variable_get (SS_SID, “LastTrip”, sensorDeviceNo) or os.time()
if (os.difftime (os.time(), tonumber (lastTrip)) >= period) then
luup.call_action (SP_SID, “SetTarget”, {[“newTargetValue”] = 0}, lightDeviceNo) – Turn off the light.
else
luup.call_delay (“checkLastTrip”, period) – Check when the sensor was last tripped every seconds.
end
end

luup.call_delay (“checkLastTrip”, period)

return true

Run one PLEG

[quote=“ziggy5875, post:4, topic:197404”]Hello-

i think i’m running into the same issue as mentioned above where my last trip is not updating. Is there any other alternative to running 2 separate scenes? I was doing that previously and was still encountering issues where the lights would turn off on me. Here’s the code i have gathered so far:[/quote]

I would suggest using this: It basically unsures that the sensor is also untripped before turning the light off.

[code]local sensorDeviceNo = 80
local lightDeviceNo = 24
local period = 180

local SS_SID = “urn:micasaverde-com:serviceId:SecuritySensor1” – Security Sensor Service ID
local SP_SID = “urn:upnp-org:serviceId:SwitchPower1” – Switch Power Service ID

function checkLastTrip()
local lastTrip = luup.variable_get (SS_SID, “LastTrip”, sensorDeviceNo) or os.time()
local senstat = luup.variable_get (SS_SID, “Tripped”, sensorDeviceNo)
if (os.difftime (os.time(), tonumber (lastTrip)) >= period) and senstat ~= “1” then
luup.call_action (SP_SID, “SetTarget”, {[“newTargetValue”] = 0}, lightDeviceNo) – Turn off the light.
else
luup.call_delay (“checkLastTrip”, period) – Check when the sensor was last tripped every seconds.
end
end

luup.call_delay (“checkLastTrip”, period)[/code]

have a look at Plugin: DelayLight
http://forum.micasaverde.com/index.php/topic,60498.0.html

It’s been working good for me with a camera detection and Blue Iris API…

– DETECTiION MOVEMENT OUTSIDE FROM MY CAMERA

local device1 = 319 – Light
local device2 = 330 – Detect.

local dayNo = 9
local delay = 120
local delay1 = 5

function checkmotion()

local itsday = luup.variable_get(“urn:rts-services-com:serviceId:DayTime”,“Status”, dayNo) – Night Time

if(itsday == “0”)then

luup.call_action(“urn:micasaverde-com:serviceId:SecuritySensor1”, “SetArmed”, {newArmedValue = “0”},device2)

luup.call_delay( ‘switch_on’, delay1) – Call the switch on function after a delay of x seconds
function switch_on()

luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“1” },device1)
luup.call_action(“urn:dcineco-com:serviceId:MiLightRGBW1”,“SetWhiteMode”,1,device1)
luup.call_action(“urn:upnp-org:serviceId:Dimming1”,“SetLoadLevelTarget”,{newLoadlevelTarget = 100},device1)

luup.call_delay( ‘switch_off’, delay) – Call the switch off function after a delay of x seconds
function switch_off()

luup.call_action(“urn:dcineco-com:serviceId:MiLightRGBW1”,“SetHue”,{newHue = 40},device1)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“1” },device1)
luup.call_action(“urn:upnp-org:serviceId:Dimming1”,“SetLoadLevelTarget”,{newLoadlevelTarget = 100},device1)

luup.call_delay( ‘switch_arm’, delay1) – Call the switch off function after a delay of x seconds
function switch_arm()

luup.call_action(“urn:micasaverde-com:serviceId:SecuritySensor1”, “SetArmed”, {newArmedValue = “1”},device2)

end
end
end
end
end
checkmotion()

I know this is an old one but I have a no code solution that works perfect.
It is sad this basic scenario isn?t natively supported in Vera but here goes:

Install timer app and create a timer ?kitchen lights timer? set it to 5 or 10 minutes.
Scenes:

  1. Start timer: triggers when motion happens, all it does is reset and start the timer.
  2. Lights on by timer: if timer starts and not muted - turn on lights
  3. Lights off by timer: if timer finishes and not muted turn off lights.

Now you can mute the timer when you want to keep lights off or on.

This works perfectly for me in several rooms.

This is also exactly the function of the DelayLight plugin. Direct control of lights; no scenes required (but supports scenes if you wish).

with this plugin, if i turn the lights on with motion and set this plugin to turn off after 10 minutes - will subsequent motion triggers delay the “off” timer, or will they just start new timers?

Meaning - see this time line:
minute: action
0 - motion detected, lights on
0 - DelayLight starts 10 minutes lights off timer
4 - motion detected, lights on (were already on)
4 - DelayLight ?? starts a new timer or delay the current running timer?
10 - original timer is done. will the lights turn off? or was this timer cancelled and extended in minute 4?

my solution makes sure lights turn off 10 minutes after the latest motion detected, in the above scenario it will turn off at minute 14, not minute 10.

Subsequent motion events extend the timing (minute 14 in your example).

Wow that would be perfect! I will give it a try!
One last question if you already here, before I delete all my scenes for nothing…
Is it possible to “pause” this, meaning if i want the lights to stay on can I temporarily pause all/some timers?

DelayLight has two separate timings–auto and manual. The auto timing is started by sensors (usually motion). The manual timing is started by turning a switch on without motion. You can set a longer manual timing interval to keep the lights on (an hour, a day…). If you can’t beat the motion sensor to the switch, turning the switch off and then back on effectively switches the timing mode from auto to manual.

Alternately, you can use another sensor or switch (or any virtual sensor or switch) as an “inhibitor”, which prevents auto timing from starting. You can also disable the timer entirely via the switch on the DelayLightTimer’s dashboard card or Luup action.