Code: Select all
--[[
%% properties
80 value
%% globals
]]--
-- Set Variables
LightID = 474 -- The dimmer to activate
TriggerID = 80 -- Device that is breached for turning on light
wakeUpHour = 5 -- Before this hour light is dimmed
debugState = false -- Turn on and of debugging
alwaysTurnOn = false -- Override darkness
maxLight = 99 -- Level of light after wakeUpHour
dimLight = 20 -- Level of light before wakeUpHour
lightOffAfterMin = 15 -- How long (minutes) before auto off
local sourceTrigger = fibaro:getSourceTrigger();
-- Give debug a fancy color
Debug = function ( color, message )
if debugState then fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span")); end
end
Debug( "orange", "AutoLight - LUA Scripting by Klas Bonde 2016" );
-- Timer, function
startTimer = function ()
Debug( "green", "Timer started");
local counter = lightOffAfterMin * 60
while(counter > 0)
do
counter = counter - 1
fibaro:sleep(1000)
if tonumber(fibaro:getValue(LightID, "value")) < 1 then
counter = 0;
Debug( "green", "Light turned off manually");
fibaro:sleep(5000) -- dont turn on light on way out
Debug( "green", "Door sensor active again");
end
end
Debug( "green", "Timer stopped");
end
if (sourceTrigger["type"] == "autostart") then
if (fibaro:countScenes() > 1) then fibaro:abort() end;
else
if (fibaro:countScenes() > 1) then fibaro:abort() end;
Debug( "orange", "Started by trigger - Nr of active scenes = "..fibaro:countScenes() );
local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (
( tonumber(fibaro:getValue(TriggerID, "value")) > 0 ) -- or ( startSource["type"] == "other" )
)
then
if (tonumber(fibaro:getValue(LightID, "value")) < 1) then
Debug( "orange", "The lamp is off");
if ((fibaro:getGlobal("dark") == "1") or alwaysTurnOn) then
Debug( "orange", "It is dark.");
if alwaysTurnOn then Debug ( "red", "But darkness is overridden" ) end
if ( currentDate.hour > wakeUpHour ) then
Debug( "yellow", "It is not before wakeup so lamp is turned on at "..maxLight.."%");
fibaro:call(LightID, "setValue", maxLight)
else
Debug( "yellow", "It is before wakeup so lamp is set to "..dimLight.."%");
fibaro:call(LightID, "setValue", dimLight)
end
Debug( "green", "Countdown started to LightOff");
startTimer();
else
Debug( "green", "It is daylight - not activating lamp");
end
else
Debug( "orange", "The lamp is already on");
end
end
end