Lab Fun: Math! Dates!
With the sunset of the SmartThings Groovy platform announced, we noticed an influx of webCoRE users arriving in the SharpTools community who brought with them a desire for math functionality and improved date features!
While the feature request for Math with Variables is gaining more votes by the day, we wanted to quickly put something together to help our transitioning webCoRE friends, so we built out some 'labs' features!
Overall Concept
Since we already support HTTP Actions within SharpTools rules, we knew we could quickly build out endpoints on our labs environment and make those available to the community.
The beauty of this is you can pass variables to these endpoints and get data back in the form of context variables. This enables a high degree of customization and flexibility while keeping things relatively simple to use for this experienced demographic.
Math and Formulas
The /math
endpoint accepts a single formula
parameter that enables you to perform calculations. The beauty of this is you can include variables in the formula enabling you to build dynamic calculations.
In the above example, we're using the well-known temperature conversion formula to convert Fahrenheit to Celsius.
C = (F - 32) * 5/9
We're able to pass our existing $temperature
variable into the formula and then can access the result
of the formula using a context variable.
Dates and Time
The /date
endpoint provides information about the current date and time. You can optionally pass a timezone
, locale
, and format
parameter to further customize the response.
As an example, check out the following URL which shows the current date/time in Phoenix, Arizona along with a custom MMM-D
formatted response (eg. Aug-20):
https://lab.sharptools.dev/date?timezone=America/Phoenix&format=MMM-D
You can either use the individual components from the response to build up your string or you can use the format
option and get the formatted
response in a single go:
Since the response is made available as a variable, you can do things like use it in conditions, notifications, or even pass it forward to the math endpoint for calculations (eg. limit rule to only even weeks).
Furthermore, the endpoint can parse input dates and format those in your localized date / time formats.
By request, we've also added the ability to check if the current date is between two dates. This is particularly helpful for things like holiday lighting or rules that you only want to run during a limited window of days.
What is SharpTools?
SharpTools enables you to create beautiful custom dashboards and powerful automation to level up your smart home! π
Get Started Free!