Converting Pascal code to LUA

Discussion in 'C-Bus Automation Controllers' started by bak26, Apr 5, 2021.

  1. bak26

    bak26

    Joined:
    Mar 11, 2020
    Messages:
    3
    Likes Received:
    0
    I am having trouble replicating some simple code I used in a PACA to work in the SHAC. It is to do with turning a fan on after 1 minute of a light being turned on. When the light is turned off and the timer is greater than 90 mins then keep the fan running for a further 5 mins.
    Here is the code I have for it. If anyone has a better way to do it in the SHAC it would be much appreciated.

    once GetLightingState("Powder Room") = ON then
    TimerStart(2); { light has gone on }

    once TimerTime(2) = 60 then
    SetLightingState("Powder Room Fan", ON); { switch on fan }

    once GetLightingState("Powder Room") = OFF then
    begin { light has gone off }
    if TimerTime(2) > 90 then
    delay(300);
    SetLightingState("Powder Room Fan", OFF); { switch off fan }
    TimerStop(2);
    end;
     
    bak26, Apr 5, 2021
    #1
    1. Advertisements

  2. bak26

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    149
    Likes Received:
    23
    Location:
    New Zealand
    There's no simple way to achieve this on the SHAC like we do it in piced with logic timers, the simplest way i can think of might get you started as below,

    An Event script on the powder room ga to store os.time into a user parameter, we will need to make these (called "stored_time1" & "stored_time2" )

    --event script for "Powder Room" group address
    local level = event.getvalue()
    if level == 255 then
    SetUserParam(0, 'stored_time1', os.time())
    log('st1 stored at '.. os.date("%H:%M:%S"))
    end

    if level == 0 then
    SetUserParam(0, 'stored_time2', os.time())
    log('st2 stored at '.. os.date("%H:%M:%S"))
    end

    Then resident script with delay (might take a bit of tweaking id start with a 20 sec delay) to check if time has lapsed
    --resident script
    local st1 = GetUserParam(0, 'stored_time1')
    local st2 = GetUserParam(0, 'stored_time2')

    if (os.time() - st1) > 60 then
    log('timer 1 lapsed at '.. os.date("%H:%M:%S"))
    SetLightingState("Powder Room Fan", true); { switch on fan }
    end

    if (os.time() - st2) > 90 then
    log('timer 2 lapsed at '.. os.date("%H:%M:%S")')
    SetLightingState("Powder Room Fan", false); { switch off fan }
    end
     
    Last edited: Apr 5, 2021
    Pie Boy, Apr 5, 2021
    #2
    1. Advertisements

  3. bak26

    bak26

    Joined:
    Mar 11, 2020
    Messages:
    3
    Likes Received:
    0
    Thanks Pie Boy I will have a play with it and see how it goes.
     
    bak26, Apr 5, 2021
    #3
  4. bak26

    bak26

    Joined:
    Mar 11, 2020
    Messages:
    3
    Likes Received:
    0
    Can't seem to get the Resident script to work. Here is the error code.

    Powder Room Fan 11.04.2021 16:04:12
    Resident script:4: attempt to perform arithmetic on local 'st1' (a table value)
    stack traceback:

    How should the stored_time1 etc be setup?
    I have set them up as User Parameters in Objects and Data type (time/day). Is this correct?
     
    bak26, Apr 11, 2021 at 5:13 AM
    #4
  5. bak26

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    149
    Likes Received:
    23
    Location:
    New Zealand
    Use data type Unsigned integer (32 bit) for the user parameter/s
     
    Pie Boy, Apr 11, 2021 at 7:53 AM
    #5
    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.