Math in Expression not working correclty

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

image

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

1 Like

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

1 Like

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

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'"
)
3 Likes

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?

Thank you! I was unaware how to do that.

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

https://docs.inductiveautomation.com/display/DOC81/Expression+Functions

2 Likes

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

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

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

https://docs.inductiveautomation.com/display/DOC81/mean

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

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.

1 Like

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