# Valve State Control with Boolean

**URL:** <https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727>\
**Category:** Ignition\
**Tags:** perspective, scripting, python\
**Created:** [December 12, 2024, 4:14am UTC](https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727 "2024-12-12T04:14:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Hankins](https://avatars.discourse-cdn.com/v4/letter/r/f07891/32.png) [@Ryan\_Hankins](https://forum.inductiveautomation.com/u/Ryan_Hankins)\
**Post date:** [December 12, 2024, 4:14am UTC](https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727/1 "2024-12-12T04:14:48Z")

</div>

I am new to Ignition and having a hard time figuring out where exactly to set this up.

I need to control the state property of a valve. I don't have a single tag I can bind with this property. I am using the 'open', 'closed' and 'failedToClose' states. I have a boolean tag to represent each one. That is to say I have a tag that is 'valveOpen', 'vavleClosed' and 'failedToClose'.

I thought I could use an expression binding for runScript(returnValveState, 1000, 'valveOpen', 'vavleClosed', 'failedToClose') and then have a script that would return 'open', 'closed' or 'failedToClose' based on the logic I wrote but it is not working.

 ![vavleState](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/a/9a0b2650a8d12302f3e096ba3e4c147f69ebc027.png)

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [December 12, 2024, 4:55am UTC](https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727/2 "2024-12-12T04:55:27Z")

</div>

Just use an expression binding with the following:

```python
case(true, valveOpenedTag, "Opened", valveClosedTag, "Closed", FailtocloseTag, "Failed to Close", "Unknown") 

```

Substituting your tags appropriately in an expression binding.

---

<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:** [December 12, 2024, 9:06am UTC](https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727/3 "2024-12-12T09:06:00Z")

</div>

```python
case(
    true, 
    valveOpenedTag, "Opened", 
    valveClosedTag, "Closed", 
    FailtocloseTag, "Failed to Close", 
    "Unknown"
)

```

That's an interesting use of the `case()` function. At first glance I thought it wouldn't work. It does - but there may be a problem.

From [case](https://www.docs.inductiveautomation.com/docs/8.1/appendix/expression-functions/logic/case):

> It takes the value argument and compares it to each of the case1 through caseN expressions. If value is equal to caseX, then case returns valueX. If value is not equal to any of the case1..N, then returnDefault is returned.

That means that if more than one status tag is true that it will return the first.

I would use [binEnc](https://www.docs.inductiveautomation.com/docs/8.1/appendix/expression-functions/logic/binEnc) to generate a unique integer for any combination of status bits and use that in the `case()` expression.

```python
case(
    binEnc(
      {[~]valveOpen}, // 1 (LSB)
      {[~]valveClosed}, // 2 
      {[~]failedToClose}, // 4 (MSB)
    ),
    0, "Valve moving", 
    1, "Opened", 
    2, "Closed", 
    4, "Failed to Close", 
    "Error"
)

```

This way we get an "Error" if more than two states are on and the `0` option suppresses the error while the valve is changing state normally until the failedToClose turns on.

@Ryan_Hankins note that you have no `failedToOpen` warning.

Expressions are much faster than scripts. Use them in preference where possible.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [December 12, 2024, 11:41am UTC](https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727/4 "2024-12-12T11:41:00Z")

</div>

I was originally going to suggest that but since they only asked for the individual bits in that order, I did the easiest way I could. (Thanks for reformatting my expression as I was on mobile and had no tab.)

---

<div class="post-metadata">

**Author:** ![Ryan\_Hankins](https://avatars.discourse-cdn.com/v4/letter/r/f07891/32.png) [@Ryan\_Hankins](https://forum.inductiveautomation.com/u/Ryan_Hankins)\
**Post date:** [December 12, 2024, 5:26pm UTC](https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727/5 "2024-12-12T17:26:39Z")

</div>

Thanks for the help.

In our application, if our closed feedback isn't active then we are assuming its open as long as we aren't telling it to be closed.

I figured out the script method I was using. I was naming the script and the function the same thing and I never called the function. runScript(returnValveState, 1000, 'valveOpen', 'vavleClosed', 'failedToClose') changed to  
runScript("myScripts.returnValveState", 1000, 'valveOpen', 'vavleClosed', 'failedToClose')

I tried the case option previously but I missed putting the true in the statement.

---

<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:** [December 12, 2024, 5:37pm UTC](https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727/6 "2024-12-12T17:37:55Z")

</div>

As explained, you shouldn't use scripts (or runScript from an expression) if you can do the job directly with an expression.

Please see [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831). (You have a typo in your code, 'vavle'.)

---

<div class="post-metadata">

**Author:** ![Francisco.Mejia](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/francisco.mejia/32/63246_2.png) [@Francisco.Mejia](https://forum.inductiveautomation.com/u/Francisco.Mejia)\
**Post date:** [December 17, 2024, 1:58pm UTC](https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727/7 "2024-12-17T13:58:39Z")

</div>

@Ryan_Hankins the state of a valve depends on what type of valve you have, either an open/close or % opening valve, so since you are trying to control the valve I ti should be assumed is a control valve.  
The open/close valve says that is open or close, if it get stuck while opening, it will send a signal that is open, and viceversa.  
I then case of a control valve with a actuator/positioner it will tell you what percent is open, which still send you a signal that is open. These valves are controlled with 4-20 mA signal in most cases.  
If you want to know why the valve is stuck open or close, or partially either you have to go and check the valve to see the reason. I have to do that many times.  
Hope this helps

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [December 17, 2024, 2:02pm UTC](https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727/8 "2024-12-17T14:02:57Z")

</div>

I would recommend not treating those valves the same. There's so many differences usually. I know on many of the valves in gas plants that are modulating valves don't have any feedback at all unless it's over HART communications which is rarely utilized. In this case, you don't have any alarms for fail to open/close because we don't even know, but we also don't want to just show open/close on the screen and want to show the actual commanded position. Plus the operator doesn't just click an open/close button as these are usually controlled via PID loop or manual operator entry of a 0-100% position.

---

<div class="post-metadata">

**Author:** ![Francisco.Mejia](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/francisco.mejia/32/63246_2.png) [@Francisco.Mejia](https://forum.inductiveautomation.com/u/Francisco.Mejia)\
**Post date:** [December 20, 2024, 1:53pm UTC](https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727/9 "2024-12-20T13:53:29Z")

</div>

Michael what do you mean by modulating? I thought a valve is either a control valve or a manually operated valve on the field.  
Thanks

---

<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:** [December 20, 2024, 2:23pm UTC](https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727/10 "2024-12-20T14:23:45Z")

</div>

> [@Francisco.Mejia](#):
>
> what do you mean by modulating?

Generally, a modulating valve would be a valve which has some type of analog driving signal, as opposed to a valve such as a solenoid which has a discrete driving signal (On/Off).

Generally we refer to modulating valves as Control Valves, and discrete valves as Functional Valves.

A "manually operated" valve can have many different meanings, for instance it is possible to have a control valve where the driving signal is being manually controlled from the HMI system. There are many control valves which do not have traditional position feedback, so the control system must assume, I told it to go to 50% its there, or it gets its feedback through some other mechanism such as a flow meter, tank level, or other device.

Or a "manually operated" valve could just mean a hand valve.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [December 20, 2024, 3:13pm UTC](https://forum.inductiveautomation.com/t/valve-state-control-with-boolean/99727/11 "2024-12-20T15:13:31Z")

</div>

Yeah, different terms are used. Many times the term Control Valve (FCV, PCV, TCV, etc) is used for a modulating valve, but I try to avoid it as it could get confusing for anyone not using that term as any valve can really control flows, pressures, etc. and the term modulate means (abbreviated definition): _to regulate by or adjust to a certain measure or proportion_ which truly defines it.

Our open/close valves many times are just referred to as shutdown valves (SDV) as they completely stop/isolate pressures, flows, etc., and are usually the larger assembly that uses one or two solenoid valves (SV).

Sometimes solenoid valves are also used for Control Valves but typically as a dump valve to dump the air pressure so the valve moves to its fail position (for safety).

All of these valves typically have an Auto/Program mode and a Manual/Operator mode depending on the terms you like to use.
