# Expression Tag that Writes to OPC Address (PLC)

**URL:** <https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614>\
**Category:** Ignition\
**Created:** [February 19, 2021, 1:25pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614 "2021-02-19T13:25:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![DANewton](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/danewton/32/10851_2.png) [@DANewton](https://forum.inductiveautomation.com/u/DANewton)\
**Post date:** [February 19, 2021, 1:25pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/1 "2021-02-19T13:25:21Z")

</div>

The answer is probably obvious, but…

I need to create an expression tag that writes to an OPC location, which is a bit on a PLC connected via OPC UA. I’ve been reading OPC tags (Allen Bradley PLCs) and even writing to them, but not running an expression and writing to it both.

In the expression tag (Boolean), I’m watching a pressure differential and if it crosses a threshold, it returns a TRUE. If not, it returns a FALSE. Simple. Now I want to write that true or false to a bit on a PLC so it turns on a stack light?

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [February 19, 2021, 1:40pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/2 "2021-02-19T13:40:59Z")

</div>

Take a look at Gateway Tags Change Scripts: [https://docs.inductiveautomation.com/display/DOC81/Gateway+Event+Scripts](https://docs.inductiveautomation.com/display/DOC81/Gateway+Event+Scripts)

Your script will look something like this:

```auto
if newValue.getValue() > diffSetpoint:
    system.tag.writeBlocking([tagToWrite],[1])
else:
    system.tag.writeBlocking([tagToWrite],[0)

```

where tagToWrite is the tagpath that you want to write. This is just pseudocode so you will have to adjust to fit your system.

---

<div class="post-metadata">

**Author:** ![DANewton](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/danewton/32/10851_2.png) [@DANewton](https://forum.inductiveautomation.com/u/DANewton)\
**Post date:** [February 19, 2021, 1:42pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/3 "2021-02-19T13:42:49Z")

</div>

Thank you for the quick response. I’ll try this. I’m wondering if there’s a way to do this other than scripting.

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [February 19, 2021, 1:51pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/4 "2021-02-19T13:51:17Z")

</div>

Not in a way that will be reliable.

Expressions are basically only available for use when a client is running.

Your best bet will be to use the Tag Change Event to trigger that.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [February 19, 2021, 2:01pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/5 "2021-02-19T14:01:09Z")

</div>

> [@bschroeder](#):
>
> Expressions are basically only available for use when a client is running.

Expressions on regular tags run all the time, you must be thinking about expressions on client tags.

But there's still no way to make an expression go and write to another tag without using the 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:** [February 19, 2021, 2:05pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/6 "2021-02-19T14:05:28Z")

</div>

> [@Kevin.Herron](#):
>
> But there’s still no way to make an expression go and write to another tag without using the change script.

In 7.9 there was an option on expression tags to write the value back to an OPC item. If this functionality was removed how is it handled during conversion?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [February 19, 2021, 2:05pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/7 "2021-02-19T14:05:46Z")

</div>

Turned into a tag change script on upgrade.

See [[13069] - Write value to Opc item feature for Expression Tags is no longer available in 8.0](http://forum.inductiveautomation.com/t/13069-write-value-to-opc-item-feature-for-expression-tags-is-no-longer-available-in-8-0/23112/5)

---

<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:** [February 19, 2021, 2:06pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/8 "2021-02-19T14:06:00Z")

</div>

If you are still on v7.9, there are OPC write-back settings available for Expression Tags:

[https://docs.inductiveautomation.com/display/DOC79/Types+of+Tags#TypesofTags-ExpressionTags](https://docs.inductiveautomation.com/display/DOC79/Types+of+Tags#TypesofTags-ExpressionTags)

This feature has since been dropped. /:

Edit: ninja’d by @lrose. (:

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [February 19, 2021, 2:12pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/9 "2021-02-19T14:12:49Z")

</div>

That’s pretty much what I meant I just didn’t expound like ya’ll did. 😃

---

<div class="post-metadata">

**Author:** ![DANewton](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/danewton/32/10851_2.png) [@DANewton](https://forum.inductiveautomation.com/u/DANewton)\
**Post date:** [February 19, 2021, 3:23pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/10 "2021-02-19T15:23:34Z")

</div>

So, here’s what I have. I’m not used to scripting, so I’m not sure about the syntax concerning the tags.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/8/d827167a46793efaca94ad483c1d1a624ef3611b.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/b/4bd07e5450b9979c35a1db9360e74ab3bb04a5f3.png)

When I trigger the expression tag ([default]_Rkn031\_900_/Atomizer/Atomizer\_Open\_Check),  
the tag it’s supposed to write to (…Atomizer\_Open) doesn’t change.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [February 19, 2021, 3:25pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/11 "2021-02-19T15:25:44Z")

</div>

You need brackets around the tag paths in the `writeBlocking` calls. Both parameters are lists.

---

<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:** [February 19, 2021, 3:28pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/12 "2021-02-19T15:28:24Z")

</div>

Also, diffSetpoint is not defined, so you’ll need to either assign a value, or hard code a value

```auto
diffSetpoint = 100
if newValue.getValue() > diffSetpoint:

```

or

```auto
if newValue.getValue() > 100:

```

Edit: just thinking through this a bit. If the tag that is triggering this event is an expression tag which results in a bool value then your script can be simplified

if you only want to write to the tag on a true value:

```auto
if newValue.getValue():
    system.tag.writeBlocking(['[default]_Rkn031_900_/Atomizer/Atomizer_Open'],[newValue.getValue()])

```

if you want it to write for both then omit the if statement

```auto
system.tag.writeBlocking(['[default]_Rkn031_900_/Atomizer/Atomizer_Open'],[newValue.getValue()])

```

If the tag is a numeric value then you’ll need what you have.

---

<div class="post-metadata">

**Author:** ![DANewton](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/danewton/32/10851_2.png) [@DANewton](https://forum.inductiveautomation.com/u/DANewton)\
**Post date:** [February 19, 2021, 3:54pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/13 "2021-02-19T15:54:53Z")

</div>

GOT IT !!!

Thank you everyone. 😁 New skill learned that can be applied elsewhere.

Here’s the final (before I saw lrose’s simplified version):

if newValue.getValue() \> 0:  
system.tag.writeBlocking(["[default]_Rkn031\_900_/Atomizer/Atomizer\_Open"],[1])  
else:  
system.tag.writeBlocking(["[default]_Rkn031\_900_/Atomizer/Atomizer\_Open"],[0])

This works, but I will simplify it.

" A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." ~ Antoine de Saint-Exupery.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [February 19, 2021, 5:45pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/14 "2021-02-19T17:45:45Z")

</div>

Just to note, you can also have on change scripts directly on individual tags if it’s more appropriate there. For example for tags within a udt that require it, it’s far easier to use a tag’s on change event than have to add each instance tag into the gateway tag change event

---

<div class="post-metadata">

**Author:** ![Oscar\_Saborio](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/oscar_saborio/32/32415_2.png) [@Oscar\_Saborio](https://forum.inductiveautomation.com/u/Oscar_Saborio)\
**Post date:** [March 3, 2022, 11:21pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/15 "2022-03-03T23:21:08Z")

</div>

Is it possible to write a tag with another tag, for example: system.tag.writeBlocking([tagToWrite],[another\_Tag])? To send a expression tag to OPC tag

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [March 4, 2022, 1:12am UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/16 "2022-03-04T01:12:27Z")

</div>

Yes, although I don’t know what kind of performance impact this might have if doing this quickly and with many tags. Keep in mind that you can use relative tag path references within tag change scripts. e.g. use `[.]../../Tag` to refer to a tag 2 folder parent folders down

---

<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 26, 2024, 3:01pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/17 "2024-08-26T15:01:17Z")

</div>

I am trying to use this script as a template for a similar situation (if we lose comms with server, write a 1 to this bool opc tag) but am getting an error saying

Traceback (most recent call last):  
File "", line 1, in   
NameError: name 'newValue' is not defined

How would I define newValue?

---

<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 26, 2024, 3:10pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/18 "2024-08-26T15:10:59Z")

</div>

newValue is defined as part of the gateway tag change event. It is a special QualifiedValue that represents the QualifiedValue that has just been written to the tag which triggered the change.

This error makes me wonder if you are attempting to use this script in a Tag Value Change script which is not the same thing as a Gateway Tag Change event.

---

<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 26, 2024, 3:24pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/19 "2024-08-26T15:24:11Z")

</div>

Yes I was trying to use a tag value change script, should I be applying this script in a gateway tag change event 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 26, 2024, 3:30pm UTC](https://forum.inductiveautomation.com/t/expression-tag-that-writes-to-opc-address-plc/43614/20 "2024-08-26T15:30:59Z")

</div>

For a script of this nature, it probably doesn't matter much. Though for a value change script it should be modified.

Something like this would be used in a Value Change Script:

```python
if currentValue.value:
    system.tag.writeAsync(['Path to tag'],[currentValue])

```

Notice the use of `writeAsync` here. It is important becuase of how value change scripts work that they execute as quickly as possible (in single digit ms).

Gateway Tag Change Events do not have this restriction. They are also a project resource and so have access to project specific information where Value Change scripts do not. Which you use depends on execution time and the need for project specific information.

Never touch a DB in a value change script. (Never is maybe a heavy word, but generally this is a good rule).
