Lua Fan Controller Scripts

Discussion in 'C-Bus Automation Controllers' started by noyzey, Apr 16, 2026 at 1:21 AM.

  1. noyzey

    noyzey

    Joined:
    Mar 24, 2010
    Messages:
    47
    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 at 1:21 AM
    #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 at 1:40 AM
    #2
  3. noyzey

    noyzey

    Joined:
    Mar 24, 2010
    Messages:
    47
    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 at 2:56 AM
    noyzey, Apr 16, 2026 at 1:53 AM
    #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 at 3:00 AM
    Pie Boy, Apr 16, 2026 at 2:54 AM
    #4
  5. noyzey

    noyzey

    Joined:
    Mar 24, 2010
    Messages:
    47
    Likes Received:
    4
    Location:
    Sunshine Coast, Australia
    Thanks heaps Pie Boy. This is now working
     
    noyzey, Apr 16, 2026 at 3:02 AM
    #5
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.