Expression Evaluation - Last Seen Date/Time
-
I am trying to run a reaction that turns on lights if a sensor has not been tripped for 2 minutes. Basically, when a garage door is open, turn on inside lights if nothing has been sensed for 2 minutes. My question has to do with the expression/variable for "Last Seen". The variable never gets updated over time since the last seen event never changes.
In the screenshot above, the reaction checks for any garage door as well as the variable called "GarageLastEntry". The expression for this variable is displayed below:
The problem that I am experiencing is that the variable never changes over time. I understand that this is because the "last seen" variable does not change over time when there is no activity. In this screenshot, the value shows 0.029... This can also be seen as the current value in the original reaction screenshot above. After 10 minutes, this value should be 10. However, when a garage door is opened, the variable does not get updated and thus the rule does not become true since .029 is less than 2.
When I go to the variable and click on the > icon, the variable then gets updated as expected as seen below:
So the ultimate question is, how do I use a rule that can use a variable such as "last seen"? Do I need to create another variable or reaction that multiplies this by 1 every minute? From what I read in the manual, a variable is evaluated every time it is included in a reaction. But this is not what I am seeing.
I also want to ask another question related to this variable. It seems like Reactor continuously deletes this variable and I have to keep recreating it? I have never experienced this with MSR so wanted to ask if this is a common problem or if my definition is causing this.
Any info is appreciated!!!
-
I usually use metadata to accomplish the same:
e = getEntity( "virtual>house_mode" ), e.attributes.string_sensor.value == "away" ? ( time() - e.attribute_meta.string_sensor.value.last_modified ) > 64800 : false
Change attributes and timeouts (in seconds) accordingly.
YMMV. -
@therealdb Thanks for your feedback!
I have not used a metadata statement like this before so have a follow up. I think I have the proper data elements. But is this statement supposed to be part of the Expression definition or the Reaction Trigger? The screenshot below shows what I tried creating the expression.
Sorry for the lack of understanding on my part! Is the goal to have the variable itself be true/false? Or is the goal to have the variable still provide a numeric result that is used in the reaction? I may just need a little more direction.
Thanks again!
-
I think I solved this with a completely different approach. Instead of using variables based on Last Seen, I am just checking the current state of the sensor and used the Restriction that the sensor must be FALSE (ie door closed) for 120 seconds. Not sure why I did not try this the first time.
Since my Reaction is now working, I should probably close this out as Solved. However, I am still interested in how to use a variable as described in the original post in case I need to use a similar approach in the future. Hopefully someone can provide some guidance (thanks realdb!) on how to use the variables. If I don't hear back within a few days, I will go ahead and update this as Solved.
-
@sidmsmith said in Expression Evaluation - Last Seen Date/Time:
Instead of using variables based on Last Seen, I am just checking the current state of the sensor and used the Restriction that the sensor must be FALSE (ie door closed) for 120 seconds. Not sure why I did not try this the first time.
This is the way.
-
-
@sidmsmith said in Expression Evaluation - Last Seen Date/Time:
If I don't hear back within a few days, I will go ahead and update this as Solved.
your solution is correct. This code will give you the ability to use it in notifications (ie: Door left open for 5 minutes) or when you want to do things programmatically (ie: house is away since 12 hours, do that, 18 hours, do this, etc).
2/6