On/off delay timer #
Create a resident script with 0 sleep time. Modify the timers
table as needed.
Each table key is an input group address (1/1/1
and 1/1/3
in this example).
Each table value is a table containing output
group address,
ON delay (delayon
) and OFF delay (delayoff
) values in seconds.
Sending the same ON or OFF value several times does not restart the timer.
if not client then
timers = {
['1/1/1'] = {
output = '1/1/2', -- binary output
delayon = 10, -- on delay time (seconds)
delayoff = 0, -- off delay time (seconds)
},
['1/1/3'] = {
output = '1/1/4', -- binary output
delayon = 40, -- on delay time (seconds)
delayoff = 60, -- off delay time (seconds)
}
}
grp.sender = 'tm'
function set(timer)
grp.checkwrite(timer.output, timer.state == 'on')
timer.state = nil
timer.ticks = nil
end
client = require('localbus').new(0.1)
client:sethandler('groupwrite', function(event)
local timer = timers[ event.dst ]
if timer and event.sender ~= grp.sender then
local value = tonumber(event.datahex, 16) or 0
local state = value > 0 and 'on' or 'off'
if timer.state ~= state then
timer.state = state
local ticks = timer['delay' .. state]
if ticks > 0 then
timer.ticks = ticks
else
set(timer)
end
end
end
end)
end
client:loop(1)
for _, timer in pairs(timers) do
if timer.ticks then
timer.ticks = timer.ticks - 1
if timer.ticks == 0 then
set(timer)
end
end
end