Fibaro Home Center 2 – LUA-scen för att tända/släcka belysning – avancerade versionen
Fibaro Home Center 2 har stöd för scener som skrivs med LUA-kod. Möjligheterna är mycket stora att göra användbara avancerade scener. Vi har tänkt att vi i några korta guider ska presentera ett par olika scener.
Syftet med denna scen är att tända och sedan automatiskt släcka belysningen i våra lokaler.
- Om rörelse detekteras i lagret tänds all belysning förutom vid Micron och hålls tänd i 20 minuter om ingen ny rörelse detekteras.
- Om rörelse detekteras vid micron tänds all belysning och hålls tänd i 5 minuter om ingen ny rörelse detekteras.
TAnken med att ha två olika timervärden är att det inte är säkert att belysningen behövs i lagret bara för att man varit vid micron, och är man vid micron är man inte där särskilt länge.
Rörelsesensorn i lagret sitter inte helt optimalt till så man får gå en bit i mörker för att den ska triggas, och då är det fiffigt att tända allt när man är vi micron så man slipper gå i mörker i lagret. (Hänger ni med?)
Rörelsesensorn i lagret tänder dock inte vid Micron, för kommer man från lagret sitter rörelsesensorn bra till och tänder lampan lagom tills man är framme
Scriptet är välkommenterat, självklart är det bara att fråga om något är otydligt.
För att scriptet ska fungera måste variablerna timerMicro och timerLager vara skapade i panelen variabler. Här kan du läsa mer om variabler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
--[[ %% properties 152 value 202 value %% globals --]] --152 = PIR Lager --202 = PIR Micro --Scene purpose -- IF PIR detects movement at "Lager" then turn on all lamps except at the Micro and keep them on for 20 minutes. -- If PIR detects movement at "Micro" then turn on all lamps and keep them on for 5 minues. -- If new movement is detected within the timerange the timers are reset to keep lamps on. local sceneId = 25 --ID of this scene, used to detect number of instances run local timerEndLager = 1200 --Seconds to keep the lamp turned on after PIR detects movement local timerEndMicro = 300 --Seconds to keep the lamp turned on after PIR detects movement local timerEndMain = timerEndLager -- Set default timer value (will be altered if it's movement at the Micro) local timerLager = "timerLager" --Name of global variabel to control the timer in Lager local timerMicro = "timerMicro" local timerValueLager = fibaro:getGlobalValue(timerLager) --Current value of global variabel for timer local timerValueMicro = fibaro:getGlobalValue(timerMicro) --Current value of global variabel for timer local pirLager = fibaro:getValue(152, "value") --Get PIR value that activated this scene local pirMicro = fibaro:getValue(202, "value") --Get PIR value that activated this scene local lampaArbetsbank = 86 --lights to be controlled by this scene local lampaGang = 128 local lampaSwitch = 145 local lampaNyaLagret = 196 local lampaMicro = 78 local lampaEntre = 40 local fan = 156 --forced ventilation if ( tonumber(pirLager) < 1 ) then --if its not the Lager Pir that is triggerd, we'll handle the timer value. We'll check the Lager Pir since the Micro Pir stays triggered for 30 seconds and both Pirs will be triggered if we walk from micro to Lager fibaro:debug("pirMicro triggered") fibaro:debug("timerValueLager =" .. timerValueLager .. "timerEndMicro = " .. timerEndMicro ) if ( tonumber(timerValueLager) < timerEndMicro ) then --if the current timervalue is less than the preset timervalue for Micro Pir, set the timer value to 5 mins fibaro:debug("setting timer to micro") timerEndMain = timerEndMicro else -- if the timer value is greater than 5 mins, set the timervalue to the current value, it wont be changed this way and the rest of the script doesn't need to care about what triggerd the scene fibaro:debug("setting timer to current") timerEndMain = timerValueLager end end fibaro:debug("TimerEndMain = " .. timerEndMain) fibaro:debug("Start! SceneId=" .. fibaro:countScenes(sceneId) .. ", timerLager = " .. timerValueLager .. ", timerMicro = " .. timerValueMicro .. " lights = " .. fibaro:getValue(lampaNyaLagret, "value") .. " ,pirLager = " .. pirLager .. " ,pirMicro = " .. pirMicro) if ( fibaro:countScenes(sceneId) == 1 and (tonumber(pirLager) + tonumber(pirMicro)) > 0 ) then --First run and Pir is triggered fibaro:debug("Lets begin") fibaro:call(lampaArbetsbank, "turnOn") --Turn lights on fibaro:call(lampaGang, "turnOn") fibaro:call(lampaSwitch, "turnOn") fibaro:call(lampaNyaLagret, "turnOn") fibaro:call(lampaEntre, "turnOn") fibaro:call(fan, "turnOn") if ( tonumber(pirMicro) > 0 ) then fibaro:debug("Micro on") fibaro:call(lampaMicro, "turnOn") fibaro:setGlobal(timerMicro,timerEndMicro) --Set global variable to the correct timer end fibaro:setGlobal(timerLager, timerEndMain) --Set global variable to the correct timer while (fibaro:getGlobalValue(timerLager) ~= "0") do -- loop until the timer reaches 0 timerValueLager = tonumber(fibaro:getGlobalValue(timerLager)) --Get current value of global variabel timer timerValueMicro = tonumber(fibaro:getGlobalValue(timerMicro)) --Get current value of global variabel timer if ( timerValueMicro > 0 ) then fibaro:setGlobal(timerMicro, timerValueMicro-1) --Decrease global variabel for timer with 1 second end if ( timerValueMicro == 0 ) then fibaro:call(lampaMicro, "turnOff") end fibaro:setGlobal(timerLager, timerValueLager-1) --Decrease global variabel for timer with 1 second fibaro:debug("Loop - timerLager = " .. fibaro:getGlobalValue(timerLager) .. "timerMicro = " .. fibaro:getGlobalValue(timerMicro)) fibaro:sleep(1000) --Wait 1 second end --times up fibaro:debug("Släcker") fibaro:call(lampaArbetsbank, "turnOff") --Turn lights on fibaro:call(lampaGang, "turnOff") fibaro:call(lampaSwitch, "turnOff") fibaro:call(lampaNyaLagret, "turnOff") fibaro:call(lampaEntre, "turnOff") fibaro:call(lampaMicro, "turnOff") elseif fibaro:countScenes(sceneId) > 1 then --We've already got an instance of this scene running in the background main purpose now is to reset the timer since new movement is detected fibaro:call(lampaArbetsbank, "turnOn") --Turn lights on, just to be sure they are on fibaro:call(lampaGang, "turnOn") fibaro:call(lampaSwitch, "turnOn") fibaro:call(lampaNyaLagret, "turnOn") fibaro:call(lampaEntre, "turnOn") fibaro:call(fan, "turnOn") if ( tonumber(pirMicro) > 0 ) then fibaro:call(lampaMicro, "turnOn") fibaro:setGlobal(timerMicro,timerEndMicro) --Set global variable to the correct timer end fibaro:setGlobal(timerLager, timerEndMain) --Reset the timer, full value if Lager is triggered, Micro value if timer is less than micro and current value if micro is triggered and timer is greater than the micro values. end fibaro:debug("End, instances = " .. fibaro:countScenes(sceneId)) |