Fibaro LUA scripting, hall lighting

Forum all about Z-Wave Fibaro
Post Reply
User avatar
jrkalf
Member
Member
Posts: 291
Joined: Fri Nov 12, 2010 4:20 pm
Location: Nootdorp
Contact:

Fibaro LUA scripting, hall lighting

Post by jrkalf »

Hi guys,

Thanks to some guys over at the Fibaro forum and some effort of my own I've been able to use some of their code to build a proper "sunset" and "sunrise" timer and use that to automatically turn on my hallway lighting when the frontdoor is opened.

Here's the code for the NightTime:

Code: Select all

--[[ 
%% autostart 
%% properties 
%% globals 
--]] 


while true do 
local sunUp = os.date("%H:%M", os.time()+15*60)  --Off 30min before sunrise 
local sunDown = os.date("%H:%M", os.time()-15*60) --On 30min after sunset 
local currentDate = os.date("*t"); 

if ( sunUp == fibaro:getValue(1, "sunriseHour") ) 
then 
    fibaro:setGlobal("NightTime", "0");
    fibaro:debug("Gooooooodmorning Nootjedorp!!1!");
elseif ( sunDown == fibaro:getValue(1, "sunsetHour") ) 
then 
    fibaro:setGlobal("NightTime", "1");
    fibaro:debug("Fortune, good night; smile once more, turn thy wheel.");
end 

fibaro:sleep(60*1000); 
end 
For this code to work you need to make a standard "global variable" in the "Panels section" and then it's the "Variables Panel". The global variable is the top one of the two selectable. Call this variable NightTime and make it a zero or a 1 pending your current sunset area.


Second part of the code is the actual door and light code:

Code: Select all

--[[ 
%% autostart 
%% properties 
2 value 
%% globals
--]] 

if tonumber(fibaro:getGlobalValue("NightTime")) == 1 and tonumber(fibaro:getValue(2, "value")) == 1 
then
  fibaro:call(3, "turnOn");
  fibaro:sleep(120000);
  fibaro:call(3, "turnOff");
end
See the code above and here's the explanation.
In the top bit you see a 2 value declared. The number is the deviceID (not zwave nodeID) of the door sensor. We need to fetch the value before we're able to use it. Specifically from the deviceID number 2 we get the "value" information type and use this to see if the door is opened or not.

In the if statement we check if the NightTIme global variable is set to 1 and the door is opened (value 1) and then we'll call deviceID number 3 to "turnOn" and after a wait period of 120 seconds to "turnOff" automatically again.

Enjoy the code.
If you can't fix it with a hammer, you've got an electrical problem!

Fibaro HC2, various z-wave switching, alerting, detection modules.
ELV FHT80b heating system.
Post Reply

Return to “Fibaro”