15.08.2023, 16:32
(08.12.2022, 13:02)admin Wrote: Resident script with 0 sleep time. Single script can be used for multiple timers by adding more entries to the timers table at the top of the script.I use the script above when the input becomes 1 the output becomes 1 10 seconds later. If the input becomes 0 the output stays 1 not going back to 0. What is the problem?
Code: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)
}
}
grp.sender = 'tm'
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
timer.ticks = timer['delay' .. state]
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
grp.checkwrite(timer.output, timer.state == 'on')
timer.state = nil
timer.ticks = nil
end
end
end