# Scripting for a MOA switch in Ignition 8

**URL:** <https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048>\
**Category:** Ignition\
**Tags:** python, scripting\
**Created:** [September 14, 2022, 1:16pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048 "2022-09-14T13:16:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brandon\_Friedman](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@Brandon\_Friedman](https://forum.inductiveautomation.com/u/Brandon_Friedman)\
**Post date:** [September 14, 2022, 1:16pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/1 "2022-09-14T13:16:26Z")

</div>

I am trying to script an MOA tag in Ignition but it keeps giving me the same error. Syntax Error on Token: ‘BOUND CONST’ (Line 1 , Char 31). Here is the code itself attached to the Multi-State Switch and the MOA tag.

```auto
if {[.]../digital/cr_minus_bit} == 0:
    {[.]../bypass/cr_minus_bypass} = True
    {[.]../bypass/cr_minus_bypass_off} = False
if {[.]../digital/cr_minus_bit} == 1:
    {[.]../bypass/cr_minus_bypass} = False
    {[.]../bypass/cr_minus_bypass_off} = False
if {[.]../digital/cr_minus_bit} == 2:
    {[.]../bypass/cr_minus_bypass} = False
    {[.]../bypass/cr_minus_bypass_off} = True

```

Any help at all would be appreciated.

---

<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:** [September 14, 2022, 1:25pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/2 "2022-09-14T13:25:31Z")

</div>

You can’t make assignments in the expression language. You **return** a value.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [September 14, 2022, 1:27pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/3 "2022-09-14T13:27:15Z")

</div>

Additionally in the expression language you use a single `=` for comparison. You would also need parentheses around your `if( {[.]…/digital/cr_minus_bit}=0, 'someTrueValue', 'someFalseValue')` statement. Check out the documentation here -  
[https://docs.inductiveautomation.com/display/DOC80/if](https://docs.inductiveautomation.com/display/DOC80/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:** [September 14, 2022, 1:42pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/4 "2022-09-14T13:42:02Z")

</div>

You’ve tagged your post with `python`, and your script looks kinda like python, but you’re using the expression style `{tag}`. Which are you using, python or expression ?

For expression, see posts above.  
For python, you can’t access tags like this, you need to use `system.tag.readBlocking/readAsync` and `system.tag.writeBlocking/writeAsync` functions.  
Also, if you’re using python, I’d suggest reducing the number of if statements:

```python
minus_bit = system.tag.readBlocking(["path/to/minus_bit_tag"])[0].value

bypass = minus_bit == 0
bypass_off = minus_bit == 2

system.tag.writeBlocking(["path/to/bypass_tag", "path/to/bypass_off_tag"], [bypass, bypass_off])

```

```auto

```

---

<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:** [September 14, 2022, 1:46pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/5 "2022-09-14T13:46:36Z")

</div>

Brandon, please use the \</\> code formatting button when posting code. It will indent properly (important for Python) and give syntax highlighting. You can use the 🖉 edit link to fix your post.

---

<div class="post-metadata">

**Author:** ![Brandon\_Friedman](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@Brandon\_Friedman](https://forum.inductiveautomation.com/u/Brandon_Friedman)\
**Post date:** [September 14, 2022, 4:50pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/6 "2022-09-14T16:50:10Z")

</div>

I have tried using every tool I was given on here and still not working. Is it actually possible to write out an expression or scripting for a Manual Off Auto switch that Ignition will accept besides making a tag in the PLC?

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [September 14, 2022, 4:57pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/7 "2022-09-14T16:57:43Z")

</div>

Perhaps I missed this, but is this Vision or Perspective? It could make a difference, not sure.

---

<div class="post-metadata">

**Author:** ![Brandon\_Friedman](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@Brandon\_Friedman](https://forum.inductiveautomation.com/u/Brandon_Friedman)\
**Post date:** [September 14, 2022, 4:59pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/8 "2022-09-14T16:59:49Z")

</div>

Vision

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [September 14, 2022, 5:54pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/9 "2022-09-14T17:54:43Z")

</div>

The issue is that you can’t write to a tag using an expression. If you are looking to keep it all in the component, I’d recommend:

- Two custom properties (I called mine ‘bypassOn’ and ‘bypassOff’. I’m very imaginative.).  

- Bind these two properties to your _cr\_minus\_bypass_ and _cr\_minus\_bypass\_off_ tags. Be sure to check the ‘Bidiectional’ box.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/e/1e3d6192519630a24a4235b68b59ecf55cac9f29.png)  

- Use a propertyChange script to write to these properties.  

```python
if event.propertyName == 'controlValue':
	
	if newValue == 0:
		event.source.bypassOn = True
		event.source.bypassOff = False
	elif newValue == 2:
		event.source.bypassOn = False
		event.source.bypassOff = True
	else:
		event.source.bypassOn = False
		event.source.bypassOff = False

```

---

<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:** [September 14, 2022, 6:00pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/10 "2022-09-14T18:00:57Z")

</div>

Simplified further:

```auto
if event.propertyName == 'controlValue':
	v = event.newValue
	event.source.bypassOn = v == 0
	event.source.bypassOff = v == 2

```

---

<div class="post-metadata">

**Author:** ![Brandon\_Friedman](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@Brandon\_Friedman](https://forum.inductiveautomation.com/u/Brandon_Friedman)\
**Post date:** [September 14, 2022, 6:14pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/11 "2022-09-14T18:14:06Z")

</div>

> [@JordanCClark](#):
>
> ```python
> if event.propertyName == 'controlValue':
> 	
> if newValue == 0:
> event.source.bypassOn = True
> event.source.bypassOff = False
> elif newValue == 2:
> event.source.bypassOn = False
> event.source.bypassOff = True
> else:
> event.source.bypassOn = False
> event.source.bypassOff = False
> 
> ```

This fix does make the values bidirectional right?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 14, 2022, 6:21pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/12 "2022-09-14T18:21:05Z")

</div>

That script will update the custom properties. The custom properties, because they’re bidirectionally bound, will update the tags.

---

<div class="post-metadata">

**Author:** ![Brandon\_Friedman](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@Brandon\_Friedman](https://forum.inductiveautomation.com/u/Brandon_Friedman)\
**Post date:** [September 14, 2022, 7:54pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/13 "2022-09-14T19:54:59Z")

</div>

So I’m trying the custom properties fix, it works partially. it still will not change the values of the tags associated with the properties. They are OPC tags inside of a UDT instance.

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [September 14, 2022, 8:06pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/14 "2022-09-14T20:06:20Z")

</div>

You followed what @JordanCClark had above, bindings set to bidirectional?

By the way, not sure I would call this a fix, it’s pretty much how you do it. 🙂

---

<div class="post-metadata">

**Author:** ![Brandon\_Friedman](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@Brandon\_Friedman](https://forum.inductiveautomation.com/u/Brandon_Friedman)\
**Post date:** [September 14, 2022, 8:08pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/15 "2022-09-14T20:08:31Z")

</div>

Yes, I followed what @JordanCClark had above to the letter, barring changing some things to fit the tag names, and it seems as if Ignition refuses to accept it.

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [September 14, 2022, 8:10pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/16 "2022-09-14T20:10:35Z")

</div>

Perhaps your tags are read only? Maybe try creating some test tags, making them memory, to isolate the OPC side of things?

---

<div class="post-metadata">

**Author:** ![Brandon\_Friedman](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@Brandon\_Friedman](https://forum.inductiveautomation.com/u/Brandon_Friedman)\
**Post date:** [September 14, 2022, 8:14pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/17 "2022-09-14T20:14:56Z")

</div>

I keep getting this error each time I select something on the MOA.  
 ![Capture](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/f/9f7cbdef1e790b21ac632fff7b472439568df3c0.png)

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [September 14, 2022, 8:15pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/18 "2022-09-14T20:15:49Z")

</div>

Well that’s important to know. 🙂

Paste in the details of the error.

---

<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:** [September 14, 2022, 8:16pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/19 "2022-09-14T20:16:44Z")

</div>

Paste as text and format with the \</\> button, please. Not a screenshot.

---

<div class="post-metadata">

**Author:** ![Brandon\_Friedman](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@Brandon\_Friedman](https://forum.inductiveautomation.com/u/Brandon_Friedman)\
**Post date:** [September 14, 2022, 8:30pm UTC](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048/20 "2022-09-14T20:30:55Z")

</div>

Error message:  
Error executing script for event: `propertyChange`  
on component: `comp1_unloader2_moa`.

Details:  
Traceback (most recent call last):  
File “event:propertyChange”, line 2, in   
NameError: name ‘value’ is not defined

[Next page](https://forum.inductiveautomation.com/t/scripting-for-a-moa-switch-in-ignition-8/64048.md?page=2)
