-- SCENE SCENARIO
-- You wake up and need to go up to bathroom during the night
-- Sensor is triggered in bathroom, lights will only dim to 10%
-- and turnOff after "endTimer" has stop.
-- Its saturday night and you have freinds at home. Your "TimeOfDay"
-- variable have already been set to "Night". Now you dont want to
-- light up the bathroom to only 10%. So if any of the "lights2Check"
-- is on, it will set dim value to 100% and turnOff after "endTimer"
-- has stop.
-- If sensor is triggered again during the "endTimer" period,
-- the "endTimer" period will start over.
Code: Select all
--[[
%% autostart
%% properties
221 value
%% globals
--]]
-- REFERENCE
-- forum.fibaro.com, lua.org, domotique-fibaro.fr,www.gronahus.se
-- Thanks to Richo, stevenvd, Steven for good LUA functions code.
-- SCENE SCENARIO
-- You wake up and need to go up to bathroom during the night
-- Sensor is triggered in bathroom, lights will only dim to 10%
-- and turnOff after "endTimer" has stop.
-- Its saturday night and you have freinds at home. Your "TimeOfDay"
-- variable have already been set to "Night". Now you dont want to
-- light up the bathroom to only 10%. So if any of the "lights2Check"
-- is on, it will set dim value to 100% and turnOff after "endTimer"
-- has stop.
-- If sensor is triggered again during the "endTimer" period,
-- the "endTimer" period will start over.
-- FEATURES
-- Improvements
-- 0.0.4 - Change in checkLights function. from == "1" to ~= "0"
-------------------- USER SETTINGS -----------------------
lights2Check = {339,341}; -- If any of those lights is ON then set dimmer value to 100%
lights = {343}; -- LightId
sensor = 221 -- SensorId
nightdimValue = "10" -- Dimmer value to set during night
endTimer = 30 -- How long to keep lights on, in seconds
varName = "TimeOfDay" -- Variable name
varNightvalue = "Natt" -- varName predefined value
debug = true -- set debug to true or false
-----------------------------------------------------------
------------- DO NOT CHANGE LINES BELOW -------------------
startSource = fibaro:getSourceTrigger();
version = "0.0.3"
-- Give debug a fancy color
Debug = function ( color, message )
fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span"));
end
-- Kill new instance of this scene
if (fibaro:countScenes() > 1) then
Debug( "red", "Abort, count scene = "..fibaro:countScenes());
fibaro:abort();
end
-- Scene was triggered, function
SceneTriggered = function()
checkLights();
turnLightOn();
startTimer();
end
-- Timer, function
startTimer = function ()
if debug then
Debug( "green", "Timer started");
end
local counter = endTimer
while(counter > 0)
do
counter = counter - 1
fibaro:sleep(1000)
if
tonumber(fibaro:getValue(sensor, "value")) > 0 then
counter = endTimer;
end
end
if debug then
Debug( "green", "Timer stopped");
end
for i = 1,#lights do
lightItems = lights[i];
fibaro:call(lightItems, "turnOff");
end
end
-- TurnOn lights, functions
turnLightOn = function()
for i = 1,#lights do
lightItems = lights[i];
if fibaro:getGlobal(varName) == varNightvalue and lightsOff then
Debug( "grey", "Night time, dim to 10%");
fibaro:call(lightItems, "setValue", nightdimValue)
else
fibaro:call(lightItems, "setValue", "100")
Debug( "grey", "Not night or other lights is ON");
--end
end
end
end
checkLights = function()
Debug( "grey", "Check if lights is ON");
for i = 1,#lights2Check do
lightItems = lights2Check[i];
status = fibaro:getValue(lightItems, "value")
if status ~= "0" then lightsOff = false break
else lightsOff = true end
end
end
------------------ START OF SCENE ----------------------
if ( startSource["type"] == "other" ) then
SceneTriggered();
elseif ( startSource["type"] == "property" ) then
SceneTriggered();
end
Debug( "orange", "Lights & Motion Scene - LUA Scripting by Jonny Larsson 2015" );
Debug( "orange", "Version: "..version);