Fantastic, thank you!!
You have my interest haha. What’s the workaround you’re thinking of?
Side note, you have an awesome name!
Fantastic, thank you!!
You have my interest haha. What’s the workaround you’re thinking of?
Side note, you have an awesome name!
Sending you a PM.
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
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.
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.
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.
https://lab.sharptools.dev/math
{
"formula": "1 + 2"
}
Anywhere after that step, you can access the result of the calculation using Context Variables.
Let’s use a notification as an example:
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.
I have attempted this workaround for a simple n=n+1 operation and it works as intended. Thank you for posting this up.
Of course you would like it!
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
Hi
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.
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.
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.
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
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.
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.