Math with variables?

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.

: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 rather than the lab endpoint noted in this thread!

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.

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

The native expression feature automatically pulls your timezone from your SharpTools account, so there’s no need to specify the timezone as was required with the original ‘labs’ approach.


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.

https://lab.sharptools.dev/

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:

:rofl:

I missed this one. Thank you!!!

Where does this “Round()” go?

I’m stuck on another formatting issue.

I need to add 1 day to today’s date and set time for 4:00 am.

formatDate(addDays(now(),1,setDatePart(startOfDate(now()), 'hour', 4)),'MMM D, h:mm a') // Sep 25, 2:33 pm

It can be used in a formula. In the above example, the calculation was returning a decimal value (eg. 21.53) and an integer value was preferred, so the whole formula could be wrapped in the round() function.

{
  "formula": "round(22.83 + 0.14)"
}

It looks like a parenthesis issue again which can get tricky when nesting multiple function calls within each other. You can think of nesting functions within functions kind of like those Russian nesting dolls. Each function needs to have its own parenthesis closed before you can nest it within another function.

                                   addDays(now(),1)
                       startOfDate(                )
           setDatePart(                             , 'hour', 4)
formatDate(                                                     ,'MMM D, h:mm a')

Which ultimately gets you:

formatDate(setDatePart(startOfDate(addDays(now(),1)), 'hour', 4),'MMM D, h:mm a')
1 Like

The expression feature is now available in beta as a native Rule Engine feature! If you’re part of our beta program, you should be able to see the post in the beta category.

If you’re not part of the beta program, you can use the /math endpoint noted above or send a PM to @support with your math/date expression use-case and we can get you added to the beta group!

:warning: Using a variable that contains an array will no longer “pretty print” arrays as the new expression feature enables custom formatting.*

1 Like

I am making an API call to openweather to get the sunset using context.variable.response.current.sunset that returns in seconds. 1664586776.
I want to convert that to ‘h:mm a’ but for it to convert correctly it needs to be in milliseconds. Is there a way to either convert the response from openweather to result in millisecond or to format the conversion to use seconds.
In the reference documents I see x for milliseconds and X for seconds but I don’t know what that format would look like. I’m currently using the math lab http with formatDate($sunset, 'h:mm a')

Is $sunset referring to the stored API response (timestamp in seconds) in your second formula?

You could multiply it by 1000 either when saving it to the $sunset variable or if the variable is stored in seconds, you could multiply it by 1000 when using the variable.

Adjusting when saving:

Set $sunset:
$context.reponse.data.current.sunrise * 1000

-OR-

Adjusting when using:

formatDate($sunset * 1000, 'h:mm a')
1 Like

Yes and that worked! Thank you!

1 Like

Just a reminder that the expression feature is available in beta! If anyone wants to join the beta and help test, shoot a PM to @support

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

Here’s a quick demo going through some of the features including math with variables, logic, date formatting, and date math. I was moving quick to stay under a minute, but feel free to pause the video if needed!

3 Likes

Love the Tap to Evaluate! Much easier than a test rule set to run the next minute, and then . . . waiting.

2 Likes

5 posts were split to a new topic: How to round Variable values

Math and Date Expressions are now available! Thanks to everyone who voted for this feature and a special thanks to those of you who helped beta test! You can find details on the release in the following thread:

Math and Date Expressions
https://community.sharptools.io/t/math-date-expressions/10368?u=josh

1 Like