Det verkar som det funkar nu, variabeln uppdateras som det ska 1 gång per dygnBamsefar wrote:Ta bort dom två raderna som säger:
Fibaro:log(response)
"work tomorrow" false exakt som det ska.
Det verkar som det funkar nu, variabeln uppdateras som det ska 1 gång per dygnBamsefar wrote:Ta bort dom två raderna som säger:
Fibaro:log(response)
Japp det kan du få göra, funkar så bra igenBamsefar wrote:Äntligen! Nu kanske jag vågar lägga till koden för att slumpa fram släcktidpunkten som jag nämde tidigare!
Code: Select all
--[[
%% autostart
%% properties
%% globals
--]]
function Vacation_Days(dateForCheck)
-- Ange vilka datum som skall anses som lediga datum
local LedigaDatum = { "2014-05-02" , "2014-05-30" , "2014-07-07" , "2014-07-08" , "2014-07-09" , "2014-07-10" , "2014-07-11" , "2014-07-14" , "2014-07-15" , "2014-07-16" , "2014-07-17" , "2014-07-18" , "2014-07-21" , "2014-07-22" , "2014-07-23" , "2014-07-24" , "2014-07-25" , "2014-07-28" , "2014-07-29" , "2014-07-30" , "2014-07-31" , "2014-08-01" }
local datum
for z,datum in pairs(LedigaDatum)
do
TestDatum = os.date("*t", os.time({year = tonumber(string.sub(datum,1,4)), month = tonumber(string.sub(datum,6,7)), day = tonumber(string.sub(datum,9))}))
if ( TestDatum.year == dateForCheck.year and TestDatum.month == dateForCheck.month and TestDatum.day == dateForCheck.day ) then
return true
end
end
return false
end
function Holidays_CalculateEaster(year)
golden = (year % 19) + 1
c = math.floor(year/400) + math.floor(8*(math.floor(year/100)+11)/25) - math.floor(year/100)
s = (11 * golden + c) % 30
if (s < 0) then
s = s + 30
end
pfm = os.time{year=year, month=4, day=19} - s * 24 * 60 * 60
if (pfm == os.time{year=year, month=4, day=19}) then
pfm = pfm - 24 * 60 * 60
elseif (pfm == os.time{year=year, month=4, day=18} and golden > 11) then
pfm = pfm - 24 * 60 * 60
end
sunday = pfm + (7 - tonumber(os.date("%w", pfm))) * 24 * 60 * 60
friday = sunday - 2 * 24 * 60 * 60
return {year, tonumber(os.date("%m", friday)), tonumber(os.date("%d", friday))}
end
function WorkDay(dateToCheck)
local easterDay
local returnValue
returnValue = true
-- Indatum är en lördag eller Söndag
if ( dateToCheck.wday == 1 or dateToCheck.wday == 7 ) then
returnValue = false
-- Indatum är nationaldag
elseif ( dateToCheck.month == 6 and dateToCheck.day == 6 ) then
returnValue = false
-- Indatum är Nyårsdag eller trettondag
elseif ( dateToCheck.month == 1 and (dateToCheck.day == 1 or dateToCheck.day == 6 ) ) then
returnValue = false
-- Indatum är 1:a maj
elseif ( dateToCheck.month == 5 and dateToCheck.day == 1 ) then
returnValue = false
-- Indatum är Julafton,Juldag, Annandag eller Nyårsafton
elseif ( dateToCheck.month == 12 and (dateToCheck.day == 24 or dateToCheck.day == 25 or dateToCheck.day == 26 or dateToCheck.day == 31) ) then
returnValue = false
end
-- Långfredag
easterDay = Holidays_CalculateEaster(dateToCheck.year)
if ( easterDay[1] == dateToCheck.year and easterDay[2] == dateToCheck.month and easterDay[3] == dateToCheck.day ) then
returnValue = false
end
-- AnnandagPåsk
easterDay2 = os.date("*t", os.time({year = easterDay[1], month = easterDay[2], day = easterDay[3]})+3*24*60*60)
if ( easterDay2.year == dateToCheck.year and easterDay2.month == dateToCheck.month and easterDay2.day == dateToCheck.day ) then
returnValue = false
end
-- Kristihimmelfärdsdag? +41 dagar från Långfredag?
datum3 = os.date("*t", os.time({year = easterDay[1], month = easterDay[2], day = easterDay[3]})+41*24*60*60)
if ( datum3.year == dateToCheck.year and datum3.month == dateToCheck.month and datum3.day == dateToCheck.day ) then
returnValue = false
end
-- hitta midsommardagen för att kunna kontrollera midsommarafton
-- midsommardagen = den lördag som infaller under tiden den 20-26 juni -> midsommarafton bör vara den fredag som inträffar under tiden 19-25 juni
if ( dateToCheck.month == 6 and (dateToCheck.day >= 19 and dateToCheck.day <= 25) and dateToCheck.wday == 6 ) then
returnValue = false
end
if ( Vacation_Days(dateToCheck) ) then
returnValue = false
end
return returnValue
end
--Settings
LExecuteSecond = 60; -- Execute Every N-th second
-- Moved time settings to inside while loop for generating new values correct
LMidnightHour ="0100"; --Time which is considered to be midnight
LMidnightTime =0; -- Time when values are reset if empty it will run imediately
--kill any extra instances of the same scene
if (fibaro:countScenes() > 1) then fibaro:abort() end;
--Loop
while true do
-- Current Time
LCurrentDate = os.date("*t"); -- Current Date in table format
LCurrentTime = os.time(); -- Current time in unix format
-- Check if workday
if (LMidnightTime < LCurrentTime) then
if WorkDay(os.date("*t")) then
fibaro:setGlobal("WorkToday","True");
else
fibaro:setGlobal("WorkToday","False");
end
if WorkDay(os.date("*t", os.time()+24*60*60)) then
fibaro:setGlobal("WorkTomorrow","True");
else
fibaro:setGlobal("WorkTomorrow","False");
end
-- MidnightTime
LMidnightTime = os.time{year=LCurrentDate.year, month=LCurrentDate.month, day=LCurrentDate.day+1, hour=string.sub(LMidnightHour, 1 , 2), min=string.sub(LMidnightHour, 3)};
-- Turn off lamps 22:xx when tomorrow is workday, and 23:xx when tomorrow is not a workday
-- Setup time to turn off devices, LNightTime
if ( fibaro:getGlobalValue("WorkTomorrow") == "False" ) then
LNightHour = "23:" .. string.format("%02d",math.random(12,32));
else
LNightHour = "22:" .. string.format("%02d",math.random(12,32));
end
fibaro:debug("Work Today: " .. fibaro:getGlobalValue("WorkToday"));
fibaro:debug("Work Tomorrow: " .. fibaro:getGlobalValue("WorkTomorrow"));
end
if ( string.format("%02d", LCurrentDate.hour) .. ":" .. string.format("%02d", LCurrentDate.min) == LNightHour )
then
fibaro:call(38, "turnOff");--Köksfönstren
fibaro:sleep(math.random(8000,12000));
fibaro:call(4, "turnOff");--Bakom Soffan
fibaro:sleep(math.random(5000,8000));
fibaro:call(33, "turnOff");--Vitrinskåpet
fibaro:sleep(math.random(8000,12000));
fibaro:call(45, "turnOff");--Bakom Tvn
fibaro:sleep(math.random(8000,12000));
fibaro:call(36, "turnOff");--Fönsterlampa Alcia
fibaro:sleep(math.random(8000,12000));
fibaro:call(34, "turnOff");--Lampa Byrån
fibaro:sleep(math.random(8000,12000));
fibaro:call(79, "pressButton", "2");--Lampa Sovrummet
fibaro:sleep(math.random(5000,8000));
fibaro:call(80, "pressButton", "2");--Lampa Diskbänken
fibaro:sleep(math.random(5000,8000));
end
--Sleep LExecuteSecond seconds
fibaro:sleep(LExecuteSecond*1000);
end