# Unable to run gateway event script

**URL:** <https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883>\
**Category:** Ignition\
**Created:** [November 12, 2019, 3:37pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883 "2019-11-12T15:37:02Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [November 12, 2019, 3:37pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/1 "2019-11-12T15:37:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martinel3746](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@martinel3746](https://forum.inductiveautomation.com/u/martinel3746)\
**Post date:** [November 12, 2019, 3:41pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/2 "2019-11-12T15:41:13Z")

</div>

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 )

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [November 12, 2019, 4:14pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/3 "2019-11-12T16:14:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 12, 2019, 4:15pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/4 "2019-11-12T16:15:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [November 12, 2019, 5:04pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/5 "2019-11-12T17:04:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 12, 2019, 5:16pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/6 "2019-11-12T17:16:03Z")

</div>

Gateway scripts need the fully qualified path to the tag:

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

```

---

<div class="post-metadata">

**Author:** ![martinel3746](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@martinel3746](https://forum.inductiveautomation.com/u/martinel3746)\
**Post date:** [November 12, 2019, 5:24pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/7 "2019-11-12T17:24:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [November 12, 2019, 6:17pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/8 "2019-11-12T18:17:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![martinel3746](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@martinel3746](https://forum.inductiveautomation.com/u/martinel3746)\
**Post date:** [November 12, 2019, 10:02pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/9 "2019-11-12T22:02:16Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [November 13, 2019, 1:32am UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/10 "2019-11-13T01:32:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 13, 2019, 3:35am UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/11 "2019-11-13T03:35:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 13, 2019, 1:57pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/12 "2019-11-13T13:57:22Z")

</div>

> [@nminchin](#):
>
> I havent used `system.opc.read` but I presume this will read the tag directly from the tag provider connection itself

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** _.

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [November 13, 2019, 2:23pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/13 "2019-11-13T14:23:30Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [November 13, 2019, 5:04pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/14 "2019-11-13T17:04:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![martinel3746](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@martinel3746](https://forum.inductiveautomation.com/u/martinel3746)\
**Post date:** [November 13, 2019, 10:51pm UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/15 "2019-11-13T22:51:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 14, 2019, 6:40am UTC](https://forum.inductiveautomation.com/t/unable-to-run-gateway-event-script/29883/16 "2019-11-14T06:40:36Z")

</div>

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
