I have created the code below, however am worried it is not very efficient.
The basic flow is:
- Motion detected
- If light is off then turn it on
- Trigger timer passing tripped time as data
- When timer ends test whether motion has been tripped again since the time was called
- If motion was not tripped since the timer was called then switch light off
However, this means I have lots of parallel timer threads running, as a new one is created every time motion is detected.
Would it be more efficient to have a loop that runs every second and evaluates LastTrip timestamps for all motion detectors and turns off lights if they detector was not tripped for some time? I would call the loop function once on startup then have a timer at the end to call itself after a 1 sec delay.
Current code snippet is below.
Thanks,
Chris
[code]function setup_watchers()
core_log(“Setting up watchers”)
luup.variable_watch(“livingroom_motion”, MOTION_SID, “Tripped”, LIVINGROOM_MOTION)
end
function livingroom_motion()
local tripped = luup.variable_get(MOTION_SID, “Tripped”, LIVINGROOM_MOTION)
if (tripped == “1”) then
core_log(“Livingroom motion detected”)
local this_trip = os.time()
local last_trip = luup.variable_get( MOTION_SID, “LastTrip”, LIVINGROOM_MOTION) or this_trip
local day = luup.variable_get(DAY_NIGHT_SID, “Status”, DAY_NIGHT)
local receiver_power = luup.variable_get(SW_SID, “Status”, LIVINGROOM_RECEIVER)
local receiver_input = luup.variable_get(INPUT_SID, “Input”, LIVINGROOM_RECEIVER)
local transport_state = luup.variable_get(AV_TRANSPORT_SID, “TransportState”, SONOS)
local dimmer_status = luup.variable_get(DIMMER_SID, “LoadLevelStatus”, LIVINGROOM_LIGHT)
local house_mode = luup.variable_get(VCONTAINER_SID, “Variable1”, HOUSE_VARS)
luup.variable_set(MOTION_SID, "LastTrip", this_trip, LIVINGROOM_MOTION)
-- activate lights if night and home and not already on and av off or set to music
core_log("house_mode=" .. house_mode .. ", dimmer_status=" .. dimmer_status .. ", day=" .. day .. ", receiver_power=" .. receiver_power .. ", receiver_input=" .. receiver_input)
if (house_mode == "Home") and (day == "0") and (receiver_power == "0" or (receiver_power == "1" and receiver_input == MUSIC_INPUT)) then
if (dimmer_status == "0") then
core_log("Setting Livingroom light dim level to 100%")
luup.call_action( DIMMER_SID, "SetLoadLevelTarget", { newLoadlevelTarget = LIVINGROOM_LEVEL}, LIVINGROOM_LIGHT)
end
luup.call_timer("livingroom_lights_off", 1, LIGHTS_DELAY, "", this_trip)
end
-- activate music if sonos playing and home amp not already on
core_log("house_mode=" .. house_mode .. ", transport_state=" .. transport_state .. ", receiver_power=" .. receiver_power)
if (house_mode == "Home") and (transport_state == "PLAYING") then
if (receiver_power == "0") then
core_log("Running Livingroom music on scene")
luup.call_action(HA_SID, "RunScene", {SceneNum = LIVINGROOM_MUSIC_ON}, 0)
end
luup.call_timer("livingroom_music_off", 1, MUSIC_DELAY, "", this_trip)
end
end
end
function livingroom_lights_off(this_trip)
core_log(“Livingroom lights off called by timer”)
local last_trip = luup.variable_get( MOTION_SID, “LastTrip”, LIVINGROOM_MOTION)
local dimmer_status = luup.variable_get(DIMMER_SID, “LoadLevelStatus”, LIVINGROOM_LIGHT)
core_log("this_trip=" .. this_trip .. ", last_trip=" .. last_trip)
-- Only turn off if the sensor was not tripped again after the timer was called and the light is still on
if (this_trip == last_trip) then
core_log("Turning livingroom light off")
luup.call_action( DIMMER_SID, "SetLoadLevelTarget", { newLoadlevelTarget = "0"}, LIVINGROOM_LIGHT)
end
end
function livingroom_music_off(this_trip)
core_log(“Livingroom music off called by timer”)
local last_trip = luup.variable_get( MOTION_SID, “LastTrip”, LIVINGROOM_MOTION)
local receiver_power = luup.variable_get(SW_SID, “Status”, LIVINGROOM_RECEIVER)
local receiver_input = luup.variable_get(INPUT_SID, “Input”, LIVINGROOM_RECEIVER)
core_log("this_trip=" .. this_trip .. ", last_trip=" .. last_trip .. ", receiver_power= " .. receiver_power .. ", receiver_input=" .. receiver_input)
-- Only turn off if the sensor was not tripped again after the timer was called and the amp is still powered up and on the music input
if (this_trip == last_trip) and (receiver_power == "1") and (receiver_input == MUSIC_INPUT) then
speak("turning off music in the living room")
core_log("Running livingroom music off scene")
luup.call_action(HA_SID, "RunScene", {SceneNum = LIVINGROOM_MUSIC_OFF}, 0)
end
end[/code]