To jest główna pętla stworzona po to, żeby nie tworzyć wielu scen z własnym harmonogramami, które mogą się kłócić i zabijać procesor HC2. Niestety nie jest ona w pełni uniwersalna i trzeba odpowiednio dopasować własne sceny i parametry. W razie czego proszę pytać.
--------------------------
--[[
%% properties
%% autostart
%% globals
--]]
function timetonumber(s) --converts time in string i.ex. "00:00" to number
local xh, xm = s:match("(%d+)%:(%d+)")
local czaszimowy = 0
local date = os.date("*t")
return os.time({year = date.year, month = date.month, day = date.day, hour = xh, min = xm, sec = 0, }) - czaszimowy*3600
end
function extratime(x) -- calculates extra time in seconds before sunrise and after sunset
local currentDate = os.date("*t")
local month = currentDate.month
local cond = fibaro:getValue(3, "WeatherConditionConverted")
local k, w = 0, 1
if month < 6 then k = month else k = 12 - month end --shorter time in winter/fall
--if cond == 'clear' then w = 1.5 end --double time if clear weather
return x * k * w * 60
end
function setdark() --set real dark outsied
local sunset_str = fibaro:getValue(1, "sunsetHour")
local sunset_nr = timetonumber(sunset_str) + extratime(5)
return os.date("%H:%M", sunset_nr)
end
function setlight() --set real light outside
local sunrise_str = fibaro:getValue(1, "sunriseHour")
local sunrise_nr = timetonumber(sunrise_str) - extratime(5)
return os.date("%H:%M", sunrise_nr)
end
local oldpora = ''
local pora = ''
local zmianapory = false
local ciemno = setdark()
local jasno = setlight()
local oldDay = 0
local Poranek = false
local poraroku = ""
fibaro:debug('start')
fibaro:setGlobal("lastscene",'Od rana do nocy')
fibaro:sleep(1000)
--- Ustawienia wstępne po restarcie HC
fibaro:setSceneEnabled(150, false) --zdeaktywuj scenę nocnego oswietlenia
fibaro:setSceneEnabled(176, false) --zdeaktywuj scenę furtki po dzwonku
while true do
--local currTime = "06:46"
local currTime = os.date("%H:%M", os.time())
local currentDate = os.date("*t")
-- nowy dzień
if currentDate.day ~= oldDay
then
--ustawienie umownej pory roku:
if currentDate.month < 4 or currentDate.month >12 then poraroku = 'zima' end
if currentDate.month < 7 and currentDate.month > 3 then poraroku = 'wiosna' end
if currentDate.month < 10 and currentDate.month > 6 then poraroku = 'lato' end
if currentDate.month < 13 and currentDate.month > 9 then poraroku = 'jesień' end
fibaro:setGlobal("Pora_roku", poraroku)
-- ustawienie parametrów nowego dnia
ciemno = setdark()
jasno = setlight()
fibaro:debug('Nowy dzień '..currentDate.day..'-'..currentDate.month..' jest '..poraroku)
fibaro:debug('Wschód o: '..fibaro:getValue(1, "sunriseHour")..' jasno o: '..jasno..' /Zachód o: '..fibaro:getValue(1, "sunsetHour")..' ciemno o: '..ciemno)
oldDay = currentDate.day
Poranek = false
end
-- ustalenie pory dnia
if currTime >= ciemno
then if currTime >= "23:55"
then pora = 'Noc'
else pora = 'Wieczór' end
elseif currTime >= "07:30" --było 7:00
then pora = 'Dzień'
elseif currTime < jasno
then pora = 'Noc'
else pora = 'Poranek'; Poranek = true
end
if pora ~= oldpora
then
zmianapory = true
fibaro:setGlobal("Pora dnia", pora); fibaro:debug('Jest '..pora)
if pora == 'Poranek' or pora == 'Dzień' then fibaro:setGlobal("Ciemno", 0);fibaro:debug("Jest Jasno")
elseif pora == 'Wieczór' or pora == 'Noc' then fibaro:setGlobal("Ciemno", 1); fibaro:debug("Jest Ciemno") end
end
-- KONIEC ustalanie pory dnia i ciemno jasno - poniżej wkładać inne warunki czasowe
-- sceny zależne od pory dnia
if zmianapory == true
then
if pora == 'Dzień'
then if Poranek == false then fibaro:startScene(174); fibaro:debug('scena o poranku') end --
fibaro:startScene(173); fibaro:debug('scena za dnia')--
elseif pora == 'Poranek'
then fibaro:startScene(174); fibaro:debug('scena o poranku') --
elseif pora == 'Wieczór'
then fibaro:startScene(172); fibaro:debug('scena wieczorna') --
elseif pora == 'Noc'
then fibaro:startScene(171); fibaro:debug('scena nocna') --
else end
else
end
--sceny zależne od konkretnej godziny
if currTime == "05:45" then fibaro:startScene(101); fibaro:debug('scena stanu baterii') end
if currTime == "07:30" then fibaro:startScene(89); fibaro:debug('scena prognozy pogody') end
if currTime == "11:00" then fibaro:startScene(134); fibaro:debug('scena rolety front na słońce') end
if currTime == "17:30" then fibaro:startScene(136); fibaro:debug('scena rolety front do góry') end --tylko latem i wiosną
if currTime == "01:00" and tonumber(fibaro:getValue(237, "valueSensor")) < 91 then fibaro:call(237, "turnOff"); fibaro:debug('RTV OFF') end --wyłącz RTV w nocy jak nie używane
-- sceny uruchamiane co kilka minut
-- wyślij dane z pieca co 5 minut
if (math.floor(os.time()/60)-math.floor(1381874400/60))%5 == 0 then fibaro:call(189, "pressButton", "3") end
fibaro:sleep(1000)
-- wyślij dane nt temperatur co 10 minut
if (math.floor(os.time()/60)-math.floor(1381874400/60))%10 == 0 then fibaro:startScene(90) end
fibaro:sleep(1000)
-- wyłącz zbędne lampy i sprawdż kamerę furtki co 60 minut
if (math.floor(os.time()/60)-math.floor(1381874400/60))%60 == 0 then fibaro:startScene(59); fibaro:startScene(190) end
-- KONIEC warunków czasowych
oldpora = pora
zmianapory = false
fibaro:sleep(58*1000);
end