Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

[solved] LUA script elseif is never triggered


Guest b95_lun

Question

Guest b95_lun

I have created a script to turn on a dimmer if movement is detected by PIR. The light should be turned on for 5 minutes after detected movement and if new movement detected during this period the coundown timer should restart at 5 minutes.

I two issues

1. Aeon-Labs PIR is set for a timeout period of 2 minues before OFF state is sent after being triggered (Parameter 3 = 120). This is very random in HC2 sometimes it takes 2 minutes but quite often it takes longer time.

2. In the script the elseif statement is never triggered. I want it to be triggered if an instance of the scene is already running (i.e. One while loop for the timer is running) and then I just want to set the timer back to 5 minutes and end the last instance. I want to have the first instance as the only controller of the timer.

Could anyone have a look on my script to see if I have made some misstake in the lua code?

Please login or register to see this code.

Here is the debug from it and see that a new movement is triggered at 08:41:48 but it never enters the elseif.

As well the last fibaro:debug statement is never executed

[DEBUG] 08:39:24: start, timer = 0 lights = 0 ,triggerValue = 0

[DEBUG] 08:39:36: start, timer = 0 lights = 0 ,triggerValue = 1

[DEBUG] 08:39:36: Loop - timer = 4

[DEBUG] 08:40:36: Loop - timer = 3

[DEBUG] 08:41:35: start, timer = 3 lights = 20 ,triggerValue = 0

[DEBUG] 08:41:36: Loop - timer = 2

[DEBUG] 08:41:48: start, timer = 2 lights = 20 ,triggerValue = 1

[DEBUG] 08:42:36: Loop - timer = 1

[DEBUG] 08:43:36: Loop - timer = 0

[DEBUG] 08:44:59: start, timer = 0 lights = 0 ,triggerValue = 0

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0
Guest jrkalf
  • Inquirer
  • Don't run a script with if and elseif

    You have to run the check for seeing if this script is already running in the IF and use the ELSEIF for the actual script you're running.

    Elseif is only triggered when the IF does not apply.

    Link to comment
    Share on other sites

    • 0
    Guest b95_lun
  • Inquirer
  • I finaly manage to find the mistake in my code.

    Thanks for you reply jrkalf but the issue was a misstake in the lua code. I have spent a couple of hours to find the issue.

    I hade used "." instead of ":" in the function fibaro:countScenes(sceneId)

    In my bad code I wrote

    Please login or register to see this code.

    and it should be

    Please login or register to see this code.

    I never got any error messages it just exited the scene

    Link to comment
    Share on other sites

    • 0
    I hade used "." instead of ":" in the function fibaro:countScenes(sceneId)

    ":" access instance function (you can use .self in the function -> this.) ex: in accessor functions (getters and setters). in clear if you have a member linked with the instance only function call with ":" can read the value... and Inside this function you use "." to call private members.

    See OOP (oriented object programming concepts).

    Link to comment
    Share on other sites

    • 0

    Hello,

    Many thanks for the scene - I was going insane trying to figure this out..

    But can you please add few lines (i'm not good with LUA).

    The scene should start ONLY when lights 199 and 146 (my ID-s) are turned off. I do not want to turn lights on 100% if the ceiling light is dimmed to 30% maybe or wall light is turned on already.

    Thank you in advance!

    Edit: It seems I do not understand the variable concept in this scene.

    local timer = "Variable_name" --Name of global variabel to control the timer <- What this should be.

    Scene works somehow - If light has been on for 4,5 minutes and movement is detected, then the light will work for 60seconds more - it doesnt loop the scene.

    --[[

    %% properties

    199 value

    %% globals

    --]]

    --199 = PIR for movement detection

    --Scene purpose

    --IF PIR detects movement then turn on dimmer and turn it off 5 minutes.

    -- If new movement detected during the 5 minute period set the timer for 5 minutes

    local sceneId = 57 --ID of this scene, used to detect number of instances run

    local timerEndTime = 5 --Minutes to keep the lamp turned on after PIR detects movement

    local timer = "Aeg" --Name of global variabel to control the timer

    local timerValue = fibaro:getGlobalValue(timer) --Current value of global variabel for timer

    local triggerValue = fibaro:getValue(199, "value") --Get PIR value that activated this scene

    local lights = 204 --dimmer to be controlled by this scene

    local dimmerValue = "70" --Ljusstyrka

    fibaro:debug("start, timer = " .. timerValue .. " lights = " .. fibaro:getValue(lights, "value") .. " ,triggerValue = " .. triggerValue)

    if (fibaro:getValue(lights, "value") == "0" and triggerValue == "1") then

    --If starts if lights off and PIR detected movement

    fibaro:call(lights, "setValue", dimmerValue) --Turn dimmer on

    fibaro:setGlobal(timer, timerEndTime) --Set global variabel for timer

    while (fibaro:getGlobalValue(timer) ~= "0") do

    --Loop used to control timer, each loop 1 minute and continues

    --as long as timer variabel isn't 0

    timerValue = tonumber(fibaro:getGlobalValue(timer)) --Get current value of global variabel timer

    fibaro:setGlobal(timer, timerValue-1) --Decrease global variabel for timer with 1 minute

    fibaro:debug("Loop - timer = " .. fibaro:getGlobalValue(timer))

    fibaro:sleep(60*1000) --Wait 1 minute

    end

    fibaro:call(lights, "turnOff") --Turn dimmer off

    elseif fibaro.countScenes(sceneId) > 1 then

    --if scene is already running the set timer to 5 minutes

    fibaro:setGlobal(timer, timerEndTime) --set global variabel for timer to 5 minutes

    fibaro:debug("New instance, totalt = " .. fibaro.countScenes(sceneId))

    end

    fibaro:debug("End, instances = " .. fibaro:countScenes(sceneId))

    fibaro:debug("End, instances = " .. fibaro:countScenes(sceneId))

    Alusta

    Peata

    Tühista

    [DEBUG] 22:52:14: start, timer = 0 lights = 0 ,triggerValue = 1

    [DEBUG] 22:52:14: Loop - timer = 4

    [DEBUG] 22:52:16: start, timer = 4 lights = 70 ,triggerValue = 0

    [DEBUG] 22:52:29: start, timer = 4 lights = 70 ,triggerValue = 1

    [DEBUG] 22:52:31: start, timer = 4 lights = 70 ,triggerValue = 0

    [DEBUG] 22:53:14: Loop - timer = 3

    [DEBUG] 22:54:14: Loop - timer = 2

    [DEBUG] 22:55:14: Loop - timer = 1

    [DEBUG] 22:56:14: Loop - timer = 0

    [DEBUG] 22:57:14: End, instances = 1

    [DEBUG] 22:57:29: start, timer = 0 lights = 0 ,triggerValue = 1

    [DEBUG] 22:57:29: Loop - timer = 4

    [DEBUG] 22:57:32: start, timer = 4 lights = 70 ,triggerValue = 0

    [DEBUG] 22:57:43: start, timer = 4 lights = 70 ,triggerValue = 1

    [DEBUG] 22:57:46: start, timer = 4 lights = 70 ,triggerValue = 0

    [DEBUG] 22:58:00: start, timer = 4 lights = 70 ,triggerValue = 1

    [DEBUG] 22:58:02: start, timer = 4 lights = 70 ,triggerValue = 0

    [DEBUG] 22:58:24: start, timer = 4 lights = 70 ,triggerValue = 1

    [DEBUG] 22:58:26: start, timer = 4 lights = 70 ,triggerValue = 0

    [DEBUG] 22:58:29: Loop - timer = 3

    [DEBUG] 22:58:40: start, timer = 3 lights = 70 ,triggerValue = 1

    [DEBUG] 22:58:42: start, timer = 3 lights = 70 ,triggerValue = 0

    [DEBUG] 22:59:26: start, timer = 3 lights = 70 ,triggerValue = 1

    [DEBUG] 22:59:29: start, timer = 3 lights = 70 ,triggerValue = 0

    [DEBUG] 22:59:29: Loop - timer = 2

    [DEBUG] 23:00:29: Loop - timer = 1

    [DEBUG] 23:01:00: start, timer = 1 lights = 70 ,triggerValue = 1

    [DEBUG] 23:01:02: start, timer = 1 lights = 70 ,triggerValue = 0

    [DEBUG] 23:01:15: start, timer = 1 lights = 70 ,triggerValue = 1

    [DEBUG] 23:01:17: start, timer = 1 lights = 70 ,triggerValue = 0

    [DEBUG] 23:01:29: Loop - timer = 0

    [DEBUG] 23:01:41: start, timer = 0 lights = 70 ,triggerValue = 1

    [DEBUG] 23:01:44: start, timer = 0 lights = 70 ,triggerValue = 0

    [DEBUG] 23:02:11: start, timer = 0 lights = 70 ,triggerValue = 1

    [DEBUG] 23:02:13: start, timer = 0 lights = 70 ,triggerValue = 0

    [DEBUG] 23:02:28: start, timer = 0 lights = 70 ,triggerValue = 1

    [DEBUG] 23:02:29: End, instances = 1

    [DEBUG] 23:02:31: start, timer = 0 lights = 0 ,triggerValue = 0

    [DEBUG] 23:02:44: start, timer = 0 lights = 0 ,triggerValue = 1

    [DEBUG] 23:02:44: Loop - timer = 4

    [DEBUG] 23:02:46: start, timer = 4 lights = 70 ,triggerValue = 0

    [DEBUG] 23:02:58: start, timer = 4 lights = 70 ,triggerValue = 1

    [DEBUG] 23:03:00: start, timer = 4 lights = 70 ,triggerValue = 0

    [DEBUG] 23:03:11: start, timer = 4 lights = 70 ,triggerValue = 1

    [DEBUG] 23:03:14: start, timer = 4 lights = 70 ,triggerValue = 0

    [DEBUG] 23:03:44: Loop - timer = 3

    [DEBUG] 23:04:29: start, timer = 3 lights = 70 ,triggerValue = 1

    [DEBUG] 23:04:31: start, timer = 3 lights = 70 ,triggerValue = 0

    [DEBUG] 23:04:43: start, timer = 3 lights = 70 ,triggerValue = 1

    [DEBUG] 23:04:44: Loop - timer = 2

    [DEBUG] 23:04:45: start, timer = 2 lights = 70 ,triggerValue = 0

    [DEBUG] 23:04:58: start, timer = 2 lights = 70 ,triggerValue = 1

    [DEBUG] 23:05:01: start, timer = 2 lights = 70 ,triggerValue = 0

    EDIT2: NOT working still - went to shower and lights went off - I saw PIR sensor reacting many times during my presence but the timer was not restarted.. Help`?

    Link to comment
    Share on other sites

    • 0

    try this code. A bit different aproach but tested and working. Have to adapt to your needs and devices:

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0

    Great, got it working, thanks alot!

    Link to comment
    Share on other sites

    • 0
    Guest dsilletti
  • Inquirer
  • Great, got it working, thanks alot!

    Hi Lauri, I'd like to use the same scene for my bathrooms, no more light switch off in the worst moments

    Please login or register to see this image.

    /emoticons/default_icon_mrgreen.gif" alt=":mrgreen:" />

    can you please share the final working code?

    Thank you in advance

    Domenico

    Link to comment
    Share on other sites

    • 0

    Hi

    This also working

    --[[

    %% properties

    xx value -- in this place please set ID "xx" your motion sensor

    %% globals

    --]]

    local counter = 1800; ------------- this is time 30 min

    local startSource = fibaro:getSourceTrigger();

    if (

    ( tonumber(fibaro:getValue(xx, "value")) > 0 ) -- in this please please set ID "xx" yuor motion sensor

    or

    startSource["type"] == "other"

    )

    then

    fibaro:call(yy, "turnOn"); -- in this please you have set ID "yy" your Relay Switch module, which connect the lamp

    while(counter > 0)

    do

    counter=counter-1;

    fibaro:sleep(1000);

    if

    tonumber(fibaro:getValue(xx, "value")) > 0 -- in this please please set ID "xx" your motion sensor

    then

    counter = 1800;

    end

    end

    fibaro:call(yy, "turnOff"); -- in this please please set ID "yy" your Relay switch, which connect the lamp

    end

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...