Change tag expression from script

I want to know if it is possible to change the expression of an Expression Tag via script. I use an event to create a tag using an UDT instance that contains the Expression Tag I want to modify.

I’m don’t think you can do that, but you should be able to use a memory tag or a value from the UDT to create an if function to choose what formula you want to use.

It is possible, if not a bit advanced.

Enclosed is a snippet of code. This may or may not work in 8, just a heads up.

from com.inductiveautomation.ignition.common.tags.config import TagConfigProps  #Import TagConfigProps
t = system.tag.browseConfiguration("[]Test Tag",False)[0] #Browse for the tag, get the first member
ev = t.get(TagConfigProps.EventScripts) #Get the EventScripts property
ev.set("valueChanged","\tprint \"dlrow olleh\"") #Change the expression
t.set(TagConfigProps.EventScripts,ev) #Write the modified expression to the TagConfiguration
system.tag.configure("[]",t)  Write the TagConfiguration back tot he tag

2 Likes

Thank you for your response. It was not exactly what I was looking for, but I have been able to adapt your code to it.

Here is a more pythonic way of doing this, using a dictionary

t = system.util.jsonDecode(system.tag.browseConfiguration("[]Test Tag",False)[0].toJSON())
eventExists = False
for i in range(len(t["eventScripts"])):
	if t["eventScripts"][i]["eventid"] == "valueChanged":
		eventExists = True
		break
if eventExists:
	t["eventScripts"][i]["script"] = "\tprint \"dlrIw olleh\""
else:
	t["eventScripts"].append({"eventid":"valueChanged","script":"\tprint \"dlrow olleh\""})
	
system.tag.configure("[]",system.util.jsonEncode(t))

Using this code I am able to read the configuration of the tag I want, but when writing the new expression it creates a new tag inside the root folder, it does not overwrite the tag.

Using the image I attach as an example, I read the expression of the 080018-002/CALC001529/Valors/Mesura tag correctly, change it and try to overwrite it. However, instead of overwritting it it creates a new tag inside the 080018-002 folder with the name ‘Mesura’. How can I solve this?

I am sure that the problem comes from the last line of the code where I set the path as “” as I guess it refers to the folder istead of the tag instance.