How to create THEN statement

I'm creating a multicolor state for tank level using 2 different coils from my PLC over modbus. What would be the best way to create THEN statement.

If my tank low is not active then I want to run the tank high IF statement.

if(tanklow)=0 THEN
if({[default]Main/InfTankControl/LT200/HighAlmFlag}=1 && !{[default]Main/InfTankControl/LT200/HighAlmFlag}=0,
color (255,71,71), //red
color (0,255,255,179) //blue

This is the syntax for the if statement:

TabHereif({[default]Main/InfTankControl/LT200/HighAlmFlag}==1) && (!{[default]Main/InfTankControl/LT200/HighAlmFlag}=0):
AnotherTabHerecolor (255,71,71), //red
color (0,255,255,179) //blue

I'm on mobile, sorry for the awkward formatting , but tabbing is important (not using spaces). Of note, use "==" when not assigning a value. Not sure if the parentheses is necessary around the conditions, I can't remember.

Use three backticks ``` before and after your code block.

        && (!{[default]Main/InfTankControl/LT200/HighAlmFlag}=0):
        color (255,71,71), //red
        color (0,255,255,179) //blue

but it's missing the closing ')' and has a couple of other issues.

You're mixing python and expressions/other languages: You're scoping using indentation, but also using && which won't work in python.

If you want to use expressions, the syntax looks like this:

if (condition,

You can nest ifs.

The python version would look like this:

if condition:
    return foo
    return bar


return foo if condition else bar

Now, a few things about what you're trying to achieve and your snippet:

  • expressions, just as python scripts, will always return something (actually, a python function that raises an error will not return anything, but...). You can't just return in some cases and nothing in others. Not returning in python will actually return None.
    So, if you want to assign a color ONLY on some condition, you'll have to figure out something else.
    More details about what you actually want to do will help us guide you towards a proper solution.
  • You're checking the same tag twice:
    {tag} = 1 && !{tag} = 0
    Which is weird. If it's = 1, it can't be anything else. No need for any other check.

Perspective application or Vision? (Please add the tag to your question title.)

Is this Expression Language or Script?


Yeah it's hard to tell on mobile, I was hoping he would get the idea though

I write the following below expression, but getting the following error
"Function 'if(condition, trueReturn, falseReturn)' does not work with 1 arguments"

TAB if({[default]Main/InfTankControl/LT200/HighAlmFlag}=1)
TABTAB &&(!{[default]Main/InfTankControl/LT200/HighAlmFlag}=0):
TABTAB color (255,71,71), //red
TABTAB color (0,255,255,179) //blue

Please use the </> code formatting button or three backticks ``` before and after for your code blocks. (There's an edit pencil icon below each of your posts so you can fix them.)

Well... The syntax is shown in the error message, and the reason for the error is there as well.
We can't do much more than that, except point out what's already obvious:
You're not following the syntax. Here's what you're doing:

if (condition:
  if condition2:

instead of

if (condition,
  if (condition2,

@christopher_hurst, a key part of your question seems to be your confusion over expressions versus scripts. They are very different things in Ignition, and you cannot mix their syntax.

In an expression, if is a function that always takes three arguments, separated by commas.

In a script, if is a statement, using a colon after the condition, and the else: part is optional.

Using vision writing expression, maybe something like this

You'll have to nest them, as Pascal showed. I would check the HighAlm tag in the outer if(), and the HighWarn tag in the inner if().

If that's all the cases you have, you could do this:

case ({HighWarnFlag} + {HighAlmFlag},
    0, blue,
    1, yellow,
    2, red,

Obviously, replace the shortened tags for the correct paths, and color names with the appropriate color codes. Also, if your tag only ever take the values 0 or 1, the default color will never be used, but it's necessary for the function's syntax to be correct. Which means you could also use red as the default color and remove the 2, red line.

1 Like

That won't work. As others are saying, expression functions can be considered as equivelant to formulas in Excel cells (but without the starting '=' sign). You can't write a program (which is what you have done in your screengrab). You can only write an expression which returns a value. So, if you need multiple ìf branches then you must nest them properly, as you would in Excel.