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

Question

Posted

Hi,

I want to create a LUA script where I can check if somebody is home. I have a lua script when one of the sensor is breached it will set a variable to Yes. But no I want to check the last breachtime of a sensor (if breach > 1 hour ago and we are not sleeping, we are not at home). But how can I check this?

11 answers to this question

Recommended Posts

  • 0
Posted

Please login or register to see this code.

Replace 86 with your motion detector id. It apparently doesn't work with some versions of the hc firmware, but it works for me. It returns unix system time of last breached which can then be compared to present time

  • 0
  • Inquirer
  • Posted

    Dalle1985,

    Thanks!

    [ Added: 2014-10-05, 15:49 ]

    Is there also a lua function where I can check when a global variable is last changed?

    I have several motion sensor and this is maybe not the past way to check if somebody is home(I have to check 10 sensors every time). If I set a global variable when a motion is detected is maybe better? But then I need to check the last time the global variable is changed.

    • 0
    Posted

    Create a scene and set the global variable as the trigger condition? Anyone can enlighten me if changes in global variable can be set as a trigger condition?

    PS I have just checked, it can be used as a trigger condition. I tested for the case IN and out. If globalvar == IN, do something.

    • 0
    Posted

    There is no way to check when the global was last changed. But you could set the global to the current unix time when motion is detected, so basically, have a scene which triggers on any motion detector breach and then save the current unix timestamp to the global. That way you can always find the last time a motion detector was breached:

    Please login or register to see this code.

    This should do it for you. Then your scene can just take the timestamp from the global and check it against the current os.time. If it the difference is greater than i.e. 3600 it means no motion for more than one hour.

    • 0
  • Inquirer
  • Posted

    Dalle1985,

    Thanks, good solution!

    [ Added: 2014-10-09, 22:51 ]

    I am not the best(...) programmer. But I have this now:

    Please login or register to see this code.

    The problem what I seems to have is only one of the if statements runs (the first). Do I need two scenes to achieve my goal?

    • 0
    Posted

    Maybe you should use 2 different sets of if else. Change elseif to else if or end if

    • 0
    Posted

    First off, the same global can't be two values so this will never return true:

    Please login or register to see this code.

    If it is because it should be either, you should use an or statement instead. And then you should put the two in a parenthesis, this way, the lua engine will evaluate it together and return the parenthesis true or false instead.

    Try and see if this works:

    Please login or register to see this code.

    I've also removed the elseif. As you mention, this is never run. This is because your scene is only run when a motion detector is breached. So it will not trigger 3600 seconds after last motion, but only on the next motion event, and then the timer will ofcause reset. Therefore, you need either a separate scene or recode you scene to run in an infinite autostart loop with no triggers. So basically run above code, sleep for 5 seconds and then repeat. This isn't very tidy - some would call it junk code, but it is the only way to work with the lua available.

    The second solution would be to run two scenes:

    Please login or register to see this code.

    Above scene is triggered on motion detection, kills the scene below and then restarts it. This might seem a bit strange, but there is reason behind it. Lets take the second scene first, then I'll explain:

    Please login or register to see this code.

    So what this does, is that the first scene will set the somebodyhome variable to yes whenever motion is detected, it also kills the second scene and spawns a new instance. The second scene meanwhile, is set to start, wait 3600 seconds and then change the somebodyhome variable to no. Now as the first scene will autokill the second scene whenever motion is detected, the second scene will only ever complete if there has been no motion for 3600 seconds.

    Some might call this junk programming... And it probably is, but it works, so I don't think it really matters if purist (those who cann themselves "real") programmers don't like this... Anyway, hope this helps?

    • 0
    Posted
    There is no way to check when the global was last changed. But you could set the global to the current unix time when motion is detected, so basically, have a scene which triggers on any motion detector breach and then save the current unix timestamp to the global. That way you can always find the last time a motion detector was breached:

    Please login or register to see this code.

    This should do it for you. Then your scene can just take the timestamp from the global and check it against the current os.time. If it the difference is greater than i.e. 3600 it means no motion for more than one hour.

    FYI, You can use a global as a trigger to a scene.

    Please login or register to see this link.

    Look at the example using "A change to the global variable ‘isItDarkOutside’"

    If you use Dalle's example above you can trigger your scene each time the global is updated.

    • 0
  • Inquirer
  • Posted

    Thanks guys for the suggestions!

    • 0
    Posted

    Your very welcome... Did you find a solution?

    • 0
    Posted

    Is there also a lua function where I can check when a global variable is last changed?

    There is no way to check when the global was last changed.

    Well, in fact there is one way...

    The function fibaro:getGlobal("GlobalVariableName") actually returns two values. The first one is the value of the global, the second one the time it changed. Watch out: CHANGED. This means that it will not be updated if the global value is overwritten with the same value. It only updates the time if the value is different.

    So:

    Please login or register to see this code.

    will set "v" with the value and "t" with the last change time of global "GlobalVariableName".

    You might probably found a different solution through Dalle1985's approach though, but this info should be useful for further scenarios.[/b]

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