Timer function to check if motion persists to keep light on

Hello, I’m looking for some suggestions on creating a timer function of some sort for the scene I’ve written.

The scene checks if it is dark and the light is off, and on motion turns on the light. I currently just have it set for a 5 minute delay because I couldn’t get the timer function working. I kept getting errors using the luup.call_timer function despite my code being spot on with other examples I’ve found here.

So then I tried an if(timerCheckMotion())then type approach, but when timerCheckMotion() was called, I would get a call error regarding a Nil value and I couldn’t figure out why.

Here is the code so far. Needs a bit of clean-up, but I’m looking to replace the sleep timeout and light off stuff, with a call to a timer function that checks if motion is detected, then call the timerCheckMotion() function again recursively until no motion is detected after the interval. At this point the light gets turned off. Also, there would be a check if the light is already off, and break the loop.

Any help would be greatly appreciated. And if the code is useful for anyone else, have at 'er :slight_smile:

[code]local timerVal = “300” – “30” for 30 seconds
local lightThresh = “50” – “0”-“100” in percentage points

local LS_SID = “urn:schemas-micasaverde-com:device:LightSensor:1”
local LS_DEV = 9

local MS_DEV = 8

local LIGHT_SID = “urn:schemas-upnp-org:device:BinaryLight:1”
local LIGHT_DEV = 11
local LightON = “1”
local LightOFF = “0”

local function turnLightOnOff(reqState)

	luup.task("Action: change light state",1, "Result", -1)

 		luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = reqState}, LIGHT_DEV)

end

local function isLightOff()

luup.task("Check: is Light Off",1, "Result", -1)

local lightOnOff = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", LIGHT_DEV)

if(lightOnOff == "0")then
	return true
else
	return false
end

end

local function isDark()

luup.task("Check: is Dark",1, "Result", -1)

local lightLevel = luup.variable_get("urn:micasaverde-com:serviceId:LightSensor1","CurrentLevel",LS_DEV)

if(lightLevel <= "50")then   --need to add threshold value here instead
	return true
else
	return false
end

end

local function isMotionDetected()

luup.task("Check: is Motion Detected",1, "Result", -1)

local motionDet = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", MS_DEV)

if(motionDet == "1")then
	return true
else
	return false
end

end

local function nightLight()

luup.task("night Light engaged",1, "Result", -1)

if(isLightOff() and isDark())then
	
	turnLightOnOff(LightON)		
	luup.task("Light turned on",1, "Result", -1)
	
	luup.task("Sleep: Waiting for user interval",1, "Result", -1)
	luup.sleep(timerVal * 1000)

	turnLightOnOff(LightOFF)
	luup.task("Light turned off",1, "Result", -1)
		
	return true
else 
	luup.task("DEBUG: Light is already on or it is not dark",1, "Result", -1)

	return false
end

end

nightLight()
luup.task(“DEBUG: night Light ended”,1, “Result”, -1)[/code]

hello

i have posted here
http://forum.micasaverde.com/index.php/topic,8283.15.html
i want turn on light when motion is detected

i want get state of lap before motion and return whith the original status after a delay on for on and off for off

it’s possible with your script ?

thanks