# Variable Integer in string - define places

Morning all.

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.

That's my \$0.02 (with a leading zero, LOL).

1 Like

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!

All it would need is a suitable trigger for these actions to do their thing. Hope this helps!

1 Like

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!

Also check out the wc formatDurtation() and format() functions in the wiki.

If you understand RM, WebCoRE will be straightforward for you. Here are some of the parallels between the two that immediately come to mind:

``````RM  Child App  Rule  Trigger  Action  Variable Math
ā¼      ā¼        ā¼      ā¼        ā¼         ā¼
WC  Dashboard Piston Trigger Statement Expression
``````

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.)!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.