Code: Select all
--[[
%% autostart
%% properties
%% events
62 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
--
-- UPDATES
-- 0.0.4 - Change in checkLights function. from == "1" to ~= "0"
-- 0.0.5 - possible to add a binarySwitch light and if not Night or
-- - other lights are turnOn it will also turnOn the binarySwitch
--
-------------------- USER SETTINGS -----------------------
lights2Check = {206,114,284}; -- If any of those lights is ON then set dimmer value to 100%
lights = {223}; -- LightId
sensor = 62 -- 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.5"
-- 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 and will be active for: "..endTimer.."sec");
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()
oneTimeDebugOnly = true
for i = 1,#lights do
lightItems = lights[i];
--if fibaro:getType(lightItems) ~= "com.fibaro.binarySwitch" then
if fibaro:getGlobal(varName) == varNightvalue and lightsOff then
if oneTimeDebugOnly then
Debug( "grey", "Night time, dim to 10%");
oneTimeDebugOnly = false end
fibaro:call(lightItems, "setValue", nightdimValue)
else
fibaro:call(lightItems, "setValue", "100")
if fibaro:getType(lightItems) == "com.fibaro.binarySwitch" then
fibaro:call(lightItems, "turnOn")
end
if oneTimeDebugOnly then
Debug( "grey", "Not night or other lights are on");
oneTimeDebugOnly = false end
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);