Add value change script to a tag


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?

Project Browser → Scripting → Gateway Events → Tag Change.

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

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 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):

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.

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:

# 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.


Thanks !!!

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 ="[default]DBName").value
dbconfig ="[default]/DBConfiguration").value

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

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

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, 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

Please see Wiki - how to post code on this forum. You can fix your last post using the pencil icon below the post.

Thanks !!!

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