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