Lua Fan Controller Scripts

Discussion in 'C-Bus Automation Controllers' started by noyzey, Apr 16, 2026.

  1. noyzey

    noyzey

    Joined:
    Mar 24, 2010
    Messages:
    48
    Likes Received:
    4
    Location:
    Sunshine Coast, Australia
    Hi Guys

    I have a legacy system upgrade from wiser2 to AC2. The house has old DLTs and had logic scripts to control the speed of the ceiling fans from a bell press button on the DLT. I've written a lua script to provde the same function from the switch, but it's not working. Any advice would be apreciated.

    Lua Script below - GA181 is the bell press on the DLT
    Code:
    FanPress1 = GetCBusLevel(0, 56, 181)
    Fan1LowGA = 160
    Fan1MedGA = 159
    Fan1HighGA = 158
    
    if FanPress1 == 255 then
      FanCount1 = FanCount1 + 1
    
      if FanCount1 == 1 then
            SetCBusState(0, 56, Fan1LowGA, true)
            SetCBusState(0, 56, Fan1MedGA, false)
            SetCBusState(0, 56, Fan1HighGA, false)
      end
     
      if FanCount1 == 2 then
            SetCBusState(0, 56, Fan1LowGA, false)
            SetCBusState(0, 56, Fan1MedGA, true)
             SetCBusState(0, 56, Fan1HighGA, false)
      end
     
       if FanCount1 == 3 then
            SetCBusState(0, 56, Fan1LowGA, false)
            SetCBusState(0, 56, Fan1MedGA, false)
            SetCBusState(0, 56, Fan1HighGA, true)
      end
     
      if FanCount1 == 4 then
            SetCBusState(0, 56, Fan1LowGA, false)
            SetCBusState(0, 56, Fan1MedGA, false)
            SetCBusState(0, 56, Fan1HighGA, false)
            FanCount1 = 0
      end
     
    end
     
    noyzey, Apr 16, 2026
    #1
  2. noyzey

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    257
    Likes Received:
    31
    Location:
    New Zealand
    An idea that might make it more functional, instead of responding to bell press, make the edlt input an on only then use the code to switch off the GA something like

    local FanPress1 = GetCBusLevel(0, 56, 181)
    local Fan1LowGA = 160
    local Fan1MedGA = 159
    local Fan1HighGA = 158

    if FanPress1 == 255 then
    -- increment and wrap 0–3
    FanCount1 = ((FanCount1 or 0) + 1) % 4

    -- set fan states
    SetCBusState(0, 56, Fan1LowGA, FanCount1 == 1)
    SetCBusState(0, 56, Fan1MedGA, FanCount1 == 2)
    SetCBusState(0, 56, Fan1HighGA, FanCount1 == 3)

    -- reset trigger group back to OFF
    SetCBusLevel(0, 56, 181, 0)
    end
     
    Pie Boy, Apr 16, 2026
    #2
  3. noyzey

    noyzey

    Joined:
    Mar 24, 2010
    Messages:
    48
    Likes Received:
    4
    Location:
    Sunshine Coast, Australia
    Thanks for the suggestion. Unfortuantely this only selects the low fan speed and does nothing each following button press (in bell press mode). When I change the button to on, as I think you suggested, the button is always on and does not reset to off.

    All other parts of my original script works fine, it is only the count function isn't working. It's not adding 1 to the FanCount1 variable on each button press. FanCount1 = FanCount + 1 isn't working.

    The Error log has this error "attempt to perform arithmetic on global 'FanCount1' (a nil value)
    stack traceback:".
     
    Last edited: Apr 16, 2026
    noyzey, Apr 16, 2026
    #3
  4. noyzey

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    257
    Likes Received:
    31
    Location:
    New Zealand
    Ahh yes I see… because thos is event script it won’t remember the variable FanCount1 between each script execution event etc try this
    local FanPress1 = GetCBusLevel(0, 56, 181)

    local Fan1LowGA = 160
    local Fan1MedGA = 159
    local Fan1HighGA = 158

    if FanPress1 == 255 then

    -- get stored value (default to 0 if nil)
    local FanCount1 = storage.get('FanCount1') or 0

    -- increment and wrap 0–3
    FanCount1 = FanCount1 + 1
    if FanCount1 > 4 then
    FanCount1 = 1
    end

    -- set outputs


    -- store updated value
    storage.set('FanCount1', FanCount1)

    -- optional: reset trigger group (only if needed in your setup)
    SetCBusLevel(0, 56, 181, 0)

    end

    I have also done it another way without storage etc but I can’t remember off the top of my head…
     
    Last edited: Apr 16, 2026
    Pie Boy, Apr 16, 2026
    #4
  5. noyzey

    noyzey

    Joined:
    Mar 24, 2010
    Messages:
    48
    Likes Received:
    4
    Location:
    Sunshine Coast, Australia
    Thanks heaps Pie Boy. This is now working
     
    noyzey, Apr 16, 2026
    #5
  6. noyzey

    SgrAystar

    Joined:
    Oct 4, 2018
    Messages:
    85
    Likes Received:
    11
    Location:
    Melbourne, Australia
    Call me paranoid, but I re-order the execution of each of the scenes so that the 'off' groups are processed before 'on' groups.
    An AC2 restart at inopportune moment could theoretically result in executing FanCount1==1 when Fan1MedGA or Fan1HighGA are already on.
     
    SgrAystar, Apr 16, 2026
    #6
  7. noyzey

    noyzey

    Joined:
    Mar 24, 2010
    Messages:
    48
    Likes Received:
    4
    Location:
    Sunshine Coast, Australia
    I hear you. I used to do this too when I was using the Wiser Home Controller. Since the AC2 came along, the commands in a script are implemented within milliseconds. If a relay is already on, the script would have completely run by the time the relay has physically closed. Hence, my lack of concern and preference for readability.
    In this case, it's not a big deal, as for a fraction of a second, there may be a different capacitive value sent to the fan. It's not like a blind motor which could be powered for up and down at the same time. This would need delays to ensure this doesn't happen.
     
    noyzey, Apr 17, 2026
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.