Math with variables?

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.

:tada: Update: Math and Date functions are now available as part of our native Rule Expressions feature!

Make sure to use the official Expressions feature from the linked article rather than the lab endpoint noted below in this post!

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:
    https://lab.sharptools.dev/math
    
  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 $context.response.data.result.

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

4 Likes