Expression Comparisons

In Python you can say:

if 2 < 3 < 4:
print ‘Hello’

and it will be very clear and it will work.

In expression language if you say:
if( 2 < 3 < 4, ‘Hello’, ‘Goodbye’)

it will give you an error because it can’t compare three values like that.

The best way around this I have found is:
if( 2 < 3 && 3 < 4, ‘Hello’, ‘Goodbye’)

but in practice and for longer comparisons it quickly becomes less clear than the Python version.

if( myVal1 < myVal2 && myVal2 < myVal3 && myVal3 < myVal4, ‘Hello’, ‘Goodbye’)

Is there another way to do this or has Python spoiled me?

Any ideas are appreciated.

Thanks,

  • Mike

Python has spoiled you. :laughing:

I’ve never used it before but I believe the switch function will handle multiple evaluations and returns.

If you really must, you can do this with python via objectScript() or the latest runScript():objectScript('args[0] < args[1] < args[2]', {Root Container.property1}, {Root Container.property2}, {Root Container.property3})Though I doubt I’d actually do this unless one or more of the arguments had execution side effects or big delays.
{ /end shameless plug for one of my modules :laughing: }

I have 3 LED Displays that have a script changing them to a 1 or 0 in the value that in turn changes the text. I now need to check each LED data value field to see if they are all 1's, if they are it will will return true (1) and enable a button, if any 1 of the 3 fields are 0, it should return a false (0) and not enable the button. I have tried multiple ways but cannot seem to get the statement right.

Current statement:

If({Root Container.Displacement Display.value} = {Root Container.Generic Indicator.value} = {Root Container.Serial Number LED Display.value},1,0)

Change the expression to a simple logical AND.

{Root Container.Displacement Display.value} 
  && {Root Container.Generic Indicator.value} 
  && {Root Container.Serial Number LED Display.value}

That will return a boolean true or false.

Expression functions work a little like Excel formulas (but without the = operator at the start).


Tip: use the </> button when posting code. It preserves tabs, does syntax highlighting and makes it obvious where the code starts and ends.

2 Likes

I implemented these a while ago, but seeing this topic pushed me to release an update to Ignition Extensions that adds an anyOf expression function that could be useful for collapsing repeated boolean checks.

2 Likes

Thank you transistor. This is working like I hoped. Is there a way to bind this boolean to a button enable/disable. I have been trying to use this to enable a button but have been unsuccessful. If the boolean is a 1 the button is on and if 0 the button is disabled.

Will the expression not work in the enable property?

1 Like

It does not seem to in the format Transistor gave me. I also tried an if statement with Transistors syntax with the same results. When you say enable property do you mean the set property?

HaHa! I'm a newb. I found it in the filter for the vision property filter for all instead of Basic.

Thank you Jordan.

1 Like