Tag in Event Script

Hi,
I have configured an event script "onClick" with the following text:
{[default]Tag/MyTag.value} = 123456
but MyTag is not changing. It is so simple, what is going wrong? I want to set that value by clicking the element.
What if I would set MyTag (memory tag) to another OPC tag on click?
{[default]Tag/Memory.value} = {[default]Tag/OPCTag.value}

It is not working, I am not familiarized with Event Script. Thanksss!

Try this script instead:

tagPaths = ['[default]Tag/MyTag']
writeValues = [123456]
system.tag.writeBlocking(tagPaths, writeValues)
1 Like

If it's happening what i think it's happening and the script worked, here's what's wrong:
When you're working with Scripts, you don't need the "{}" arround the tag name, that's for when you're using expressions and you want to use a tag value as a variable.
You can learn more about the differences here:
https://docs.inductiveautomation.com/display/DOC81/Scripting+Vs.+SQL+Vs.+Expressions

To write to a tag using a script, you need to use the system.tag.writeBlocking() or the system.tag.writeAsync functions, and for it to work you need to pass two parameters, a list of tag paths to write to and a list of values to write for each tag path, in the script above i've named them tagPaths and writeValues respectively.
You can learn more about these functions here:
https://docs.inductiveautomation.com/display/DOC81/system.tag.writeBlocking

And here:
https://docs.inductiveautomation.com/display/DOC81/system.tag.writeasync

For you to be able to write to a tag in a script you need a function, so naturally to read a tag value you also need a function, the functions are called system.tag.readBlocking() and system.tag.readAsync() but in this case you only need to provide a parameter with the list of tags you want to read and the function will return a list of qualified values, so in this case if you want to set your tag to another tag value, your script will look like this:

#First we get the path(s) we want to read
readTagPaths = ['[default]Tag/OPCTag.value']
#Now we read the tags in the paths list
valuesRead = system.tag.readBlocking(readTagPaths)
#After reading,the function will return a list of qualified values
#so we need to acces the first item on the list and get the .value
#of the read result
writeValues = [valuesRead[0].value]
#Now we define the path(s) of the tag(s) we want to write to
writeTagPaths = ['[default]Tag/MyTag']
#And finally, we pass the value we've read earlier as the value param
system.tag.writeBlocking(writeTagPaths, writeValues)

You can learn more about the functions here:
https://docs.inductiveautomation.com/display/DOC81/system.tag.readBlocking

And here:
https://docs.inductiveautomation.com/display/DOC81/system.tag.readasync

I would really recommend taking the free Inductive University credential courses, especially the Scripting in Ignition, besides being free, it's a great way to learn the basics of the Ignition Platform.

I hope this was helpful.

5 Likes