Feature request: Condition that compares variable to now()


I'm trying to implement an automation that is triggered based on a comparison between the now() value and a timestamp that I've set in a global variable (e.g. motion_sensor_last_active_at).

My current approach is to have a "Tick time_now" rule that is triggered every second and updates a global time_now variable to now() in seconds.

Then I use a variable comparison condition to compare motion_sensor_last_active_at to time_now.

I suspect the "Tick time_now" rule is pretty intense and every so often, I receive a bunch of "org.h2.jdbc.JdbcSQLException:" errors where periodicHandler is unable to find a Column or hits a general "java.lang.NullPointerException".

I wonder if, in addition to the relative to variable comparison condition, we could have a relative to now() in seconds comparison, which would do away with the need for a "Tick time_now" rule that runs every second?

Thanks. I'm a big fan of your product.

Just randomly poking around, what I'm trying to do here would also work for Rule machine question and avoid queuing up a bunch of delayed actions.

You can't set global variables to time values. They can be strings of text, but can't be used in the way you have asked, yet at least.

You CAN set global variables to time values.
Set vTime to now() in seconds.

And how exactly would you put that expression into Rule Machine?

And how would that be used to do anything? You can force a rule to be triggered at a particular time using that. That is u less you run that rule every second, which I'm pretty sure would lock up your hub pretty fast.

you definitely can set global variables to time now(). here is bruce's post explaining:

here is an example of a rule that I use to track presence:

try to remember that hubitat is an event based system. when writing rules, try think of what events you need to monitor. it doesn't have the hardware to constantly monitor everything, so you have to be careful with how you construct your rules to keep things from getting overloaded.

You can put now() in either milliseconds or seconds into a global variable. Let's say you use seconds. Suppose you have a rule triggered by a motion sensor active. One of the actions could be to record the time by putting it into some gv. Suppose you have some other rule that is triggered by something else. It can put now() into another gv, and then subtract the two gv's to know the elapsed time between the events, etc. You can do comparisons on the time values placed in the gv's also.

You never really explained what it is you want to accomplish. You certainly do not need a once-a-second rule to do it.

You can get current hour, current minute, and unix time (now) in seconds whenever you want. With math you should be able to do any transformations you need, and any conditional logic as well.

See Bruce’s example in the message ABOVE. That is what I’m doing in the rule I posted earlier.

Okay....but that can also be accomplished using Private Boolean also. Why would you need to know the time difference between two motion events? I just don't see a practical use for this.

I believe that the original question was about trigger actions at a specific time set in a global variable. Which, I believe isn't possible, as I originally stated.

Not quite what he said:

He never filled us in on what the comparison would be, but let's say the comparison is that the difference between the two is >= x seconds, and he wants to trigger his rule then. What he's missing is that he could have built x into a rule triggered by the motion sensor active, such as Run Actions Rule ZZ Delayed by x seconds. Many of these time based automations are easier than they first appear, and what needs to be adjusted is the thought process about how to accomplish the desired outcome.

1 Like

And I would still love to see an episode on Hubitat Live where you develop your logic and build a rule. We all need to learn to think like you. :wink:

I know this is old, but while I agree with @bravenel I also didn't see a specific answer. I had a similar desire. I wanted my motion detector to turn on my garage light. I also wanted the garage light to turn off after 10 minutes, regardless of how it was turned on (switch or motion.) So I created 1 rule that was just "when switch turns on, delay 10 minutes then turn it off". Then I created a second rule that was "if motion in garage, turn on the light then cancel any delayed action on both this rule and the first rule, then delay 10 minutes then turn off the light." Now, each time motion is triggered, it pushes the delay forward 10 minutes. No need to remember the times in variables.