Unable to run gateway event script

Hello, so I’m having an issue running a gateway event script. I am using the tag change script however my script won’t run whenever the tag is changed. Whenever I run the script in the script console it works just fine though.

The script is:
CIP_End = system.tag.read(‘Controller:Global/CIP_End_SQL’).value
CIP_CircuitSel = system.tag.read(‘Controller:Global/CIP_CircuitSel’).value
T_Stamp = system.tag.read(‘Controller:Global/CIP_TStamp’).value
CIP_Start = system.tag.read(‘Controller:Global/CIP_Start_SQL’).value
UniqueID = system.tag.read(‘Controller:Global/UniqueID’).value

system.db.runNamedQuery(“Insert Data”, {“CIP_End”:CIP_End, “CIP_CircuitSel”:CIP_CircuitSel, “T_Stamp”:T_Stamp, “CIP_Start”:CIP_Start, “UniqueID”:UniqueID})

It’s a scope issue.
The script console knows what project to use but the tag doesn’t.
Add the project name as the first of 3 parameters in your named query call.

system.db.runNamedQuery( ‘myProjectName’, ‘namedQueryPath’, paramsDictionary )

So I went ahead and added in my project name but the query still isn’t triggering.

Be sure the tag provider is included in your tag names.

So my tags are in a folder called tags. So would I need to structure it Tags:Controller:Global/tagname?

Gateway scripts need the fully qualified path to the tag:

CIP_End = system.tag.read('[default]path/to/your/tag').value

I think @JordanCClark is referring to the system.tag.read statements.
The best way to guarantee that they are correct is to right-click on the tag and select ‘Copy Tag Path’ from the option menu and paste that into your script.

After looking more closely at your tags, you might want to be using:
system.opc.readValue instead of system.tag.read
I’ve never needed to include ‘Controller.Global’
To guarantee that you get the correct tag path for an opc readValue, you can use the OPC Quick Client and browse to the tag on the device and [r]ead it, then copy the path from the read results display.

Alright so I tried the system.opc.readValue method. So my tag is:
system.opc.readValue(“Ignition OPC-UA Server”, ‘[Crystal_Geyser_PLC]CIP1Report.StartTime’)

I still can’t seem to get the query to execute. I did have one question using that method, one of the tags I’m using is a memory tag which obviously has no opc address, how would I go about still using it?

There seem to be two separate questions here:

  1. how do I read tag values
  2. why doesn’t the script run correctly.
    I’m guessing that you are using Ignition 7.9.x not 8.0.x

I’m sorry if this is overkill but I’ve put a lot of comments into the script below as an example of several things.

  • reading Ignition mapped tags
  • reading OPC Device tags
  • efficiently reading multiple tags in a single statement
  • logging to see the progress of a script to know how far it gets and what might cause failures
# initialize variables
opcServer = 'Ignition OPC-UA Server'
opcDevice = 'Crystal_Geyser_PLC'
projectName = 'myProject'
namedQuery = 'Insert Data'
queryResult = 'Failed to execute'	# this will be changed if the query runs successfully

# get a logger for debugging
logger = system.util.getLogger( 'tagTesting' )

# initialize lists and dictionaries for future expansion and readability
tagPaths = []		# a list of tags in the Ignition Tag Browser
opcPaths = []		# a list of opc tags in Devices, usually not configured in the Tag Browser
queryParams = {}	# initialize an empty dictionary

tagPaths.append( 'CIP_CircuitSel' )	# I'm guessing a user selected item known to Ignition not to the opcDevice
tagValues = system.tag.readAll( tagPaths ) # readAll doesn't stop the script if it's a bad tag
# log the results
logger.info( 'tagValues: %s' %str(tagValues) )
# assign tag values to variables
CIP_CircuitSel = tagValues[0].value

opcPaths.append( '[%s]CIP_End_SQL'	%opcDevice )
opcPaths.append( '[%s]CIP_TStamp'	%opcDevice )
opcPaths.append( '[%s]CIP_Start_SQL'%opcDevice )
opcPaths.append( '[%s]CIP_UniqueID' %opcDevice )
opcValues = system.opc.readValues( opcServer, opcPaths )
# log the results
logger.info( 'opcValues: %s' %str(opcValues) )
# assign opc values to variables
CIP_End		= opcValues[0].value
T_Stamp		= opcValues[1].value
CIP_Start	= opcValues[2].value
UniqueID	= opcValues[3].value

# put values into a dictionary for the named query
queryParams['CIP_CircuitSel']	= CIP_CircuitSel
queryParams['CIP_End']			= CIP_End
queryParams['T_Stamp']			= T_Stamp
queryParams['CIP_Start']		= CIP_Start
queryParams['UniqueID']			= UniqueID
# Note: I normally read the database from a tag and use a database parameter for all Named Queries
#		this allows me to move from development to test to production without code changes
#queryParams['database'] = database

# log the params
logger.info( 'queryParams: %s' %str(queryParams) )

try:
	queryResult = system.db.runNamedQuery( projectName, namedQuery, queryParams )
except:
	pass
logger.info( 'queryResult: %s' %str(queryResult) )

Now, when a tag change event occurs, you can watch the Ignition Gateway Status Logs and see what happens when the tag value changes.
Once you get it running, you can delete the logging

1 Like

Thank you so much for this, as it’s a huge help with the logging and everything. However when it comes to the TagPaths, and the OPCtag paths I entered those in but it’s still not finding my tags, it’s returning a null value. The tag path should just be Tags/controller/global correct? And then my OPCtag path would be Crystal_Geyser_PLC? Thank you again for the help

As Jordan said, just copy the tag path from the tag’s context menu. Then you’ll get the fully qualified tag path including the tag provider. You tag path will be something like:
'[default]area 1/sub-area 2/device A/tag'

Note, this is the ignition tag path, not the tag’s OPC item path!
This is assuming you’re using system.tag.read() (which you probably should be using). This function reads tags that you have defined in the tag browser.

I havent used system.opc.read but I presume this will read the tag directly from the tag provider connection itself, basically the raw tag which doesn’t contain any scaling, meta data, documention, alarms etc that you configure on a tag in the tag browser

No,it bypasses the tag system entirely and reads directly from the OPC server.

There's a tendency to call OPC items 'tags', and they may very well be tags in the connected PLC, but they aren't tags in Ignition until they are dragged into Ignition's tag tree (or created there manually).

Scripting functions under system.tag.* work with Ignition tags. Scripting functions under system.opc.* work with OPC items.

3 Likes

Also on any system.tag.read or system.opc.readValue calls you will have to specify the .value on the end.
The calls return a Qualified Value Object and not just the value of the tag.

CIP1StartTime = system.opc.readValue(“Ignition OPC-UA Server”, ‘[Crystal_Geyser_PLC]CIP1Report.StartTime’).value

CIP_End = system.tag.read('[default]path/to/your/tag').value
3 Likes

Thank you all so much for the help, I finally got it working. Just one quick thing, it’s entering multiple entries into the SQL whenever the script gets executed. Is there any way to prevent this?

That will be in your code.
The database won’t do that, especially if you have correct primary keys in place on the table.

2 Likes

I would place some prints inside your script to see if it’s executing twice, and print out the various values from the event to see what it’s triggering on. Prints from the gateway scope will write into the wrapper.log, so make sure you put something in the print that you can search for