Code: Select all
--[[
%% properties
76 value
%% globals
--]]
--76 = PIR for movement detection
--Scene purpose
--IF PIR detects movement then turn on dimmer and turn it off 5 minutes.
-- If new movement detected during the 5 minute period set the timer for 5 minutes
local sceneId = 59 --ID of this scene, used to detect number of instances run
local timerEndTime = 2 --Minutes to keep the lamp turned on after PIR detects movement
local timer = "timer" --Name of global variabel to control the timer
local timerValue = fibaro:getGlobalValue(timer) --Current value of global variabel for timer
local triggerValue = fibaro:getValue(76, "value") --Get PIR value that activated this scene
local lights = 5 --light to be controlled by this scene
--local dimmerValue = "20" --Ljusstyrka
fibaro:debug("start, timer = " .. timerValue .. " lights = " .. fibaro:getValue(lights, "value") .. " ,triggerValue = " .. triggerValue)
if (fibaro:getValue(lights, "value") == "0" and triggerValue == "1") then
--If starts if lights off and PIR detected movement
fibaro:call(lights, "turnOn") --Turn lights on
fibaro:setGlobal(timer, timerEndTime) --Set global variabel for timer
while (fibaro:getGlobalValue(timer) ~= "0") do
--Loop used to control timer, each loop 1 minute and continues
--as long as timer variabel isn't 0
timerValue = tonumber(fibaro:getGlobalValue(timer)) --Get current value of global variabel timer
fibaro:setGlobal(timer, timerValue-1) --Decrease global variabel for timer with 1 minute
fibaro:debug("Loop - timer = " .. fibaro:getGlobalValue(timer))
fibaro:sleep(60*1000) --Wait 1 minute
end
fibaro:call(lights, "turnOff") --Turn lights off
elseif fibaro:countScenes(sceneId) > 1 then
--if scene is already running the set timer to 2 minutes
fibaro:setGlobal(timer, timerEndTime) --set global variabel for timer to 2 minutes
fibaro:debug("New instance, totalt = " .. fibaro:countScenes(sceneId))
end
fibaro:debug("End, instances = " .. fibaro:countScenes(sceneId))