Math with variables?

Mathematics in variables is great, I give +1
I could make a virtual electricity meter in the tile :), which would count the power consumption of the indicated devices :slight_smile:

1 Like

One big up vote on this from me, I am trying to replicate most of my WebCoRE pistons for Smartthings and I use ‘average’ of values in so many ways, and also sometimes simple “offset” like sunset -30 minutes for example, or -.5 degrees of the “cool set temp”… So far I’m really at a loss for reproducing my pistons, and it’s getting frustrating. Hubitat is looking better and better.

1 Like

When you create this, would you also look at calculating time difference, including across midnight.

I currently have some webcore pistons that calculate how long before a morning alarm will go off. I use this for a disabled family member.
I use virtual switches as trigger and alarm.
Happy to providewebcore pistons and SharpTools rule numbers if you want more detail.

Yes, a PM with examples would be helpful. Time related functions would likely be a separate feature request – especially considering we don’t have a formal time construct at the moment. See the recent Rules / Variables based on Date or Calendar thread for more discussion on the topic.

I wanted to a share a workaround that might be sufficient until we get a chance to build this as a first-class feature in the Rule Engine.

We have a Lab environment which has some endpoints that can be called using the HTTP Action in the Rule Engine and you can get the results using context variables. The /math endpoint requires a formula parameter and returns the calculated value in the result property.

In the example above, I’ve referenced the $temperature SharpTools variable directly within the formula, then we reference the result using a Context Variable (Response > HTTP > Data) via the result property.

How To (Steps)

  1. Open your desired SharpTools rule and in the Flow section, add an ActionHTTP
  2. Change the Method to POST and set the URL to:
  3. Leave the Content-Type as JSON and enter the Payload in the following format:
    "formula": "1 + 2"
  4. Save the action

Anywhere after that step, you can access the result of the calculation using Context Variables.

Using the Result

Let’s use a notification as an example:

  1. Create an ActionNotification and select your desired notification type (eg. Email)
  2. You can tap the +VARIABLE button to the right of any of the input fields to insert a variable into the field
  3. Once the Select Variable screen comes up, tap the Context Variables tab then:
    ResponseHTTPResponse Data
  4. You’ll be prompted to Input Data Key where you’ll type in result

Once you’ve tapped ‘OK’, you should see a variable inserted into your input field in the format $

If you trigger your rule, that context variable will include the result of the calculation and will be inserted into your notification.


I have attempted this workaround for a simple n=n+1 operation and it works as intended. Thank you for posting this up.

1 Like

@josh, this is super duper cool! :+1:

1 Like

Of course you would like it! :laughing:

1 Like

I would love to see simple math for variables, and also being able to assign a variable to a device setting plus a certain amount. For me, I would like to be able to take action if the temperature of my attic is a certain amount over the value outside. This is to prevent ice dams. If the temperature in the attic goes over a certain amount over the outside temperature, it turns on a gable fan in the attic. It isn’t simple enough to just set the attic temperature to the outside temp, because rarely does the attic match the outside temp (always slightly higher). I would like to have the fan go on say at 10 degrees higher (or something). There are many cases where either having a variable that you can do simple math and/or having the ability to do simple math in an if statement would be critical for certain home automations. Thanks


Is it possible to round numbers in variables? I am currently downloading, for example, weather data and it shows me 21.53 degrees Celsius, and it would be better for the eye to 22 degrees Celsius

Yes. Wrap it in round()

round(22.83 + 0.14)

This may be a stupid question but in this example what are you calculating? I get the ($temperature - 32) but why * 5/9?

I agree that just “simple” would be very useful. Plain old addition and subtraction would provide an easy way to change intensity of bulbs, for example.

It’s the formula to convert Fahrenheit to Celsius:

1 Like

this is great. thank you.
I have a 4 button remove that sits on my office desk. Thanks to this, I can now do temp +1 or temp -1 when certain buttons are pressed.
With this functionality, I am now completely migrated from WebCore to SharpTools.

1 Like

Just an update that we’ve added a number of Date functions to the /math endpoint. We wanted to consolidate all calculation/expression related features into single endpoint – this is all toward the goal of ultimately making this a native ‘expression’ feature within rules.

You can find documentation and examples of the various math features in the following help article:

Some quick examples to whet your appetite:

formatDate(now(), 'YYYY-MM-DD') //2022-09-23 (en-US default locale)
formatDate(now(), 'LLL', 'pl')  //23 września 2022 17:58 (Polish locale)
humanizeDuration(60 * 60 * 1000, true) // "in an hour"
addDays(now(), 1)   //tomorrow (at the current time)
endOfDate(now(), 1) //23:59 tonight
setDatePart(startOfDate(now()), 'hour', 4) //4:00 AM today
1 Like

Awesome! Thanks.

(20 chars)

One thing to note is that you may want to pass a timezone (IANA format) as a separate parameter when calling the math endpoint with date formulas otherwise it will default to UTC.

As mentioned above, all of this is working toward making the math/date features a native feature in SharpTools. The plan is that you will be able to have a simple input field for your expression and the system will use your configured preferences for pulling in the timezone data.

If you go to the main page for the Labs site, you can sort of get a feel for this. The form tries to infer your timezone from your browser and will include it with the requests by default.

1 Like

This is fantastic!
2 questions.

Is there a way to format the date to just show the day of the week using addDays(now),1)?

formatDate(now(),‘dddd’) = Friday
formatDate(addDays(now(),1,‘dddd’)) = 2022-09-24T17:50:59-07:00

Is there a document or resource available with the accepted format options?
For example in your screenshot above you use ‘LLL’ which I’m assuming means long date?

So close! The parenthesis were slightly off:

formatDate(addDays(now(),1),'dddd') //'Saturday'

Did you see the link to the help article above? It has a link to the formats. Here they are for your reference: