I have a rule that has essentially 2 variables. 1 āHhā for hours and āMmā for minutes. They are both number variables.
They are derived from another number variable that has an elapsed minutes since event occurred āelapsedā
So what I do is
Set Hh to (elapsed / 60)
Set Hh to round (Hh)
If elapsed < 60
Then
Set Mm to elapsed
Else
Set Mm to (elapsed / 60)
End if
That gives me the elapsed minutes as Hours and Minutes
I then set a String variable HHMM to %Hh%:%Mm% which works fine to display the hours and min seperated with a colon.
But for anyone whoās looking at this from a programming background you likely see my issueā¦if either Hh or Mm is a single digit number it displays as such. So I wind up with things like 0:7 if itās only been 7 minutes or 2:5 if itās been 2 hours and 5 min.
Now I did all this because I couldnāt for the life of me figure out an easier way to
(1) determine the elapsed time since a door closed. So āelapsedā is set in a rule by setting a variable when a contact sensor reports closed and then periodically repeating variable math time difference of that time to current time. Which gives me elapsed time in min
(2) do that in a way that reports out a standard HH:MM time format
So can someone either tell me why Iām a dolt in the initial rule in calculating the time difference in an easier way that reports out HH:MM
Or
Tell me in a string if thereās a way to set the %Mm% to have 2 places with a leading 0 if the minutes is less than 10
Due to the extreme limitations placed on built-in variable math and string handling, I believe the answer you seek may lie in the app WebCoRE. It offers a robust set of numeric and string functions, and allows the result to be assigned directly to your HHMM variable.
Totally worth the afternoon it will take the typical user to design and implement all this in a WebCoRE piston. I use it for calculating the average temperature deviation among 4 sensors throughout my house, which if done in HE, would require a really grisly RM rule. In WebCoRE, it takes two lines of code.
As I continued pondering this exercise, things escalated quickly, and I hopped into WebCoRE to see if I couldn't rustle up something useful to show you. This works!
Iām going to have to sit down later after the kiddos are in bed and look at webcore syntax and what you wrote to see what you did, thanks! This may be the push for me to get into webcore to do things!
BONUS: You don't have to create a Variable Connector in order for WC to have access to a Hub Variable; WC can read/write all hub variables directly, as well as create new ones. WC can even assign sensor/device attribute values to variables. Also, you'll be astonished at how many Triggers can be defined based just on variable values (e.g. "Changed", "Drops", "Any Of", "Greater Than", etc.)!