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?
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?
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.
Also, diffSetpoint is not defined, so you’ll need to either assign a value, or hard code a value
diffSetpoint = 100
if newValue.getValue() > diffSetpoint:
or
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:
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
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.
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
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
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
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.
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:
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).