# Available math functions

**URL:** <https://community.homey.app/t/available-math-functions/16622>\
**Category:** Questions & Help\
**Created:** [August 9, 2019, 9:48am UTC](https://community.homey.app/t/available-math-functions/16622 "2019-08-09T09:48:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hihaj](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/hihaj/32/13671_2.png) [@Hihaj](https://community.homey.app/u/Hihaj)\
**Post date:** [August 9, 2019, 9:48am UTC](https://community.homey.app/t/available-math-functions/16622/1 "2019-08-09T09:48:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Morgan](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/morgan/32/15416_2.png) [@Morgan](https://community.homey.app/u/Morgan)\
**Post date:** [August 9, 2019, 10:30am UTC](https://community.homey.app/t/available-math-functions/16622/2 "2019-08-09T10:30:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [August 9, 2019, 10:33am UTC](https://community.homey.app/t/available-math-functions/16622/3 "2019-08-09T10:33:48Z")

</div>

_“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.”_

> **[Homey v2.0 — Insights, Alarms & Logic - Homey Blog](https://blog.athom.com/homey-v2-0-insights-alarms-logic/)**
>
> Homey has always been a very versatile product with many functionalities. Popular features are Insights and Alarms. In Homey v2.0 we also introduced Logic.

---

<div class="post-metadata">

**Author:** ![Ton\_Meek](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ton_meek/32/149_2.png) [@Ton\_Meek](https://community.homey.app/u/Ton_Meek)\
**Post date:** [August 9, 2019, 11:03am UTC](https://community.homey.app/t/available-math-functions/16622/4 "2019-08-09T11:03:34Z")

</div>

Try this list:  
[https://mathjs.org/docs/reference/functions.html](https://mathjs.org/docs/reference/functions.html)

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

---

<div class="post-metadata">

**Author:** ![Hihaj](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/hihaj/32/13671_2.png) [@Hihaj](https://community.homey.app/u/Hihaj)\
**Post date:** [August 9, 2019, 12:10pm UTC](https://community.homey.app/t/available-math-functions/16622/5 "2019-08-09T12:10:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nikla](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/nikla/32/18992_2.png) [@nikla](https://community.homey.app/u/nikla)\
**Post date:** [January 11, 2020, 10:03pm UTC](https://community.homey.app/t/available-math-functions/16622/6 "2020-01-11T22:03:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Canedje](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/canedje/32/8324_2.png) [@Canedje](https://community.homey.app/u/Canedje)\
**Post date:** [January 12, 2020, 12:06pm UTC](https://community.homey.app/t/available-math-functions/16622/7 "2020-01-12T12:06:53Z")

</div>

What do you expect of the “print” function?

---

<div class="post-metadata">

**Author:** ![nikla](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/nikla/32/18992_2.png) [@nikla](https://community.homey.app/u/nikla)\
**Post date:** [January 12, 2020, 3:57pm UTC](https://community.homey.app/t/available-math-functions/16622/8 "2020-01-12T15:57:49Z")

</div>

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](https://mathjs.org/docs/reference/functions.html) above I assumed all functioned were possible to use. Maybe it was wrong assumption.

---

<div class="post-metadata">

**Author:** ![Canedje](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/canedje/32/8324_2.png) [@Canedje](https://community.homey.app/u/Canedje)\
**Post date:** [January 12, 2020, 4:17pm UTC](https://community.homey.app/t/available-math-functions/16622/9 "2020-01-12T16:17:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [January 12, 2020, 4:25pm UTC](https://community.homey.app/t/available-math-functions/16622/10 "2020-01-12T16:25:29Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![nikla](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/nikla/32/18992_2.png) [@nikla](https://community.homey.app/u/nikla)\
**Post date:** [January 12, 2020, 4:58pm UTC](https://community.homey.app/t/available-math-functions/16622/11 "2020-01-12T16:58:18Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [May 3, 2020, 5:21pm UTC](https://community.homey.app/t/available-math-functions/16622/12 "2020-05-03T17:21:52Z")

</div>

are spaces allowed?

---

<div class="post-metadata">

**Author:** ![thierry\_arguimbau](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/thierry_arguimbau/32/155444_2.png) [@thierry\_arguimbau](https://community.homey.app/u/thierry_arguimbau)\
**Post date:** [July 3, 2020, 12:04am UTC](https://community.homey.app/t/available-math-functions/16622/13 "2020-07-03T00:04:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [July 3, 2020, 10:17am UTC](https://community.homey.app/t/available-math-functions/16622/14 "2020-07-03T10:17:17Z")

</div>

English please.

---

<div class="post-metadata">

**Author:** ![Patrick1610](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/patrick1610/32/6843_2.png) [@Patrick1610](https://community.homey.app/u/Patrick1610)\
**Post date:** [December 6, 2020, 11:17pm UTC](https://community.homey.app/t/available-math-functions/16622/15 "2020-12-06T23:17:45Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [December 7, 2020, 9:27am UTC](https://community.homey.app/t/available-math-functions/16622/16 "2020-12-07T09:27:07Z")

</div>

`max(CALCULATION, 0)`

---

<div class="post-metadata">

**Author:** ![Patrick1610](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/patrick1610/32/6843_2.png) [@Patrick1610](https://community.homey.app/u/Patrick1610)\
**Post date:** [December 7, 2020, 5:13pm UTC](https://community.homey.app/t/available-math-functions/16622/17 "2020-12-07T17:13:26Z")

</div>

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 🙏🏼

---

<div class="post-metadata">

**Author:** ![Torch1969](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torch1969/32/33305_2.png) [@Torch1969](https://community.homey.app/u/Torch1969)\
**Post date:** [January 17, 2021, 2:26pm UTC](https://community.homey.app/t/available-math-functions/16622/18 "2021-01-17T14:26:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Diposan](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/diposan/32/46698_2.png) [@Diposan](https://community.homey.app/u/Diposan)\
**Post date:** [August 21, 2022, 1:56pm UTC](https://community.homey.app/t/available-math-functions/16622/19 "2022-08-21T13:56:19Z")

</div>

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…?

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [August 21, 2022, 2:15pm UTC](https://community.homey.app/t/available-math-functions/16622/20 "2022-08-21T14:15:06Z")

</div>

If this works:

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

```

It should work with round like this:

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

```

Or this as well:

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

```

_Keep the closing `)` in mind_:  
`round(value,x)`

[Next page](https://community.homey.app/t/available-math-functions/16622.md?page=2)
