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.

Staircase script with dimming option
#7
(09.06.2022, 15:33)admin Wrote: Post your full script listing. Also enable logging for mapped object and see if they change values. Check Error logs as well.

Thanks. Here is my answer:
Sorry for not noticing the error log. There are errors:

---
Resident script:19: attempt to compare nil with number
stack traceback:
Resident script:19: in function 'setinputvalue'
Resident script:54: in function <Resident script:47>
Library localbus: in function ''
Library localbus: in function ''
Library localbus: in function 'loop'
---

'1/1/7' is the object of the dimming value.
I have logged 1/1/7. No logs registered.


Code:
if not client then
  timers = {
    {
      input = '9/0/1', -- binary PIR status
      output = '1/1/7', -- dimmer control (0..100%)
      onvaluehigh = 90, -- high output value in %
      onvaluelow = 10, -- low output value in %
      timeout = 5, -- in seconds
    }
  }

  function setinputvalue(timer, event)
    local value = busdatatype.decode(event.datahex, dt.bool)

    if not value then
      return
    end

    if timer.value < timer.onvaluelow then
      value = timer.onvaluelow
    elseif timer.outvalue < timer.onvaluehigh then
      value = timer.onvaluehigh
    else
      value = nil
    end

    if not timer.ticks and value then
      grp.write(timer.output, value, dt.scale)
    end

    timer.ticks = timer.timeout
  end

  function setoutputvalue(timer, event)
    timer.outvalue = busdatatype.decode(event.datahex, dt.scale)
    timer.ticks = nil -- stop timer
  end

  for _, timer in ipairs(timers) do
    timer.outvalue = grp.getvalue(timer.output)
  end

  sender = 'tm'
  grp.sender = sender

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

    for _, timer in ipairs(timers) do
      if timer.input == event.dst then
        setinputvalue(timer, event)
      elseif timer.output == event.dst then
        setoutputvalue(timer, event)
      end
    end
  end)
end

client:loop(1)

for _, timer in ipairs(timers) do
  if timer.ticks then
    timer.ticks = timer.ticks - 1

    if timer.ticks == 0 then
      grp.write(timer.output, timer.outvalue, dt.scale)
      timer.ticks = nil
    end
  end
end
Reply


Messages In This Thread
RE: Staircase script with dimming option - by Dirk79 - 11.06.2022, 15:28

Forum Jump: