Skip to content

Instantly share code, notes, and snippets.

@leeroybrun
Last active March 1, 2021 05:59
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save leeroybrun/9482703 to your computer and use it in GitHub Desktop.
Save leeroybrun/9482703 to your computer and use it in GitHub Desktop.
Fibaro LUA scenes
--[[
%% autostart
%% properties
%% globals
--]]
NB_DEVICES = 300
function turnAllOff()
fibaro:debug('Turn all devices off.')
-- Turn off all devices
for i = 1, NB_DEVICES do
fibaro:call(i, "turnOff");
end
end
local sourceTrigger = fibaro:getSourceTrigger();
if (sourceTrigger["type"] == "autostart") then
while true do
fibaro:debug('Scene automatically called.')
local currentDate = os.date("*t");
-- Turn off devices only Monday-Saturday between 19:00 and 19:10
if ((currentDate.wday >= 2 and currentDate.wday <= 7) and (currentDate.hour == 19 ) and (currentDate.min > 0 and currentDate.min < 10))
then
fibaro:debug('Lights automatically switched off.')
turnAllOff()
end
fibaro:sleep(5*60000); -- Run every 5 minutes
end
else
local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (startSource["type"] == "other")
then
turnAllOff()
end
end
--[[
%% autostart
%% properties
%% globals
--]]
NB_DEVICES = 300
NB_DEVICES_ON = 50
math.randomseed(os.time())
function rotateLights()
fibaro:debug('Rotate lights.')
math.randomseed(os.time())
local devices = {}
-- Turn off all devices
for i = 1, NB_DEVICES do
fibaro:call(i, "turnOff");
end
-- Generate random devices list
for i = 1, NB_DEVICES_ON do
devices[i] = math.random(NB_DEVICES)
end
-- Turn on NB_DEVICES_ON devices
for i = 1, NB_DEVICES_ON do
fibaro:call(devices[i], "turnOn");
end
end
local sourceTrigger = fibaro:getSourceTrigger();
if (sourceTrigger["type"] == "autostart") then
while true do
fibaro:debug('Scene automatically called.')
local currentDate = os.date("*t");
-- Rotate devices only Monday-Saturday from 10:00 to 19:00 (<= 18 so this can go up to 18:59 but not further)
if ((currentDate.wday >= 2 and currentDate.wday <= 7) and (currentDate.hour >= 10 and currentDate.hour <= 18 ))
then
fibaro:debug('Lights automatically rotated.')
rotateLights()
end
fibaro:sleep(5*60000); -- Run every 5 minutes
end
else
local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (startSource["type"] == "other")
then
rotateLights()
end
end
--[[
%% autostart
%% properties
%% globals
--]]
MAX_KWH = 10
NB_DEVICES = 300 -- Max number of devices
function checkLightsAndTurnOff()
fibaro:debug('Check lights.')
-- Turn off lights with hight consomation
for i = 1, NB_DEVICES do
if(tonumber(fibaro:getValue(i, "valueMeter")) and tonumber(fibaro:getValue(i, "value")) > 0 and tonumber(fibaro:getValue(i, 'dead')) < 1) then
if(tonumber(fibaro:getValue(i, "valueMeter")) > MAX_KWH) then
fibaro:debug("Turn off light "..i);
fibaro:call(i, "turnOff");
end
end
end
end
local sourceTrigger = fibaro:getSourceTrigger();
if (sourceTrigger["type"] == "autostart") then
while true do
fibaro:debug('Scene automatically called.')
local currentDate = os.date("*t");
-- Check devices only Monday-Saturday from 10:00 to 19:00 (<= 18 so this can go up to 18:59 but not further)
if ((currentDate.wday >= 2 and currentDate.wday <= 7) and (currentDate.hour >= 10 and currentDate.hour <= 18 ))
then
fibaro:debug('Lights automatically checked.')
checkLightsAndTurnOff()
end
fibaro:sleep(5*60000); -- Run every 5 minutes
end
else
local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (startSource["type"] == "other")
then
checkLightsAndTurnOff()
end
end
--[[
%% autostart
%% properties
%% globals
--]]
NB_DEVICES = 300
function wakeupDeadNodes()
fibaro:debug('Wakeup dead nodes.')
-- Check all devices
for i = 1, NB_DEVICES do
local status = fibaro:getValue(i, 'dead');
if status >= "1" then
fibaro:debug(i..': DEAD');
fibaro:wakeUpDeadDevice(i)
fibaro:sleep(5000) --check again in 5 sec
status = fibaro:getValue(i, 'dead');
if status >= "1" then
fibaro:debug(i..': Really dead')
else
fibaro:debug(i..': Now OK')
end
end
end
end
local sourceTrigger = fibaro:getSourceTrigger();
if (sourceTrigger["type"] == "autostart") then
while true do
fibaro:debug('Scene automatically called.')
local currentDate = os.date("*t");
-- Wakeup nodes only Monday-Saturday from 10:00 to 11:00
if ((currentDate.wday >= 2 and currentDate.wday <= 7) and (currentDate.hour >= 10 and currentDate.hour <= 11 ))
then
fibaro:debug('Nodes automatically waked up.')
wakeupDeadNodes()
end
fibaro:sleep(30*60000); -- Run every 30 minutes
end
else
local currentDate = os.date("*t");
local startSource = fibaro:getSourceTrigger();
if (startSource["type"] == "other")
then
wakeupDeadNodes()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment