[EXAMPLE] LUA Script examples

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

  1. Damaxx

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,267
    Likes Received:
    130
    Location:
    Adelaide, Australia
    Sorry again. I forgot. Lua is one of those ridiculous languages that are case sensitive. So you have to use true and false in lower case for it to recognise it. Other wise it thinks they are just uninitialized variables.

    Also, as I showed in the example, GetCBusState returns a boolean so comparing it to 255 will always fail.

    try

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

    Also, I assume you are putting this in an event base script for group 14?

    Anyway, I tried it and it works fine now :)
     
    Ashley, Nov 9, 2018
    #21
    1. Advertisements

  2. Damaxx

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    78
    Likes Received:
    1
    Thanks
    got that to work!!!!!!!!!!!
    now will try to get a bit more complex with with LUA
     
    magic8, Nov 9, 2018
    #22
    1. Advertisements

  3. Damaxx

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    78
    Likes Received:
    1
    Getting the idea now
    in piced we used to be able to enable/disable schedules easily
    with the shac do we have to somehow write scripts to be able to do this
    ie to bring a light on at 0700 and off at 1300 if we enable it by setting an enable state on
    thanks
     
    magic8, Nov 13, 2018
    #23
  4. Damaxx

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,267
    Likes Received:
    130
    Location:
    Adelaide, Australia
    You can enable/disable a schedule from the browser, but I don't know anyway of dong it programmatically.

    Unfortunately, the SHAC still misses out on a numbers of basic functions we have all been using for years.
     
    Ashley, Nov 13, 2018
    #24
  5. Damaxx

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    78
    Likes Received:
    1
    Most of our clients like to have an AWAY schedule to be enabled when they are not at home to bring on extra lights so it appears that they are home
    If they request this does that mean we cannot use the shac
    We are about to start another big cbus job for a client that previously had wiser2 and will certainly request this
    A shac would be better as we were hoping to use the RS232 for some RGB.
    Thanks
     
    magic8, Nov 14, 2018
    #25
  6. Damaxx

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    78
    Likes Received:
    1
    can you write a script to bring a light on at a certain time
    Not using schedules
    thanks again
     
    magic8, Nov 14, 2018
    #26
  7. Damaxx

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,267
    Likes Received:
    130
    Location:
    Adelaide, Australia
    One option is to create an Away Scene then set it from a scheduled script that first checks if Away mode is set.
     
    Ashley, Nov 14, 2018
    #27
  8. Damaxx

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    78
    Likes Received:
    1
    How do I get in the away scene different groups coming on at different times?
     
    magic8, Nov 14, 2018
    #28
  9. Damaxx

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,267
    Likes Received:
    130
    Location:
    Adelaide, Australia
    For different times you will need different events in a schedule. What I would do is create a new ga and for each event in the schedule set that ga's value to the ga you want to control (you could use +ve to turn the light on and -ve to turn it off). Then create an event based script that first checks whether the Away flag is set, and if so turn on or off the ga specified by the event ga value. This way you only ever need to edit the event list to change the operation.
     
    Ashley, Nov 15, 2018
    #29
  10. Damaxx

    NickD Moderator

    Joined:
    Nov 1, 2004
    Messages:
    1,418
    Likes Received:
    47
    Location:
    Adelaide
    The way I did it in the CTC I have at home is along the lines of what Ashley is suggesting... use a script which is enabled by or checks the value of an "Away Mode" group to calculate and execute some randomised events at randomised times. Here's the old pascal code, but the idea could be easily adapted to Lua... in this case the module was enabled and disabled by the "Away Mode" variable in the main code....

    Code:
    {Performs random lighting actions while away mode is active to simulate occupied premises.}
    
    {Recalculate random times and groups at 11pm each day}
    {This gives a few different random durations of between 5 and 15 mins each, as well as picking 2 random groups from a selection of 10}
    if (Time = "23:00:00") then
    begin
      RandomTime1 := random ("0:10:00") + "0:05:00";
      RandomTime1a := random ("0:10:00") + "0:05:00";
      RandomTime2 := random ("0:10:00") + "0:05:00";
      RandomTime2a := random ("0:10:00") + "0:05:00";
      RandomTime3 := random ("0:10:00") + "0:05:00";
      RandomGroup1 := random (10);
      RandomGroup2 := random (10);
    end;
    
    case RandomGroup1 of
      0 : Result1 := 02; {"Lounge"}
      1 : Result1 := 06; {"Family"}
      2 : Result1 := 08; {"Retreat"}
      3 : Result1 := 13; {"Bed1"}
      4 : Result1 := 26; {"Bed2"}
      5 : Result1 := 28; {"Bed3"}
      6 : Result1 := 00; {"Study"}
      7 : Result1 := 30; {"Laundry"}
      8 : Result1 := 17; {"Sink Bench"}
      9 : Result1 := 04; {"Dining"}
    end;
    
    case RandomGroup2 of
      0 : Result2 := 02; {"Lounge"}
      1 : Result2 := 06; {"Family"}
      2 : Result2 := 08; {"Retreat"}
      3 : Result2 := 13; {"Bed1"}
      4 : Result2 := 26; {"Bed2"}
      5 : Result2 := 28; {"Bed3"}
      6 : Result2 := 00; {"Study"}
      7 : Result2 := 30; {"Laundry"}
      8 : Result2 := 17; {"Sink Bench"}
      9 : Result2 := 04; {"Dining"}
    end;
    
    {Random light event #1}
    once (Time = "17:00:00" + RandomTime1) then
    begin
      PulseCBusLevel("WIRED", "Lighting", Result1, 100%, "0s", RandomTime1a, 0%);
    end;
    
    {Random light event #2}
    once (Time = "19:30:00" + RandomTime2) then
    begin
      PulseCBusLevel("WIRED", "Lighting", Result2, 100%, "0s", RandomTime2a, 0%);
    end;
    
    {Turn everything off at a randomised time between 10:35 and 10:50}
    once (Time = "22:30:00" + RandomTime3) then
    begin
      SetTriggerLevel("TGGeneral", "All Off");
    end;
    
    {If an outdoor PIR is triggered when the security system is armed, pulse an indoor group that's
     visible from the triggered area to simulate a reaction.}
    once (GetLightingState("NE PIR") = ON) then
    begin
      Delay(5);
      PulseCBusLevel("WIRED", "Lighting", "Dining", 100%, "0s", "00:11:23", 0%);
      Delay(5);
      PulseCBusLevel("WIRED", "Lighting", "Bed 3", 100%, "0s", "00:06:54", 0%);
    end;
    
    once (GetLightingState("NW PIR") = ON) then
    begin
      Delay(5);
      PulseCBusLevel("WIRED", "Lighting", "Study", 100%, "0s", "00:17:21", 0%);
      Delay(10);
      PulseCBusLevel("WIRED", "Lighting", "Bed 2", 100%, "0s", "00:04:42", 0%);
    end;
    
    once (GetLightingState("SW PIR") = ON) then
    begin
      Delay(5);
      PulseCBusLevel("WIRED", "Lighting", "Family", 100%, "0s", "00:20:44", 0%);
      Delay(10);
      PulseCBusLevel("WIRED", "Lighting", "Retreat", 100%, "0s", "00:05:12", 0%);
    end;
    
     
    NickD, Nov 15, 2018
    #30
  11. Damaxx

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    78
    Likes Received:
    1

    Nearly all good with LUA now!!
    The away schedule done as below

    -- works when scheduled time of 121 happens and away selected group 21 follows!!!!!
    value = GetEnableState(3)
    if value == true
    then
    TrackGroup('0', '56', '121', '21')
    end

    Where enable state (3) is away schedule selected and group 121 is a dummy group made up and inserted into a schedule .
    All good now
    Happy with SHAC now although I am sure there is heaps to learn still
     
    magic8, Nov 22, 2018
    #31
  12. Damaxx

    magic8

    Joined:
    Jan 6, 2009
    Messages:
    78
    Likes Received:
    1

    ok nearly there
    Have entered my email details in the common functions similar to wiser2
    It works on wiser2 but comes up with this in SHAC
    email 23.11.2018 15:26:53
    User script:6: attempt to call global 'mail' (a nil value)
    stack traceback:
    User script:6: in main chunk
    email 23.11.2018 15:28:32
    User script:6: attempt to call global 'mail' (a nil value)
    stack traceback:
    User script:6: in main chunk

    Just trying to send an email

    subject = 'yo'
    message = 'ho'
    mail('[email protected]', 'yo', 'ho')

    probably me again
    Thanks
     
    magic8, Nov 23, 2018
    #32
  13. Damaxx

    PMF

    Joined:
    Feb 6, 2019
    Messages:
    16
    Likes Received:
    2
    T

    This is really helpful - I'm a LUA novice - learning fast!! How do I generate the random time intervals in LUA?

    Thanks
     
    PMF, Mar 19, 2019
    #33
  14. Damaxx

    NickD Moderator

    Joined:
    Nov 1, 2004
    Messages:
    1,418
    Likes Received:
    47
    Location:
    Adelaide
    Look through the helper functions.. there is a math.random and math.randomseed
     
    NickD, Mar 21, 2019
    #34
    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.