In the code above if motion kept activated till end of timer the switch off command took place.
Solved in this one by starting the counter after motion sensor goes in OFF state, if reactivated on motion counter reset till next OFF state
No problems just wanted to share this.
Code: Select all
return {
active = true,
on = {
devices = {
"Lamp Gang",
"PIR Gang"
}
},
data =
{
sensorTurnedOnLights = { initial = false },
},
execute = function(dz, item)
local Lux = dz.devices('PIR Test Lux').lux
local maxLux = 150
local maxSeconds = 300
local motion = dz.devices('PIR Gang')
local switch = dz.devices('Lamp Gang')
if item == motion and Lux < maxLux then -- motion active
if motion.state == 'On' and switch.state == 'Off' then -- motion was the trigger
dz.data.sensorTurnedOnLights = true
switch.cancelQueuedCommands()
switch.switchOn().checkFirst()
elseif motion.state == 'On' and switch.state == 'On' and dz.data.sensorTurnedOnLights == true then -- new motion detection
switch.cancelQueuedCommands()
elseif motion.state == 'Off' and dz.data.sensorTurnedOnLights == true then -- switched on by sensor now motion in off state
switch.switchOff().afterSec(maxSeconds).checkFirst()
end
elseif item == switch and item.state == 'On' and motion.state == 'Off' then -- switch activated
dz.data.sensorTurnedOnLights = false
end
end
}