# Why does this not work? (logic to calculate an average and save the result)

**URL:** <https://community.homey.app/t/why-does-this-not-work-logic-to-calculate-an-average-and-save-the-result/37600>\
**Category:** Questions & Help\
**Created:** [November 19, 2020, 2:51pm UTC](https://community.homey.app/t/why-does-this-not-work-logic-to-calculate-an-average-and-save-the-result/37600 "2020-11-19T14:51:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![B3rt](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/b3rt/32/9435_2.png) [@B3rt](https://community.homey.app/u/B3rt)\
**Post date:** [November 19, 2020, 2:51pm UTC](https://community.homey.app/t/why-does-this-not-work-logic-to-calculate-an-average-and-save-the-result/37600/1 "2020-11-19T14:51:29Z")

</div>

I am trying to calculate and save the average result of 3 sensor lux values into the logic database.

I have in my room 3 motion sensors which all suply LUX values, now i want to save the average result as logic a value so i created the following flow:

When: sensor value changes (i choose 1 sensor)  
Then: calculate logic value.  
I choose from list the name of the value to which to save (in my case binnenlux) and then the following as text:  
{{[helderheid1]+[helderheid2]+[helderheid3]}/3}  
I expected that it would calculate the average of these 3 sensors and save the result as value in binnenlux but nothing happens, the value is empty

info:  
[helderheid1] = sensor 1 helderheid etc, choosen from list, blue tag)  
binnenlux is the logic name and is number value type

I also tried:  
{{[helderheid1]+[helderheid2]+[helderheid3]}:3}  
{[helderheid1]+[helderheid2]+[helderheid3]/3}

This is working but is NOT what i want, i want the average not the total:  
{[helderheid1]+[helderheid2]+[helderheid3]}

Anyone an idea what i am doing wrong and how to get the average and save this?

---

<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:** [November 19, 2020, 2:58pm UTC](https://community.homey.app/t/why-does-this-not-work-logic-to-calculate-an-average-and-save-the-result/37600/2 "2020-11-19T14:58:59Z")

</div>

> [@B3rt](#):
>
> {{[helderheid1]+[helderheid2]+[helderheid3]}/3}

The actual formula should use parentheses, not brackets:

```auto
([helderheid1]+[helderheid2]+[helderheid3])/3

```

---

<div class="post-metadata">

**Author:** ![B3rt](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/b3rt/32/9435_2.png) [@B3rt](https://community.homey.app/u/B3rt)\
**Post date:** [November 19, 2020, 3:02pm UTC](https://community.homey.app/t/why-does-this-not-work-logic-to-calculate-an-average-and-save-the-result/37600/3 "2020-11-19T15:02:56Z")

</div>

Thx, tried it but still no result, the logic value is still empty

(see lowest cards)

[https://homey.app/f/qT22xn](https://homey.app/f/qT22xn)

and

[https://homey.app/f/xCi2xn](https://homey.app/f/xCi2xn)

both are empty (see lowest card)

---

<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:** [November 19, 2020, 3:07pm UTC](https://community.homey.app/t/why-does-this-not-work-logic-to-calculate-an-average-and-save-the-result/37600/4 "2020-11-19T15:07:36Z")

</div>

> [@robertklep](#):
>
> `([helderheid1]+[helderheid2]+[helderheid3])/3`

To be clear, the full value of what you should enter in the flow card should be this:

```auto
{{([helderheid1]+[helderheid2]+[helderheid3])/3}}

```

Here’s an example that works for me:

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/0/1/01bbae24bbb4448cdb8cddaf9cb41bec088dcb9c.png)

Make sure you don’t have any additional spaces anywhere, those can cause issues.

---

<div class="post-metadata">

**Author:** ![B3rt](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/b3rt/32/9435_2.png) [@B3rt](https://community.homey.app/u/B3rt)\
**Post date:** [November 19, 2020, 3:10pm UTC](https://community.homey.app/t/why-does-this-not-work-logic-to-calculate-an-average-and-save-the-result/37600/5 "2020-11-19T15:10:17Z")

</div>

Yep…

I just noticed i now had not the double {{ and }}, added and now it is working!

thx for the help!

---

<div class="post-metadata">

**Author:** ![B3rt](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/b3rt/32/9435_2.png) [@B3rt](https://community.homey.app/u/B3rt)\
**Post date:** [November 19, 2020, 3:40pm UTC](https://community.homey.app/t/why-does-this-not-work-logic-to-calculate-an-average-and-save-the-result/37600/6 "2020-11-19T15:40:43Z")

</div>

Is there a command for rounding up or down?  
For example something like:

{{round(([helderheid1]+[helderheid2]+[helderheid3])/3),up)}}

---

<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:** [November 19, 2020, 6:18pm UTC](https://community.homey.app/t/why-does-this-not-work-logic-to-calculate-an-average-and-save-the-result/37600/7 "2020-11-19T18:18:12Z")

</div>

Logic cards support some (but not all) [MathJS functions](https://mathjs.org/docs/reference/functions.html).

To round up:

```auto
{{ceil(([helderheid1]+[helderheid2]+[helderheid3])/3)}}

```

To round down:

```auto
{{floor(([helderheid1]+[helderheid2]+[helderheid3])/3)}}

```

To round to two decimals:

```auto
{{round(([helderheid1]+[helderheid2]+[helderheid3])/3,2)}}

```
