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


  • 1

Need script/scene to keep lights on, extend or restart the original safe period if motion sensor is breached


Question

Posted (edited)

Hi,

 

I am new to Fibaro Home Center and LUA scripts.

 
I need a script, or block scene(s) if possible, to do the following:
 
Any of three Fibaro motion sensors are to turn lights on (plugs) when breached. The lights are to stay on for X seconds. If any of the three sensors are breached during this period, the original timer/countdown are to start over again (“safe” period is to start over again). If none of the sensors are breached during the safe period/the countdown, the lights turn off. The problem is that the Home Center 2 creates new instances of scenes instead of starting them over again when the sensors are breached, as far as I can see.
 
I would be very great full if someone would share such a LUA script or create one, must also be others that need this.
 
 
Edited by Fiberik

Recommended Posts

  • 0
  • Inquirer
  • Posted

    Thanks, again! Everything is working now. 

    • 0
  • Inquirer
  • Posted

    This script is serving me well, but on some occasions it has stopped running, it kind of “freezes”. 

     

    I believed it happened when some of the lights did not turn on immediately. 

     

    Last night all of the lights were on when I left the room/area,  but the lights were not turned off after the “sleeptime” ended. They were already on when I entered the room/area this morning. (See the debug info attached, it is the last debug info from yesterday.). 

     

    Any idea on what is causing it and how I can solve this? 

    Please login or register to see this attachment.

    • 0
    Posted

    Long shot but try to check the box "Do not allow alarm to stop scene while alarm is running" in the first tab of the scene interface on the HC2.

    Can you post the whole script as it looks now?

    • 0
  • Inquirer
  • Posted

    Please login or register to see this code.

     

    • 0
    Posted
    2 hours ago, Fiberik said:

    Please login or register to see this code.

     

    So, the script will always end up at the bottom half, turning off lights when all sensors report that they are safe.

    Your log indicates that it dies while sleeping after having printed "No one moving..." (btw, you have a sleep time of 1 hour, but I guess that's intentional?)

    Did you try checking the box "Do not allow alarm to stop scene while alarm is running" ?

    • 0
  • Inquirer
  • Posted

    The sleeptime is intentional. This is the script as it was at the time of the debug info. I will try checking the box “Do not allow alarm to stop scene ...”. 

    • 0
    Posted (edited)
    On 6/1/2018 at 10:36 AM, jgab said:

    Ok, here is a more generalized version that checks many sensor and turn off/on several lights.

    Add light IDs and sensor IDs to the local tables in the beginning of the script.

    Please login or register to see this code.

    A side note just to emphasize how tricky this seemingly easy scene is: This scene actually leverages the fact that multiple instances are started when triggers change state. When all sensors are safe it starts a scene instance that sleeps waiting for the time to expire so that it can turn off the lights. If a sensor is breached during that time, a new scene instance is started that kill all instances, itself and the 'sleeping' instance. Then later when all sensors turn safe again a new 'sleeping' instance is started.

    So the scene is toggling between two states, sleeping and killing, and there can never be two sleeping instances at the same time. 

    This is important because if one needs to add extra conditions, like do not turn on lights if global variable  "onlyDogIsHome" is true, I would recommend that it would be a test before the call to the "switch" command

    Please login or register to see this code.

    ...and leave the rest of the logic intact. If that test is done in the beginning of the scene and just aborts the scene it's easy to "get out of sync" with the state changes, and maybe end up with two sleeping instances (if that global change value) etc. So it looks simple but is tricky to generalize.

    I often see "looping and sleeping" scenes with extra tests thrown in (and sometimes a 'fibaro:countScenes' for an extra "safety" measure) and I get dizzy trying to understand the (mostly unintended)  instance interaction going on -- and there is no wonder that once in a while (that can seem random because it's usually timing dependent) there is unintended consequences and people suspect gremlins in their HC2s... :-) 

    (Btw, It would be cool if scene instances could share Lua global vars (_ENV) between them... would allow for some powerful syncing between instances, but of course would be even more tricky to to get right... )

     

     

    Very nice @jgab, i,ve gone past this a few times in the past , getting to a point now where i finally understand it. Very elegant. Precise and  smart.  Thanks for your invaluable contributions.

     

     

    Edited by MARCUSP.I.L
    • 0
    Posted
    On 5/8/2019 at 4:53 AM, MARCUSP.I.L said:

     

     

    Very nice @jgab, i,ve gone past this a few times in the past , getting to a point now where i finally understand it. Very elegant. Precise and  smart.  Thanks for your invaluable contributions.

     

     

     

    The "killScences" approach is one way to do it that makes the scene compact. Some people in the forum has reported issues with killScenes (stalling the box), but it seems to vary. Myself haven't noted any issues.

    • 0
    Posted

    I wonder why none of you use the %%killOtherInstances directive in the header of the scene. I set maxRunningInstances to 1 and use this option. This way always just one instance (the last) of the scene is running. Works fine for me.

    • 0
    Posted
    18 minutes ago, Alex de Bruin said:

    I wonder why none of you use the %%killOtherInstances directive in the header of the scene. I set maxRunningInstances to 1 and use this option. This way always just one instance (the last) of the scene is running. Works fine for me.

    Yes it works too. The logic becomes similar but kind of the inverse (test if all sensors are safe and then sleep).

    I think the reason we didn't use it here is that the origin of this script (before this thread) was from back when %%killOtherInstances was not that well known.

    You don't need to set maxRunningInstances to 1.

    • 0
    Posted
    1 hour ago, jgab said:

    You don't need to set maxRunningInstances to 1.

     

    I know that, but is just to prove that a setting of 1 indeed is enough.

    • 0
    Posted

    This script is serving me well, and i've made it a bit shorter:

     

    --[[
    %% properties
    288 value
    196 value
    %% events
    %% globals
    --]]

    local lightID = 288 -- ID van de lamp
    local Motion = 196 -- ID van de bewegingsensor
    local sleepTime = 5*60 -- tijd in seconden, dat de lampen aanblijven
    local DagNacht = fibaro:getGlobalValue("DagNacht")

    if

    fibaro:getValue(Motion,'value') == '1' and fibaro:getGlobalValue("DagNacht") == "Nacht"

    then

    fibaro:debug("Someone moved, turn on light and abort")
        fibaro:call(lightID, "setSlider", "3", "50")
        fibaro:call(lightID, "pressButton", "1")
    fibaro:killScenes(__fibaroSceneId)

    end

    fibaro:debug("No one moving...")
    fibaro:sleep(1000*sleepTime)
    fibaro:debug(string.format("No one moved moved for %ss, turn off light %s",sleepTime,lightID))
    fibaro:call(lightID,"pressButton", "2")

     

    Now I want this scene to only turn on the lights in the room when they are off and the motion sensor reports motion. When the lights are on, the scene doesn't have to change anything.

    All of the lights are setup with the Philips Hue AIO VD. Can someone help me?

     

    • 0
    Posted

    Any good way to mix this scene with physical buttons? The scene stops working if client turns off the light manually using a lightswitch and walks into the room again. Increasing scene instances allow the client to do that few times without the scene breaking but it sounds like it could eventually overwhelm the gateway...

    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...