# Change tag expression from script

**URL:** <https://forum.inductiveautomation.com/t/change-tag-expression-from-script/21472>\
**Category:** Ignition\
**Created:** [December 18, 2018, 9:29am UTC](https://forum.inductiveautomation.com/t/change-tag-expression-from-script/21472 "2018-12-18T09:29:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xantielolaa](https://avatars.discourse-cdn.com/v4/letter/x/c89c15/32.png) [@xantielolaa](https://forum.inductiveautomation.com/u/xantielolaa)\
**Post date:** [December 18, 2018, 9:29am UTC](https://forum.inductiveautomation.com/t/change-tag-expression-from-script/21472/1 "2018-12-18T09:29:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![grietveld](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grietveld/32/6984_2.png) [@grietveld](https://forum.inductiveautomation.com/u/grietveld)\
**Post date:** [December 18, 2018, 12:46pm UTC](https://forum.inductiveautomation.com/t/change-tag-expression-from-script/21472/2 "2018-12-18T12:46:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [December 19, 2018, 3:14pm UTC](https://forum.inductiveautomation.com/t/change-tag-expression-from-script/21472/3 "2018-12-19T15:14:46Z")

</div>

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.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![xantielolaa](https://avatars.discourse-cdn.com/v4/letter/x/c89c15/32.png) [@xantielolaa](https://forum.inductiveautomation.com/u/xantielolaa)\
**Post date:** [December 19, 2018, 4:04pm UTC](https://forum.inductiveautomation.com/t/change-tag-expression-from-script/21472/4 "2018-12-19T16:04:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [December 19, 2018, 4:41pm UTC](https://forum.inductiveautomation.com/t/change-tag-expression-from-script/21472/5 "2018-12-19T16:41:41Z")

</div>

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

```auto
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))

```

---

<div class="post-metadata">

**Author:** ![xantielolaa](https://avatars.discourse-cdn.com/v4/letter/x/c89c15/32.png) [@xantielolaa](https://forum.inductiveautomation.com/u/xantielolaa)\
**Post date:** [December 20, 2018, 8:41am UTC](https://forum.inductiveautomation.com/t/change-tag-expression-from-script/21472/6 "2018-12-20T08:41:53Z")

</div>

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.

![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/1/1bed32a4fb2328e3bc246c71b01a4c67de377cba.png)
