# Help with scripting an expression tag in the gateway to write to a plc tag

**URL:** <https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [August 27, 2024, 4:23pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220 "2024-08-27T16:23:59Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evan\_Curry](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@Evan\_Curry](https://forum.inductiveautomation.com/u/Evan_Curry)\
**Post date:** [August 27, 2024, 4:23pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/1 "2024-08-27T16:23:59Z")

</div>

Hey all-

I am trying to write a script for an expression tag in the gateway, so that if it becomes true, it will write to a PLC tag. This is the script I have written so far, the first line is to prevent the script from running on changes in tag quality:

if previousValue.value != currentValue.value and initialChange == False:

if newValue.value([ProjectName]vSaaS Comm Failure) \> 0:  
system.tag.writeBlocking(["[~]WWTP/Scada\_Fail\_Alarm\_Interlock"],[1])  
else:  
system.tag.writeBlocking(["[~]WWTP/Scada\_Fail\_Alarm\_Interlock"],[0])

It keeps returning an error that says "no viable alternative at input 'vSaaS'".

Any help is appreciated, thank you

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [August 27, 2024, 4:29pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/2 "2024-08-27T16:29:21Z")

</div>

1. Format your code with the \</\> button, we can't see your indentation.
2. What is the tag structure?

your second if statement is incorrect

I would also recommend that you use an intermediary variable and only call `writeBlocking` once at the end.

```python
if previousValue != currentValue and not initialChange: #qualified values can be compared directly, without using ".value"
    if newValue.value : #any non-zero value will return true
        scada_fail = 1
    else:
        scada_fail = 0
    system.tag.writeBlocking('[~]WWTP/Scada_Fail_Alarm_Interlock'],[scada_fail])

```

---

<div class="post-metadata">

**Author:** ![Evan\_Curry](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@Evan\_Curry](https://forum.inductiveautomation.com/u/Evan_Curry)\
**Post date:** [August 27, 2024, 4:52pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/3 "2024-08-27T16:52:35Z")

</div>

Ah, shoot. It looks like this:

`if previousValue.value != currentValue.value and initialChange == False:

```
	if newValue.value([ProjectName]vSaaS Comm Failure) > 0:
		system.tag.writeBlocking(["[~]WWTP/Scada_Fail_Alarm_Interlock"],[1])
	else:
		system.tag.writeBlocking(["[~]WWTP/Scada_Fail_Alarm_Interlock"],[0])`

```

How do you mean an intermediary variable?

Also, it is important that the comm failure tag being any value greater than zero is in there, that is the trigger for the script to run

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [August 27, 2024, 4:55pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/4 "2024-08-27T16:55:09Z")

</div>

> [@Evan\_Curry](#):
>
> `newValue.value([ProjectName]vSaaS Comm Failure)`

`newValue.value([ProjectName]vSaaS Comm Failure)` Is the issue. I will assume that `[ProjectName]vSaaS Comm Failure` is the tag where this property change script exists? Just use `newValue.value` - if you need to read another tag you'll have to use `system.tag.readBlocking()`. The script I provided in my previous post is correct if my assumptions are correct.

`if newValue.value :` is the same as `if newValue.value > 0 :`  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/2/92099f645acb01bbdfbdc142a1a064b3d9147b09.png)

---

<div class="post-metadata">

**Author:** ![Evan\_Curry](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@Evan\_Curry](https://forum.inductiveautomation.com/u/Evan_Curry)\
**Post date:** [August 27, 2024, 5:11pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/5 "2024-08-27T17:11:01Z")

</div>

Really appreciate the quick responses, thank you.

`[ProjectName]vSaaS Comm Failure` is the tag I am watching for a tag value change. The tag I am writing this tag value change script for is a separate tag, called `vSaaS - Autodialer Failover`.

I have written this into the script:

```python
	if previousValue != currentValue and initialChange == False:

		if system.tag.readBlocking([ProjectName]vSaaS Comm Failure) > 0:
			system.tag.writeBlocking(["[~]WWTP/Scada_Fail_Alarm_Interlock"],[1])
		else
			system.tag.writeBlocking(["[~]WWTP/Scada_Fail_Alarm_Interlock"],[0])

```

And I am now seeing this as my error:

```python
SyntaxError: mismatched input ' ' expecting EOF (input>, line 1)
>>>

```

---

<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:** [August 27, 2024, 5:13pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/6 "2024-08-27T17:13:50Z")

</div>

1. Gateway Tag Change events do not have a `currentValue` object.
2. You say you want to write to the tag when it is true, but your script does both, either way you don't need the `else` section of the script at a minimum, maybe even the entire nested if.
3. Don't compare boolean values to 0,1 instead use them as booleans.

This should work:

```python
if newValue.value and not initialChange:
    system.tag.writeBlocking(["[" + event.tagPath.parentPath + "]WWTP/Scada_Fail_Alarm_Interlock"],[True])

```

or if you want to write in both cases:

```python
if not initialChange:
    system.tag.writeBlocking(["[" + event.tagPath.parentPath + "]WWTP/Scada_Fail_Alarm_Interlock"],[newValue])

```

Gateway Events do not support realtive tag paths so you have to give the fully qualified path

---

<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:** [August 27, 2024, 5:19pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/7 "2024-08-27T17:19:26Z")

</div>

Sorry, I missed that the trigger tag and the inspected tag are different.

That would look like this:

```python
if previousValue.value != newValue.value and not initialChange and system.tag.readBlocking(["[ProjectName]vSaaS Comm Failure"])[0].value:
    system.tag.writeBlocking(["[" + event.tagPath.parentPath + "]WWTP/Scada_Fail_Alarm_Interlock"],[True])

```

I expect you can arrive at the script should you want to write to the tag in both cases.

---

<div class="post-metadata">

**Author:** ![Evan\_Curry](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@Evan\_Curry](https://forum.inductiveautomation.com/u/Evan_Curry)\
**Post date:** [August 27, 2024, 5:23pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/8 "2024-08-27T17:23:24Z")

</div>

```python
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'previousValue' is not defined

```

How do I define previousValue?

---

<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:** [August 27, 2024, 5:25pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/9 "2024-08-27T17:25:18Z")

</div>

You don't it is provided to the event script as a built-in object.

---

<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:** [August 27, 2024, 5:26pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/10 "2024-08-27T17:26:29Z")

</div>

Where are you writing this script? Is it in a tag change event on a UDT/tag or in the gateway tag change event scripts? That makes a big difference.

---

<div class="post-metadata">

**Author:** ![Evan\_Curry](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@Evan\_Curry](https://forum.inductiveautomation.com/u/Evan_Curry)\
**Post date:** [August 27, 2024, 5:35pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/11 "2024-08-27T17:35:38Z")

</div>

I've been trying to do both to see which worked first. I didn't realize they were so different.

Initially, I was trying to create an expression tag. The "trigger" tag is a gateway tag, I followed that direction and used a gateway tag change script.

---

<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:** [August 27, 2024, 5:48pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/12 "2024-08-27T17:48:32Z")

</div>

> [@Evan\_Curry](#):
>
> The "trigger" tag is a gateway tag

There is no such concept in ignition as a Gateway Specific Tag (as Opposed to say a Client Tag). So I assume that you mean its a normal memory tag.

You should probably just use an expression tag.

Set the Execution Mode to "Event Driven"

Use this expression as the expression:

```python
if({"Path/To/Trigger"},{"Path/To/DrivingTag"},false})

```

This will only return true if both the trigger and the Driving Tag are true, otherwise it will return false.

---

<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:** [August 27, 2024, 6:17pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/13 "2024-08-27T18:17:39Z")

</div>

> [@lrose](#):
>
> This will only return true if both the trigger and the Driving Tag are true, otherwise it will return false.

This, but:

```python
{Path/To/Trigger} && {Path/To/DrivingTag}

```

No need for a wrapping `if`.

---

<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:** [August 27, 2024, 6:32pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/14 "2024-08-27T18:32:58Z")

</div>

So, to summarize what everyone has said and to make sure everyone understands (and hopefully I'm getting this correct). Your `WWTP/Scada_Fail_Alarm_Interlock` tag if possible should be an expression tag (unless it needs to write this value to a different PLC in which case you'll need a separate memory expression tag). The expression in that tag should be:

```python
{vSaaS Comm Failure} && {vSaaS - Autodialer Failover}

```

---

<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:** [August 27, 2024, 6:37pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/15 "2024-08-27T18:37:50Z")

</div>

Also, as an aside. You really should avoid the use of spaces and reserved characters in your tag names.

e.g.

`vSaaS Comm Failure` should be something like `vSaaS_Comm_Failure` and `vSaaS - Autodialer Failover` should be something like `vSaaS_Autodialer_Failover`

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [August 27, 2024, 7:33pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/16 "2024-08-27T19:33:55Z")

</div>

My bad, I was just fixing syntax errors on the original script that @Evan_Curry had provided and I didn't think to check the actual variables.

> [@lrose](#):
>
> 1. Gateway Tag Change events do not have a `currentValue` object.
> 2. You say you want to write to the tag when it is true, but your script does both, either way you don't need the `else` section of the script at a minimum, maybe even the entire nested if.
> 3. Don't compare boolean values to 0,1 instead use them as booleans.
> 
> This should work:
> 
> ```python
> if newValue.value and not initialChange:
> system.tag.writeBlocking(["[" + event.tagPath.parentPath + "]WWTP/Scada_Fail_Alarm_Interlock"],[True])
> 
> ```
> 
> or if you want to write in both cases:
> 
> ```python
> Gateway Events do not support realtive tag paths so you have to give the fully qualified path
> 
> ```

I had assumed that this was on a tag property change script, not a gateway event script.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [August 27, 2024, 7:45pm UTC](https://forum.inductiveautomation.com/t/help-with-scripting-an-expression-tag-in-the-gateway-to-write-to-a-plc-tag/95220/17 "2024-08-27T19:45:23Z")

</div>

> [@Evan\_Curry](#):
>
> `[ProjectName]vSaaS Comm Failure` is the tag I am watching for a tag value change. The tag I am writing this tag value change script for is a separate tag, called `vSaaS - Autodialer Failover`.
> 
> I have written this into the script:
> 
> ```python
> if previousValue != currentValue and initialChange == False:
> 
> if system.tag.readBlocking([ProjectName]vSaaS Comm Failure) > 0:
> system.tag.writeBlocking(["[~]WWTP/Scada_Fail_Alarm_Interlock"],[1])
> else
> system.tag.writeBlocking(["[~]WWTP/Scada_Fail_Alarm_Interlock"],[0])
> 
> ```

Many syntax errors and things to clean up here.

1. Don't compare `initialChange` to `False`, just use it as a boolean.
2. Since `vSaaS Comm Failure` is the tag you're watching (tag that triggers this `valueChanged` script), you don't need to read it again, just use `currentValue.value`. Even still, your `system.tag.readBlocking()` call is incorrect, you need to provide a list of paths as strings and then you would have to access the value of the first tag read. You're missing the brackets, quotes, and list reference `(system.tag.readBlocking(["[ProjectName]vSaaS Comm Failure"])[0].value` ).
3. Again, you don't need to compare the tag value to 0, just use it as a boolean. Anything non-null and non-zero is "True".
4. You're also missing your colon on your `else`
5. @lrose is correct. If you're going to write to the tag either way, just write `newValue.value` to the tag instead of using the if statement.

So, in the valueChanged() script of an expression tag:

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if previousValue != currentValue and not initialChange:
		system.tag.writeBlocking(['[~]WWTP/Scada_Fail_Alarm_Interlock'], [currentValue.value])

```
