Available math functions

Are there built-in math functions that can be used in variable calculations (in basic logic flows, not Homeyscript)? If so, where can I find the docs?

1 Like

You can use the Logic-card “Calculate a numeric value” in THEN field an set variables to calculated values from other variables or/and tags.
I’m sorry to say I don’t know where you can find the docs.

“You can even perform math inside a Flow! Surround your formula with curly brackets, for example:
{{ 1 + 2 }}
Of course you can add tags in this calculation.”

Try this list:
https://mathjs.org/docs/reference/functions.html

Example: {{round(5.345,2)}}

5 Likes

Thanks @Ton_Meek - this is what I was looking for.

This was what I was looking for but I fail to use it.

I tried this in a Simple Logic card:
{{print('Foo $0', <Temperature>)}}
where is one of the Tags I get the following on the timeline:
{{print('Foo $0', 19.9)}}
where 19.9 is the value of

What am I doing wrong?

What do you expect of the “print” function?

I expected Foo 19.9. I assumed it was like sprintf in C.
When I tried {{ 10 + 20 }} it returned 30.

When I looked at the link https://mathjs.org/docs/reference/functions.html above I assumed all functioned were possible to use. Maybe it was wrong assumption.

I’m not sure, but I expect print means print to terminal, what not is possible at Homey

The math.print() “values” argument (the second one) is expected to be an object, an array or a matrix, not a scalar value.

Try this:

{{ print('Foo $0', [ <Temperature> ]) }}

When I enter your proposal I get the following:
{{ print('Foo $0', [ 19.9 ])}}
which is the same as I got earlier.
It seems like I fail with the print function. But I do realize how to solve my initial problem which was to create a string including a tag value in the middle. I wanted to create a json string, e.g. {{ value : <Temperature> }}.
I thought I needed the print function for it nut it now works with what you just gave me above. Thanks!

are spaces allowed?

floor(x) : partie entiÚre mathématique usuelle = entier immédiatement inférieur à x

ceil(x) : partie entiÚre par excÚs = entier immédiatement supérieur à x

round(x) : arrondi entier le plus proche

abs(x) : retourne la valeur positive

sqrt(x) : racine carrée

1 Like

English please.

1 Like

Hi, I am working on some logic for circadian lighting. I want to use a calculation {{(85-“light percentage”)/100}} to set a light temperature variable between 0 and 85 based on the light percentage (provided by sun events). But I want to prevent the results from becoming below zero. Any ideas on how to do this? The reason I use 85 as base is because I don’t want the temperature higher than 85 in the evening (2000K).

1 Like

max(CALCULATION, 0)

Ah of course! I misunderstood the function of min() and max() at first! Used your logic but applied to the min() formula. This prevents negatives as wel and also ensures it is 0.85 at most.

{{Min(((100-‘Light percentage’)/100),0.85)}} is the result and it works amazing!!

Thanks for your insight :pray:t3:

1 Like

Some more inspiration. I used this calculation in standard homey variable calculation to convert a number (in string format from MQTT) to valid dim-value for a homey dimmer:

{{round(number(<variable/tag)/254,2)}}

Where <variable/tag> is the variable or tag with string that you want to convert. In this case a value between 0-254 that converts to 0-1 with 2 decimals.

2 Likes

I also would like to round the result of a formula with a tag in it, doesn’t work


What I have, this works :
{{ ( Prijs nu + 0 + 0.0023 + 0.13979 ) * 1.06 }} → returns something like 0.4869534 (would like it to return 0.49)
(where “Prijs nu” is a tag coming from the “Power by the hour” app.

What I’m trying :
{{ round ( ( Prijs nu + 0 + 0.0023 + 0.13979 ) * 1.06 ),2 }} → returns empty var

Also, following your example :
{{ round ( ( number (Prijs nu) + 0 + 0.0023 + 0.13979 ) * 1.06 ),2 }} → returns empty var

Where is my logical error
?

If this works:

{{ ( Prijs nu + 0 + 0.0023 + 0.13979 ) * 1.06 }}

It should work with round like this:

{{ round ((( Prijs nu + 0 + 0.0023 + 0.13979 ) * 1.06) ,2) }}

Or this as well:

{{ round (( Prijs nu + 0 + 0.0023 + 0.13979 ) * 1.06 ,2) }}

Keep the closing ) in mind:
round(value,x)

1 Like