The Vera Community forums have moved!

Advanced => Programming => Scene Scripting => Topic started by: sillywalks on October 15, 2017, 11:43:16 am

Title: Turn On lights if movement and turn off if dont detect movement for a while
Post by: sillywalks on October 15, 2017, 11:43:16 am
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  ;)
Title: Re: Turn On lights if movement and turn off if dont detect movement for a while
Post by: cgmartin on October 20, 2017, 12:05:10 am
Hi @sillywalks,

I recognized that from the wiki Scripts for Scene examples (http://wiki.micasaverde.com/index.php/Scripts_for_scenes). 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:

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
Title: Re: Turn On lights if movement and turn off if dont detect movement for a while
Post by: parkerc on October 29, 2017, 01:22:06 pm
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)
Title: Re: Turn On lights if movement and turn off if dont detect movement for a while
Post by: ziggy5875 on May 27, 2018, 04:06:19 pm
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
Title: Re: Turn On lights if movement and turn off if dont detect movement for a while
Post by: RichardTSchaefer on May 28, 2018, 11:13:02 am
Run one PLEG
Title: Re: Turn On lights if movement and turn off if dont detect movement for a while
Post by: rafale77 on May 28, 2018, 11:57:09 am
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)
Title: Re: Turn On lights if movement and turn off if dont detect movement for a while
Post by: pukka on May 28, 2018, 01:28:55 pm
have a look at Plugin: DelayLight
http://forum.micasaverde.com/index.php/topic,60498.0.html  (http://forum.micasaverde.com/index.php/topic,60498.0.html)
Title: Re: Turn On lights if movement and turn off if dont detect movement for a while
Post by: Viruta57 on May 29, 2018, 05:14:05 am
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()
Title: Re: Turn On lights if movement and turn off if dont detect movement for a while
Post by: sirpinky on December 31, 2018, 12:40:11 am
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.
Title: Re: Turn On lights if movement and turn off if dont detect movement for a while
Post by: rigpapa on December 31, 2018, 09:43:41 am
This is also exactly the function of the DelayLight plugin. Direct control of lights; no scenes required (but supports scenes if you wish).