Page 1 of 1

Pre-Timer

Posted: Tuesday 12 May 2020 15:54
by azonneveld
I use Domoticz for my wake-up timer. When the timer is triggered, one of the actions is: my bedroom lights are switched on.
Another action could be, that the heating is switched on. But I want the heating to switch on XX minutes before the bedroomlights are switched on.
I do not want to use a second timer, this is confusing when setting the wake-up alarm.

So, I made a script, which detects the next trigger for a specific timer.

Perhaps this script could also useful for someone else.

1. The script should run as a time based script, this makes it running every minute.
2. The script is intended for timers that occur once a day, repeating on a weekly basis.

Code: Select all

commandArray = {}

local VerwarmingEerder = 15    --Minutes pre-trigger is ahead of 'timername' 
local timername = 'WakeUpTimer'
local tNow = os.date("*t");
local TimeInMinutes = tNow.min + tNow.hour * 60;
local triggerfound = 0  


weekday = tonumber(os.date("%w"))    --zaterdag = 6 ---  zondag = 0
if weekday == 0 then
    weekday = 7
end
weekdaybitwise = 2^(weekday-1)

function bitand(a, b)
    local result = 0
    local bitval = 1
    while a > 0 and b > 0 do
      if a % 2 == 1 and b % 2 == 1 then -- test the rightmost bits
          result = result + bitval      -- set the current bit
      end
      bitval = bitval * 2 -- shift left
      a = math.floor(a/2) -- shift right
      b = math.floor(b/2)
    end
    return result
end

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  
local a = assert(io.popen('curl "http://<DOMOTICZ-IP:PORT>/json.htm?type=schedules&filter=device"'))
local b = a:read('*all')
a:close()
local c = json:decode(b)
local result_table = c['result']
        
tc = #result_table
for i = 1, tc do
    if triggerfound == 0 then
        if result_table[i].DevName==timername then
            if result_table[i].Active == 'true' then
                if tostring(result_table[i].TimerCmd) == '0' then
                    if bitand(tonumber(result_table[i].Days),weekdaybitwise) > 0 then
                        t = result_table[i].Time
                        h = string.sub(t,1,2)
                        m = string.sub(t,4,5)
                        minuteofday = tonumber(h)*60+tonumber(m)
                        
                        if minuteofday - VerwarmingEerder == TimeInMinutes then   
                           ---- Actions taken when pre-timer is triggered (f.i. wake the wife so she can make coffee)
                        end
                        
                        triggerfound = 1  --stop further processing of script
                       
                    end          
                end 
            end
        end
    end
end

return commandArray