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

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?

The actual formula should use parentheses, not brackets:

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

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

(see lowest cards)

and

both are empty (see lowest card)

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

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

Here’s an example that works for me: Make sure you don’t have any additional spaces anywhere, those can cause issues.

Yep…

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

thx for the help!

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

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

Logic cards support some (but not all) MathJS functions.

To round up:

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

To round down:

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

To round to two decimals:

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