# Math in Expression not working correclty

**URL:** <https://forum.inductiveautomation.com/t/math-in-expression-not-working-correclty/75420>\
**Category:** Ignition\
**Created:** [June 22, 2023, 7:01pm UTC](https://forum.inductiveautomation.com/t/math-in-expression-not-working-correclty/75420 "2023-06-22T19:01:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdvancedSolutions](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/advancedsolutions/32/47573_2.png) [@AdvancedSolutions](https://forum.inductiveautomation.com/u/AdvancedSolutions)\
**Post date:** [June 22, 2023, 7:01pm UTC](https://forum.inductiveautomation.com/t/math-in-expression-not-working-correclty/75420/1 "2023-06-22T19:01:09Z")

</div>

So I have two pumps and I want to display the Hz of both of them averaged. I have it set up so if only one is on, then it will display the Hz of that one. Yet for some reason the average is never correct.

From the PLC I get a number in the thousands for Hz. So I divide by 100 first and then make sure it displays as only two numbers.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/1/4162ec16631f7766d17642f2712e82fb4e5975a5.jpeg)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/0/60787b9527f0d82c8be277cbd7f066092fedbed7.jpeg)

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [June 22, 2023, 7:06pm UTC](https://forum.inductiveautomation.com/t/math-in-expression-not-working-correclty/75420/2 "2023-06-22T19:06:30Z")

</div>

You'll want extra parenthesis around your addition. Currently you are only dividing the freq for pump 10 since multiplication/division takes precedence.

`(valA + valB)/2`

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [June 22, 2023, 7:35pm UTC](https://forum.inductiveautomation.com/t/math-in-expression-not-working-correclty/75420/3 "2023-06-22T19:35:43Z")

</div>

Tip: post code, not pictures of code, and format it using the \</\> button. That way we can copy and edit in our answers.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [June 22, 2023, 7:47pm UTC](https://forum.inductiveautomation.com/t/math-in-expression-not-working-correclty/75420/4 "2023-06-22T19:47:16Z")

</div>

I would write this expression like this. Personally I think it makes it more readable.

```auto
numberFormat(
    case(
        binEnc(
            {[default]Main/P9P10Flow/P9 Pump Status},
            {[default]Main/P9P10Flow/P10 Pump Status}
        ),
        0,0,
        1,{[default]Pop up window info/P10/Frequency}/100,
        2,{[default]Pop up window info/P9/Frequency}/100,
        mean(
            {[default]Pop up window info/P9/Frequency}/100,
            {[default]Pop up window info/P10/Frequency}/100)
    ), "00' Hz'"
)

```

---

<div class="post-metadata">

**Author:** ![AdvancedSolutions](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/advancedsolutions/32/47573_2.png) [@AdvancedSolutions](https://forum.inductiveautomation.com/u/AdvancedSolutions)\
**Post date:** [June 22, 2023, 8:04pm UTC](https://forum.inductiveautomation.com/t/math-in-expression-not-working-correclty/75420/5 "2023-06-22T20:04:18Z")

</div>

That does look much better than what I did. I'm not quite sure how that works though.  
I assume that mean function will give me the average of two inputs. Other than that I'm not familiar with the other functions (besides numberFormat).

Could this display 00 Hz when both tags do not have a value?

---

<div class="post-metadata">

**Author:** ![AdvancedSolutions](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/advancedsolutions/32/47573_2.png) [@AdvancedSolutions](https://forum.inductiveautomation.com/u/AdvancedSolutions)\
**Post date:** [June 22, 2023, 8:06pm UTC](https://forum.inductiveautomation.com/t/math-in-expression-not-working-correclty/75420/6 "2023-06-22T20:06:32Z")

</div>

Thank you! I was unaware how to do that.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [June 22, 2023, 8:13pm UTC](https://forum.inductiveautomation.com/t/math-in-expression-not-working-correclty/75420/7 "2023-06-22T20:13:46Z")

</div>

You should find a reasonable description of the operation of the Expression Language functions here:

[https://docs.inductiveautomation.com/display/DOC81/Expression+Functions](https://docs.inductiveautomation.com/display/DOC81/Expression+Functions)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [June 22, 2023, 8:17pm UTC](https://forum.inductiveautomation.com/t/math-in-expression-not-working-correclty/75420/8 "2023-06-22T20:17:04Z")

</div>

> [@AdvancedSolutions](#):
>
> I'm not quite sure how that works though

`case()` lets you examine a value and then based on that value return a different value. Similar to how a `select case` operation works in many programing languages.

[https://docs.inductiveautomation.com/display/DOC81/case](https://docs.inductiveautomation.com/display/DOC81/case)

`binEnc()` is short for Binary Encode, it takes a number of binary values and returns the decimal value.

In this case there are 4 possible values (0,1,2,3). I chose to use 3 or (1,1) as the default case, but you could also use any of the other values. The important part is knowing what integer values are possible when `binEnc()` is supplied two bits.

[https://docs.inductiveautomation.com/display/DOC81/binEnc](https://docs.inductiveautomation.com/display/DOC81/binEnc)

`mean()` will return the mean() (a.k.a Average) of the supplied values.

[https://docs.inductiveautomation.com/display/DOC81/mean](https://docs.inductiveautomation.com/display/DOC81/mean)

You could probably simplify this a bit more by factoring out the division.

```auto
numberFormat(
    case(
        binEnc(
            {[default]Main/P9P10Flow/P9 Pump Status},
            {[default]Main/P9P10Flow/P10 Pump Status}
        ),
        0,0,
        1,{[default]Pop up window info/P10/Frequency},
        2,{[default]Pop up window info/P9/Frequency},
        mean(
            {[default]Pop up window info/P9/Frequency},
            {[default]Pop up window info/P10/Frequency})
    )/100, "00' Hz'"
)

```

This will indeed Return 00 Hz if both pumps are false. If you remove the `0,0,` line it would technically return the Mean, which would also be 00 Hz, but I chose to list it explicitly for clarity, and in case you wanted to provide some other number in that case.

---

<div class="post-metadata">

**Author:** ![AdvancedSolutions](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/advancedsolutions/32/47573_2.png) [@AdvancedSolutions](https://forum.inductiveautomation.com/u/AdvancedSolutions)\
**Post date:** [June 22, 2023, 8:22pm UTC](https://forum.inductiveautomation.com/t/math-in-expression-not-working-correclty/75420/9 "2023-06-22T20:22:22Z")

</div>

Thats great!  
Thank you so much for your help. I've learned a lot from this.
