Is it possible to set a variable for one specific time (and preferably day as well) and later when a different event fires calculate the time/day difference? Ideally, I would end up with the days/hours/minutes between the 2 events (in some cases these events would be less than 24 hours but span across midnight).
I'm new to Hubitat and learning but google and my endless clicking in the rule machine has failed me. Go easy on my inexperience plz.
1 Like
An easy way would be to use the built-in now()
function, which will return a Long
representing the current Unix/epoch time in milliseconds. Save one to state
at the first time, then compare it to now()
(or another just-set variable) at the desired time, and if you can subtract to find the difference--with, perhaps, some conversion needed from milliseconds to your desired units.
EDIT: It just occurred to me you're probably talking about Rule Machine, not writing a custom (Groovy) app. You can do the above calculation in RM, too, by setting and comparing variables, but I'm not sure of any easy way to convert them to human-friendly units after that...
@bertabcd1234, yes in RM version 4 is what I was hoping for. I've tried to use time variables but it sounds like they only really handle time of day despite having a date associated with them. I've also tried using "number" type and setting them to the current time but things get wonky or just don't allow me to select the variables I'm expecting when I go to configure things
I use Number GV and just set them to the current unix time in seconds (RM allows for setting in seconds or milliseconds). Do this twice and compare the two and you have the difference in time between the two events in seconds. Do some variable math and you have days, hours, minutes, seconds.
It's funny, I made my rules with unix time well over a year ago, the topic hasn't come up since, other than last night and today, lol. Here is my post from last night: Time Global Variable Type
I use them for some tiles on my dashboard that I want to inform me of how many days until certain filters need to be changed. Here are the tiles on my dashboard:
4 Likes
I have RM rule where I calculate the minutes I am gone from the office during the day. I set a a time variable to current time, when I leave and get back and the time difference function in RM gives you the minutes difference in time.
Thanks for getting me started. Here's my code to covert to days, hours, and minutes, in case others want to crib:
set lnNow to now() in seconds
Set lnSincedStopped to (lnNow(1653484523) - lnFinished(1653437717))
Set lnDays to (lnSincedStopped(6) / 86400)
Set lnSubtract to (lnDays(0) * 86400)
Set lnSincedStopped to (lnSincedStopped(6) - lnSubtract(46800))
Set lnHours to (lnSincedStopped(6) / 3600)
Set lnSubtract to (lnHours(13) * 3600)
Set lnSincedStopped to (lnSincedStopped(6) - lnSubtract(46800))
Set lnMinutes to (lnSincedStopped(6) / 60)
Set lsAnnouncement to 'Your clothes have been sitting in the wash for '
IF (Variable lnDays(0) > 0(F) [FALSE]) THEN
Set lsAnnouncement to '%lsAnnouncement% %lnDays% days
END-IF
IF (Variable lnHours(13) > 0(T) [TRUE]) THEN
Set lsAnnouncement to '%lsAnnouncement% %lnHours% hours
END-IF
IF (Variable lnDays(0) > 0(F) OR
Variable lnHours(13) > 0(T) AND
Variable lnMinutes(0) > 0(F) [FALSE]) THEN
Set lsAnnouncement to '%lsAnnouncement% and
END-IF
IF (Variable lnMinutes(0) > 0(F) [FALSE]) THEN
Set lsAnnouncement to '%lsAnnouncement% %lnMinutes% minutes.
END-IF
2 Likes