[EXAMPLE] LUA Script examples

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

  1. Damaxx

    Damaxx

    Joined:
    May 12, 2008
    Messages:
    157
    Likes Received:
    12
    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.
    1. Advertisements

  2. Damaxx

    Damaxx

    Joined:
    May 12, 2008
    Messages:
    157
    Likes Received:
    12
    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.
    1. Advertisements

  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:
    157
    Likes Received:
    12
    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:
    768
    Likes Received:
    28
    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
  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:
    26
    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:
    157
    Likes Received:
    12
    Damaxx, Apr 17, 2018
    #8
  9. Damaxx

    Hakka

    Joined:
    May 17, 2018
    Messages:
    4
    Likes Received:
    4
    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
    ampcom likes this.
  10. Damaxx

    Hakka

    Joined:
    May 17, 2018
    Messages:
    4
    Likes Received:
    4
    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:
    4
    Likes Received:
    4
    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:
    4
    Likes Received:
    4
    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.
    1. Advertisements

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.