Author Topic: Turn On lights if movement and turn off if dont detect movement for a while  (Read 1274 times)

Offline sillywalks

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
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 <period> 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  ;)
« Last Edit: October 15, 2017, 11:45:00 am by sillywalks »

Offline cgmartin

  • Newbie
  • *
  • Posts: 15
  • Karma: +2/-0
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:
  • Motion On: Whenever {Device} detects motion whether is armed or disarmed -> turn light/tv on
  • Motion Off: Whenever {Device} stops detecting motion whether is armed or disarmed -> turn light/tv off

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:

Code: [Select]
-- 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

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2425
  • Karma: +33/-45
  • Life Moves Pretty Fast....
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: [Select]
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 <period> 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)

Offline ziggy5875

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
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 <period> seconds.
    end
end
 
luup.call_delay ("checkLastTrip", period)
 
return true

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 10090
  • Karma: +762/-142
Run one PLEG

Offline rafale77

  • Hero Member
  • *****
  • Posts: 1186
  • Karma: +56/-22
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:


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

Code: [Select]
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 <period> seconds.
    end
end
 
luup.call_delay ("checkLastTrip", period)
openLuup (97 devices, 134 scenes, 20 apps) controlling HomeAss + VeraPlus (134 zwave nodes, 8 Zigbee nodes, 200 devices, 20 scenes , 2 app) Bridged to Homekit and Alexa. VeraPlus ExtRooted!

Offline pukka

  • Sr. Newbie
  • *
  • Posts: 26
  • Karma: +1/-0

Offline Viruta57

  • Jr. Member
  • **
  • Posts: 97
  • Karma: +1/-0
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()