[EXAMPLE] LUA Script examples

Discussion in 'C-Bus Automation Controllers' started by Damaxx, Sep 25, 2017.

  1. Damaxx

    Damaxx

    Joined:
    May 12, 2008
    Messages:
    229
    Likes Received:
    47
    Just wanted to share a couple of LUA scripts that I have done - nothing ground breaking and I am far from a coding guru, but thought it might help anyone else starting out with example of code and syntax and maybe some ideas on what can be done.

    First one is a roller door sensor - I have it show change a pseudo group percentage to change the LED colour on an ELDT in the house to indicate the roller door is open, then connect to a web socket on a raspberry pi which sends me a push notification to my phone and then turns on a ultrasonic range finder and display for 2 minutes.

    This went under the 'event-based' scripts
    Code:
    [COLOR="blue"]rollerdoor = GetCBusState(0, 56, 199)
    log(rollerdoor)
    
    if rollerdoor == false then[/COLOR]
      [COLOR="Orange"]-- Set local wirednetwork , lighting, rollerdoor indicator on EDLT to 20 ,instantly).[/COLOR] 
    	[COLOR="blue"]SetCBusLevel(0, 56, 197, 20, 0)[/COLOR]
      
     [COLOR="orange"] -- Send Http Get to Node-red to trigger push notification[/COLOR]
     [COLOR="blue"] http = require'socket.http'
      body = http.request("http://***.***.***.***:1234/rollerdoor")
      log(body)  
      [/COLOR]
    [COLOR="orange"] -- Set local, Lighting application, rangefinder to ,255 ,instantly and after 120 seconds and reset to off.[/COLOR]
    	[COLOR="blue"]PulseCBusLevel(0, 56, 9, 255, 0, 120, 0)[/COLOR]
    
    [COLOR="Blue"]else if rollerdoor == true then[/COLOR]
    [COLOR="orange"] -- Set local wirednetwork , lighting, rollerdoor indicator on EDLT, 200  instantly).[/COLOR]
    	[COLOR="blue"]SetCBusLevel(0, 56, 197, 200, 0)  
       end
    end[/COLOR]
    
     
    Damaxx, Sep 25, 2017
    #1
    ampcom likes this.
  2. Damaxx

    Damaxx

    Joined:
    May 12, 2008
    Messages:
    229
    Likes Received:
    47
    Next one is for my Ness IP doorbell. Ness has provided a 3rd part interface which when the door bell is pressed, will send an ASCII message to a specified IP address and port.
    The code below opens a web socket on the automation controller (socket.bind should point to the same IP address as your automation controller and select the port of your choice)
    When it receives a message, it activates a Cbus group address for couple of seconds. This one went under Resident scripts with a sleep interval of 0, so constantly running.
    Code:
    [COLOR="Orange"]-- load socket[/COLOR]
    [COLOR="Blue"]local socket = require("socket")[/COLOR]
    [COLOR="orange"]-- create a TCP socket and bind it to the local host, at any port[/COLOR][COLOR="blue"] local server = assert ((socket.bind"IP address of the automation controller", port of your choice))
    	local ip, port = server:getsockname()[/COLOR]
    	[COLOR="orange"]-- print a message informing what's up[/COLOR]
    	[COLOR="Blue"]log("Please connect to localhost on port " .. port)
     [COLOR="orange"]-- loop[/COLOR] forever waiting for clients[/COLOR]
    [COLOR="Blue"]while 1 do[/COLOR]
    [COLOR="orange"]  -- wait for a connection from any client[/COLOR]
    [COLOR="blue"]  local client = server:accept()[/COLOR]
    [COLOR="orange"]  -- make sure we don't block waiting for this client's line[/COLOR]
    [COLOR="blue"]  client:settimeout(5)[/COLOR]
    [COLOR="orange"]  -- receive the line[/COLOR]
    [COLOR="blue"]  local line, err = client:receive()
      if line ~=""
      then[/COLOR]
    [COLOR="Orange"] -- activate door bell via MP3 player[/COLOR]
     [COLOR="orange"] --  client:close()[/COLOR]
     [COLOR="Blue"]   log (line .. "Someone at the door")
        SetCBusState(0, 56, 213, true)
        socket.sleep(2)
        SetCBusState(0, 56, 213, false)
        end 
    end [/COLOR]
    
    
     
    Damaxx, Sep 25, 2017
    #2
    ampcom and smashedpeas like this.
  3. Damaxx

    smashedpeas

    Joined:
    Feb 8, 2012
    Messages:
    9
    Likes Received:
    0
    Location:
    Melbourne, Australia
    Hey Damaxx,
    Thanks for the examples they have answered a lot of questions for me.
    is this on a 5500SHAC wiser and is there a language reference specifically for c-bus? I've been trying to find a document that has all the c-bus specific functions (like GetCBusState).

    I like the idea of changing the colour of the edlt I was trying to think of some sort of alert for that myself. I have an RPI listening on a webport that is connected via the gpio to a hacked wireless remote to operate the roller door. It looks like I can make http call to it straight from the script to operate the door which is exciting.
     
    smashedpeas, Nov 6, 2017
    #3
  4. Damaxx

    Damaxx

    Joined:
    May 12, 2008
    Messages:
    229
    Likes Received:
    47
    Glad they were of some help smashedpeas. Yes it is a 5500SHAC and I couldn't find a programming guide, not that I searched for too long, but found that the helpers for common used scripts like GetCBusState were prefilled enough for me to get the gist. There is a fair bit of info in the manual too -
    Glad they were of some help smashedpeas. Yes it is a 5500SHAC and I couldn't find a programming guide, not that I searched for too long, but found that the helpers for common used scripts like GetCBusState were prefilled enough for me to get the gist. There is a fair bit of info in the manual http://203.41.170.20/downloads/AutoController/5500NAC_5500SHAC_FW_v1.0_EN.pdf
     
    Damaxx, Nov 6, 2017
    #4
  5. Damaxx

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,548
    Likes Received:
    178
    Location:
    Adelaide, Australia
    The 5500SHAC has built in help on the scripting page. In the right hand panel under Select Topic just click on C-bus Functions to get a list, and on any function to get details.
     
    Ashley, Nov 6, 2017
    #5
    MarkB likes this.
  6. Damaxx

    smashedpeas

    Joined:
    Feb 8, 2012
    Messages:
    9
    Likes Received:
    0
    Location:
    Melbourne, Australia
    Thanks for the info Gents, I was after the info to determine if I should order one but Damaxx examples have got me over the line and have one on order.
     
    smashedpeas, Nov 8, 2017
    #6
  7. Damaxx

    jako

    Joined:
    Jan 31, 2005
    Messages:
    32
    Likes Received:
    0
    Thanks Damax for your Script. Do you have the model number to the Ness IP doorbell.
     
    jako, Apr 17, 2018
    #7
  8. Damaxx

    Damaxx

    Joined:
    May 12, 2008
    Messages:
    229
    Likes Received:
    47
    Damaxx, Apr 17, 2018
    #8
  9. Damaxx

    Hakka

    Joined:
    May 17, 2018
    Messages:
    5
    Likes Received:
    5
    Here's a couple.. Just posting to share as I would like to see more from others and even the Clipsal guys. There could be more documentation on what's and how. I'm sure they have some tricks etc.

    This one is a resident one that I run every few seconds or so. It simply allows you to automatically turn off anything that is included in the list. Most useful to stop repeating script code when you have more than a few lights or whatever.. Will post supporting scripts from other events and libs etc.

    require('user.config')

    function autoOffPollAll()
    autoOffPoll('Garage')
    autoOffPoll('Outside')
    autoOffPoll('Laundry')
    autoOffPoll('Rumpus')
    end

    function autoOffPoll(groupName)
    if (isAutoOffEnabled(groupName)) then
    t = getOnTime(groupName)
    if (t ~= 0) then
    if (os.time() > t+getAutoOffTime(groupName)) then
    setOnTime(groupName, 0)
    if (isRestoreStateRequired(groupName)) then
    alert('Restoring ' .. groupName .. ': ' .. getRestoreStateValue(groupName))
    SetLightingLevel(groupName, getRestoreStateValue(groupName), 0)
    else
    --alert('Turning off ' .. groupName)
    SetLightingLevel(groupName, 0, 0)
    end
    end
    end
    end
    end

    autoOffPollAll()
     
    Hakka, May 17, 2018
    #9
    MarkB and ampcom like this.
  10. Damaxx

    Hakka

    Joined:
    May 17, 2018
    Messages:
    5
    Likes Received:
    5
    This is a user script that I call configInit() from an init script to set some defaults. It also has some supporting functions for the auto off stuff posted earlier. Basically you just setup some variables for each light etc. that you want to put into the auto off stuff.

    function configInit()
    -- Setup some default auto off times etc.
    storage.set('GarageAutoOffTime', 5*60)
    storage.set('GarageAutoOffEnabled', true)
    storage.set('GarageRestoreStateRequired', false)

    storage.set('OutsideAutoOffTime', 10*60)
    storage.set('OutsideAutoOffEnabled', true)
    storage.set('OutsideRestoreStateRequired', false)


    storage.set('LaundryAutoOffTime', 10*60)
    storage.set('LaundryAutoOffEnabled', true)
    storage.set('LaundryRestoreStateRequired', false)


    storage.set('RumpusAutoOffTime', 60*60)
    storage.set('RumpusAutoOffEnabled', true)
    storage.set('RumpusRestoreStateRequired', false)


    -- Setup some default times
    storage.set('DuskTime', 1615)
    storage.set('DawnTime', 630)
    end

    function isAutoOffEnabled(groupName)
    str = groupName .. 'AutoOffEnabled'
    return storage.get(str, false)
    end

    function getAutoOffTime(groupName)
    str = groupName .. 'AutoOffTime'
    return storage.get(str, 0)
    end

    function setOnTime(groupName, onTime)
    storage.set(groupName .. 'OnTime', onTime)
    end

    function getOnTime(groupName)
    return storage.get(groupName .. 'OnTime', 0)
    end

    function isRestoreStateRequired(groupName)
    return storage.get(groupName .. 'RestoreStateRequired', false)
    end

    function getRestoreStateValue(groupName)
    return storage.get(groupName .. 'RestoreStateValue', 0)
    end

    function setRestoreStateValue(groupName, value)
    storage.set(groupName .. 'RestoreStateValue', value)
    end

    function getDuskTime()
    return storage.get('DuskTime')
    end

    function getDawnTime()
    return storage.get('DawnTime')
    end
     
    Hakka, May 17, 2018
    #10
    ampcom likes this.
  11. Damaxx

    Hakka

    Joined:
    May 17, 2018
    Messages:
    5
    Likes Received:
    5
    This is an event script. One needed for each group event in order to set and reset the timer variables.

    -- Outside selector event
    value = event.getvalue()
    SetLightingLevel('Outdoor FR', value, 0)
    SetLightingLevel('Outdoor FL', value, 0)

    if (value == 0) then
    --alert('Outside lights off event')
    setOnTime('Outside', 0)
    else
    --alert('Outside lights on event')
    setOnTime('Outside', os.time())
    end
     
    Hakka, May 17, 2018
    #11
    ampcom likes this.
  12. Damaxx

    Hakka

    Joined:
    May 17, 2018
    Messages:
    5
    Likes Received:
    5
    If anyone needs some info on how it works or some help I am happy to help.

    Hopefully others will post some useful stuff as this is a really cool product and very now and then I think of cool things I can do with it and interfacing it to other gear.
     
    Hakka, May 17, 2018
    #12
    ampcom likes this.
  13. Damaxx

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    97
    Likes Received:
    2
    Really struggling with basic LUA scripts.
    Can anyone show me how this should be written to work
    I know I am a real LUA newbie
    I hope someone will at least understand what i am trying to do
    Thanks

    value = GetCBusState(0, 56, 12)
    if (0 ,56, 12,) ==255
    then
    TrackGroup(0, 56, 69, 68)
    else
    SetCBusState(0, 56, 68, false)
    end
     
    magic8, Nov 8, 2018
    #13
  14. Damaxx

    Hakka

    Joined:
    May 17, 2018
    Messages:
    5
    Likes Received:
    5
    Try this:

    value = GetCBusState(0, 56, 12)
    if value == 255
    ...

    OR

    if GetCBusState(0, 56, 12) == 255
    ...
     
    Hakka, Nov 8, 2018
    #14
  15. Damaxx

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    97
    Likes Received:
    2
    value = GetCBusState(0, 56, 12)
    if value == 255
    then
    TrackGroup(0, 56, 69, 68)
    else
    SetCBusState(0, 56, 68, false)
    end


    if GetCBusState(0, 56, 12) == 255
    then
    TrackGroup(0, 56, 69, 68)
    else
    SetCBusState(0, 56, 68, false)
    end

    Tried both of these and group 68 tracks 69 all the time irrespective of the state of group 12
    I must be doing some thing wrong
    Shows no errors but i must be doing something wrong
    All else with shac is good
    Thanks
     
    magic8, Nov 8, 2018
    #15
  16. Damaxx

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    97
    Likes Received:
    2
    I have now deleted all other scripts and cant get this to work

    GetCBusState(0, 56, 12)
    if value == 255
    then
    -- Set lighting level of group 1 to 128 with ramp rate 4
    SetLightingLevel(69, 128, 4)

    end

    Anybody able to help?
    Thanks
     
    magic8, Nov 8, 2018
    #16
  17. Damaxx

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,548
    Likes Received:
    178
    Location:
    Adelaide, Australia
    You either need:

    if GetCBusState(0, 56, 12) == 512 then ...

    or

    value = GetCBusState(0, 56, 12)
    if value == 255 then ....
     
    Ashley, Nov 8, 2018
    #17
  18. Damaxx

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    97
    Likes Received:
    2
    Tried this and when i switch on group 14 nothing happens to group 69
    Must be doing something wrong!!!!!!!

    if GetCBusState(0, 56, 14) == 512 then
    SetLightingState(69, 255)
    end
     
    magic8, Nov 9, 2018
    #18
  19. Damaxx

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,548
    Likes Received:
    178
    Location:
    Adelaide, Australia
    Oops, sorry, should have read your code properly.

    GetCBUSState return a Boolean value (i.e. True or False), not a value.

    So just go

    if GetCBusState(0, 56, 14) then SetLightingState(69, TRUE)

    From memory in Lua True returns a 1 and False a Nil. If you test a number, nil return false and any value returns True.
     
    Ashley, Nov 9, 2018
    #19
  20. Damaxx

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    97
    Likes Received:
    2
    still no good
    think something else is amiss
    Here is code tried and error

    if GetCBusState(0, 56, 14) == 512
    then
    SetLightingState(69, TRUE)
    end

    Library cbuslogic:0: state variable must be a boolean
    stack traceback:
    [C]: in function 'error'
    Library cbuslogic: in function '_ParseCBusState'
    Library cbuslogic: in function 'SetCBusState'
    Library cbuslogic: in function 'SetLightingState'
    User script:2: in main chunk
     
    magic8, Nov 9, 2018
    #20
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.