This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Timer with manual stop
#2
Try this modified script. It allows mapping one output to multiple inputs. When a timer is started all running timers for the same output are stopped. Writing to the output also stops any associated timers.

Code:
if not client then
  timers = {
    ['0/0/1'] = {
      output = '0/0/255', -- dimmer control (0..100%)
      onvaluehigh = 50, -- high output value in %
      onvaluelow = 15, -- low output value in %
      timeouthigh = 8, -- time to keep high value (in seconds)
      timeoutlow = 3, -- time to keep low value (in seconds)
    },
    ['0/0/6'] = {
      output = '0/0/255', -- dimmer control (0..100%)
      onvaluehigh = 80, -- high output value in %
      onvaluelow = 30, -- low output value in %
      timeouthigh = 5, -- time to keep high value (in seconds)
      timeoutlow = 2, -- time to keep low value (in seconds)
    }
  }

  outputs = {}

  function timerreset(addr)
    for _, timer in pairs(timers) do
      if timer.output == addr then
        timer.ticks = nil
        timer.state = 'off'
      end
    end
  end

  function timersetstate(timer, state)
    grp.checkwrite(timer.output, timer['onvalue' .. state] or 0)
    timer.state = state
    timer.ticks = timer['timeout' .. state]
  end

  function timertimeout(timer)
    local state = timer.state == 'high' and 'low' or 'off'
    timersetstate(timer, state)
  end

  for _, timer in pairs(timers) do
    timer.state = 'off'
    outputs[ timer.output ] = true
  end

  grp.sender = 'tm'

  client = require('localbus').new(0.1)
  client:sethandler('groupwrite', function(event)
    if event.sender == grp.sender then
      return
    end

    local addr = event.dst
    local timer = timers[ addr ]

    if timer then
      local value = tonumber(event.datahex, 16) or 0

      if value == 1 then
        timerreset(timer.output)
        timersetstate(timer, 'high')
      end
    elseif outputs[ addr ] then
      timerreset(addr)
    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
      timertimeout(timer)
    end
  end
end
Reply


Messages In This Thread
Timer with manual stop - by AndersH - 09.10.2024, 13:44
RE: Timer with manual stop - by admin - 10.10.2024, 07:22
RE: Timer with manual stop - by AndersH - 10.10.2024, 08:51

Forum Jump: