# How to create THEN statement

**URL:** <https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058>\
**Category:** Ignition\
**Created:** [April 30, 2023, 4:00pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058 "2023-04-30T16:00:30Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![christopher\_hurst](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@christopher\_hurst](https://forum.inductiveautomation.com/u/christopher_hurst)\
**Post date:** [April 30, 2023, 4:00pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/1 "2023-04-30T16:00:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ryan\_Allen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_allen/32/47013_2.png) [@Ryan\_Allen](https://forum.inductiveautomation.com/u/Ryan_Allen)\
**Post date:** [April 30, 2023, 4:38pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/2 "2023-04-30T16:38:41Z")

</div>

This is the syntax for the if statement:

if(tanklow)==0:  
_ **TabHere** _if({[default]Main/InfTankControl/LT200/HighAlmFlag}==1) && (!{[default]Main/InfTankControl/LT200/HighAlmFlag}=0):  
_ **AnotherTabHere** _color (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.

---

<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:** [April 30, 2023, 4:46pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/3 "2023-04-30T16:46:32Z")

</div>

> [@Ryan\_Allen](#):
>
> I'm on mobile, sorry for the awkward formatting

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

```auto
if(tanklow)==0:
    if({[default]Main/InfTankControl/LT200/HighAlmFlag}==1) 
        && (!{[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.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [April 30, 2023, 4:51pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/4 "2023-04-30T16:51:42Z")

</div>

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:

```auto
if (condition,
    return_this_if_condition_is_true,
    return_this_otherwise
)

```

You can nest ifs.

The python version would look like this:

```python
if condition:
    return foo
else:
    return bar

```

or

```python
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.

---

<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:** [April 30, 2023, 4:52pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/5 "2023-04-30T16:52:13Z")

</div>

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

Is this Expression Language or Script?

---

<div class="post-metadata">

**Author:** ![christopher\_hurst](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@christopher\_hurst](https://forum.inductiveautomation.com/u/christopher_hurst)\
**Post date:** [April 30, 2023, 4:54pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/6 "2023-04-30T16:54:00Z")

</div>

Vision

---

<div class="post-metadata">

**Author:** ![Ryan\_Allen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_allen/32/47013_2.png) [@Ryan\_Allen](https://forum.inductiveautomation.com/u/Ryan_Allen)\
**Post date:** [April 30, 2023, 4:54pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/7 "2023-04-30T16:54:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![christopher\_hurst](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@christopher\_hurst](https://forum.inductiveautomation.com/u/christopher_hurst)\
**Post date:** [April 30, 2023, 4:55pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/8 "2023-04-30T16:55:01Z")

</div>

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

'''if({[default]Main/InfTankControl/LT200/HighWarnFlag})==0:  
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  
TABTAB )'''

---

<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:** [April 30, 2023, 4:56pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/9 "2023-04-30T16:56:59Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [April 30, 2023, 4:59pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/10 "2023-04-30T16:59:55Z")

</div>

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:

```auto
if (condition:
  if condition2:
    foo,
    bar
  )
)

```

instead of

```auto
if (condition,
  if (condition2,
    foo,
    bar
  ),
  baz
)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 30, 2023, 5:25pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/11 "2023-04-30T17:25:29Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![christopher\_hurst](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@christopher\_hurst](https://forum.inductiveautomation.com/u/christopher_hurst)\
**Post date:** [April 30, 2023, 5:27pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/13 "2023-04-30T17:27:41Z")

</div>

Using vision writing expression, maybe something like this

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/0/d03649457fe1ac62e40f2e2f9bf03ceeda574403.png)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 30, 2023, 5:28pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/14 "2023-04-30T17:28:36Z")

</div>

> [@christopher\_hurst](#):
>
> 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()`.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [April 30, 2023, 5:42pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/15 "2023-04-30T17:42:20Z")

</div>

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

```auto
case ({HighWarnFlag} + {HighAlmFlag},
    0, blue,
    1, yellow,
    2, red,
    default_color
)

```

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.

---

<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:** [April 30, 2023, 6:07pm UTC](https://forum.inductiveautomation.com/t/how-to-create-then-statement/73058/16 "2023-04-30T18:07:24Z")

</div>

> [@christopher\_hurst](#):
>
> ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/0/d03649457fe1ac62e40f2e2f9bf03ceeda574403.png)
> 
> image969×171 31.5 KB

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.
