# Add value change script to a tag

**URL:** <https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [February 8, 2024, 4:52am UTC](https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421 "2024-02-08T04:52:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sachin\_Kamble](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Sachin\_Kamble](https://forum.inductiveautomation.com/u/Sachin_Kamble)\
**Post date:** [February 8, 2024, 4:52am UTC](https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421/1 "2024-02-08T04:52:59Z")

</div>

Hello,

I am using Ignition 8.1.22. I have developed perspective application and I have to add a value change script while creating a tag using scripting.  
Is it possible?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 8, 2024, 9:55am UTC](https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421/2 "2024-02-08T09:55:40Z")

</div>

Project Browser → Scripting → Gateway Events → Tag Change.

[https://docs.inductiveautomation.com/display/DOC81/Gateway+Event+Scripts#GatewayEventScripts-TagChangeScripts](https://docs.inductiveautomation.com/display/DOC81/Gateway+Event+Scripts#GatewayEventScripts-TagChangeScripts)

---

<div class="post-metadata">

**Author:** ![Sachin\_Kamble](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Sachin\_Kamble](https://forum.inductiveautomation.com/u/Sachin_Kamble)\
**Post date:** [February 8, 2024, 11:23am UTC](https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421/3 "2024-02-08T11:23:36Z")

</div>

I have to add value changed script while creating tags using 'system.tag.configure'

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 8, 2024, 12:42pm UTC](https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421/4 "2024-02-08T12:42:15Z")

</div>

**Crude way**  
Create pairs of tags - one OPC tag and one expression tag.

- Set the expression tag Execution Mode = Event Driven.
- Add an expression to read the OPC tag and use the [runscript](https://docs.inductiveautomation.com/display/DOC81/runScript) expression function to call a gateway script. Pass the tag path as an argument.

**Better way**

- Create a UDT for your tag. (This assumes that they're all the same type.)
- In the UDT → Categories → Scripting → Value Changed event, add your script.

I did one that is like this (edited to remove sensitive information - so there may be errors):

```auto
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	# Retrieve Values
	project = 'myDashboard'
	payload = {}
	paths = ['[.]machineId', '[.]countTotal', '[.]countGood', '[.]firstOutAlarm', '[.]sku']
	values = system.tag.readBlocking(paths)
	
	# Pack values into a dictionary	
	payload['machineID'] = values[0].value
	payload['runStatus'] = currentValue.value
	payload['cycleCount'] = values[1].value
	payload['goodCount'] = values[2].value
	payload['firstOutAlarm']	= values[3].value
	payload['SKU'] = values[4].value
			
	# Send Message To The Gateway
	result = system.util.sendMessage(project, 'insertMachineEvent', payload)

```

Now your tag creation script needs to include the UDT as the parent type.

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [February 8, 2024, 2:49pm UTC](https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421/5 "2024-02-08T14:49:43Z")

</div>

You don't need the expression script with runScript step. You can in fact use `system.tag.getConfiguration` and `system.tag.configure` to add a Jython script to a tag.

I use a chunk quite like this in one of my projects:

```auto
# Get the current settings of the source tag (we assume the tag already exists)
tag_config = system.tag.getConfiguration(source_metric_path, False)

# Update the config with the desired valueChanged script
# WARNING: This will remove any scripts for event types other than "valueChanged"
tag_config[0]['eventScripts'] = [
	{ "eventid":"valueChanged", "script":"\treturn foo.valueChangedHandler(tag, tagPath, previousValue, currentValue, initialChange, missedEvents)\n" }
]

# Push the new config with valueChanged script into place
system.tag.configure(source_metric_parent_folder, tag_config, 'o')

```

But it's still probably a cruder solution than using UDTs.

---

<div class="post-metadata">

**Author:** ![Sachin\_Kamble](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Sachin\_Kamble](https://forum.inductiveautomation.com/u/Sachin_Kamble)\
**Post date:** [February 9, 2024, 9:16am UTC](https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421/6 "2024-02-09T09:16:14Z")

</div>

Thanks !!!

---

<div class="post-metadata">

**Author:** ![Sachin\_Kamble](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Sachin\_Kamble](https://forum.inductiveautomation.com/u/Sachin_Kamble)\
**Post date:** [February 21, 2024, 9:35am UTC](https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421/7 "2024-02-21T09:35:19Z")

</div>

I have to add below script on tag value change script while creating tag using script. I tried with your suggestion but showing error at " " ".

Script to add -

import os, os.path  
database = system.tag.read("[default]DBName").value  
dbconfig = system.tag.read("[default]/DBConfiguration").value

query = """SELECT TOP 1 CONCAT(  
TagNumber  
, '\_'  
, FileID  
, '.'  
, (CASE  
WHEN [FileName] like '%.%' THEN reverse(left(reverse([FileName]), charindex('.', reverse([FileName])) - 1))  
ELSE ''  
END)  
) AS FileName  
, [FileData]  
, [FileID]  
FROM [""" + database + """].[dbo].[Files]  
WHERE FileUploaded = 0  
ORDER BY FileTimestamp ASC"""

result = system.db.runQuery(query, dbconfig)

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [February 21, 2024, 10:23am UTC](https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421/8 "2024-02-21T10:23:54Z")

</div>

That looks like a bad idea. I haven't encountered the situation, but from what I've gathered reading this forum, doing database operations in tag change scripts may cause your system to hang while it's processing.

Is that script the same for every tag you create ?

side notes

- you don't seem to be using your imports.
- is that the whole script ? Are you doing something with `result` ?
- you can pass a list to `system.tag.readBlocking` to bulk read tags. It's way more efficient than reading them individually. By the way, `system.tag.read` and `system.tag.write` have been deprecated for ages.
- I suggest you use string formatting to build query strings, that way it's obvious it's not a constant
- You probably shouldn't be using `runQuery`, but `runPrepQuery` instead, when the query takes parameters
- named queries are probably even better in your case

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 21, 2024, 10:52am UTC](https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421/9 "2024-02-21T10:52:55Z")

</div>

Please see [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831). You can fix your last post using the pencil icon below the post.

---

<div class="post-metadata">

**Author:** ![Sachin\_Kamble](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Sachin\_Kamble](https://forum.inductiveautomation.com/u/Sachin_Kamble)\
**Post date:** [February 21, 2024, 11:16am UTC](https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421/10 "2024-02-21T11:16:20Z")

</div>

Thanks !!!

---

<div class="post-metadata">

**Author:** ![Sachin\_Kamble](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Sachin\_Kamble](https://forum.inductiveautomation.com/u/Sachin_Kamble)\
**Post date:** [February 21, 2024, 11:18am UTC](https://forum.inductiveautomation.com/t/add-value-change-script-to-a-tag/85421/11 "2024-02-21T11:18:05Z")

</div>

Thanks for side notes those will be helpful in future.  
I had used different approach to run the script.
