# Inserting PLC values into a table with a Gateway Event Tag Change Script

**URL:** <https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235>\
**Category:** Ignition\
**Tags:** database, scripting, ignition81, perspective\
**Created:** [May 10, 2024, 2:33pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235 "2024-05-10T14:33:55Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 10, 2024, 2:33pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/1 "2024-05-10T14:33:55Z")

</div>

Hi all,

I have created a Named Query that inserts two parameters (values) into a table. I would like to have a Gateway Tag Change Script that runs the Named Query when a PLC tag changes and provides the PLC values to the Named Query.

Here is my admittedly very ugly Gateway Tag Change Script:

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):	
	LiftRaised = system.tag.readBlocking(["[default]Station/ACell_15/Stations/Unload/PLC/Lift raised"])[0].value
	if previousValue.value != currentValue.value and initialChange == False and currentValue.value == 1 and LiftRaised == True:
		CycleTime = system.tag.readBlocking(['[default]Station/ACell_15/Stations/Unload/StationCycleTime'])[0].value - system.tag.readBlocking(['[default]Station/ACell_15/Stations/Unload/CycleCountdownTime'])[0].value
		Delta =system.tag.readBlocking(['[default]Station/ACell_15/Stations/Unload/StationCycleTime'])[0].value - CycleTime
		#system.tag.writeBlocking(["[.]../HistorianTags/CompletedCycleTime", "[.]../HistorianTags/CycleDelta"], [CycleTime, Delta])
		parameters = {'CompletedCycleTime':CycleTime, 'CycleDelta':CycleDelta}
		system.db.runNamedQuery(ACell_15_Data_Insert, parameters)

```

It does not have errors that I can find, but on the tag change nothing happens ☹

Am I going about this correctly? Can this even work? I have been stuck on this for a while and would really appreciate some help!

Thank you,  
Luke M. Smith

edit: here is my named query script

```python

INSERT INTO ACell15_Pacing
	(CompletedCycleTime, CycleDelta)
VALUES
	(:CompletedCycleTime, :CycleDelta)

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 10, 2024, 2:45pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/2 "2024-05-10T14:45:06Z")

</div>

Is this script exactly as you posted it, what you have in the script window of the Gateway Tag Change Script?

If so, then nothing is happening because you are defining a function, but then never calling it. so it never gets executed.

Instead what you should do is create a Library Script perhaps something like this:

```python
def insertCycleData():
    tagPaths = ['[default]Station/ACell_15/Stations/Unload/StationCycleTime', '[default]Station/ACell_15/Stations/Unload/StationCycleTime']
    cycleTime, cycleDelta = [qv.value for qv in system.tag.readBlocking(tagPaths)]
    system.db.runNamedQuery('ACell_15_Data_Insert', {'CompletedCycleTime':cycleTime, 'CycleDelta': cycleDelta})

```

Then your Tag Change script could be simply:

```python
LibraryName.insertCycleData()

```

Here is the User manual page on Gateway Tag Change Scripts, that will show how to do things like check against initial change.

> **[Gateway Event Scripts | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-in-ignition/gateway-event-scripts#tag-change-scripts)**
>
> Gateway Event Scripts Overview

---

<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:** [May 10, 2024, 3:18pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/3 "2024-05-10T15:18:31Z")

</div>

> [@Luke\_Smith1](#):
>
> ```python
> def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):	
> 
> ```

That's a tag value change script, not a gateway event.

Start by just logging things, to make sure they are actually what you think they are.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 10, 2024, 4:17pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/4 "2024-05-10T16:17:03Z")

</div>

Actually, I suspect that it started as a Tag value change script, but is being refactored as a Gateway Tag Change event.

> [@addSeconds in Expression scripting not working with Tag value](https://forum.inductiveautomation.com/t/addseconds-in-expression-scripting-not-working-with-tag-value/90152/16):
>
> Hi lrose, Thank you for the input, this is my first ignition project in years so I am relearning everything as I go. Where and how to script to be the most efficient is where I am struggling the most, so your comment is very helpful. Thank you! I will try to switch it over to a Gateway Tag Change Event.

Based on previous context, that arguably you wouldn't have if you didn't know to go look.

---

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 10, 2024, 4:30pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/5 "2024-05-10T16:30:31Z")

</div>

yup you are correct. I copy-pasted and left out the row starting with "def". But then I was getting errors and adding the "def" line in fixed it. Once this project is over im going back to the basics to learn from the ground up. Thanks for all your help,lrose.

---

<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:** [May 10, 2024, 4:42pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/6 "2024-05-10T16:42:34Z")

</div>

It fixed the errors because it made the whole thing obsolete - the function doesn't run, so it can't produce errors.

I believe that's the code you want for a gateway event tag change script:

```python
if not initialChange and newValue.value == 1:
	paths = [
		"[default]Station/ACell_15/Stations/Unload/PLC/Lift raised",
		"[default]Station/ACell_15/Stations/Unload/StationCycleTime",
		"[default]Station/ACell_15/Stations/Unload/CycleCountdownTime"
	]
	lift_raised, station_cycle_time, cycle_countdown_time = [qv.value for qv in system.tag.readBlocking(paths)]
	if lift_raised:
		cycle_time = station_cycle_time - cycle_countdown_time
		cycle_delta = station_cycle_time - cycle_time
		parameters = {
			'CompletedCycleTime': cycle_time,
			'CycleDelta': cycle_delta
		}
		system.db.runNamedQuery("ACell_15_Data_Insert", parameters)

```

---

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 10, 2024, 5:25pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/7 "2024-05-10T17:25:42Z")

</div>

This works like a charm! thank you both.

@lrose I really like your idea of using the library object. I set up the `def insertCycleData()` script in the project library and then changed my Gateway Tag Change Script to `LibraryName.insertCycleData()`. It did not work, so I hardcoded values and simplified it as much as possible to learn how it works. I could not get it to work. Is there more setup I need with this method?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 10, 2024, 5:32pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/8 "2024-05-10T17:32:53Z")

</div>

> [@Luke\_Smith1](#):
>
> LibraryName.insertCycleData()

LibraryName is just a place holder. So, for instance say you added a new script to the project library that you named, myScript. You place your function definition inside of that script resource.

Then you would call that like this:

```python
myScript.insertCycleData()

```

Since I don't know what you named your script resource (or if you placed it in a folder) then I cant tell you exactly what the call should be. So long as the script and event are defined inside the same project there is nothing else that needs to be done for this to work.

---

<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:** [May 10, 2024, 5:33pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/9 "2024-05-10T17:33:29Z")

</div>

I actually should have included that part in my answer.  
There are weird things happening in gateway event scripts and you should get the habit of not coding in them, but just calling scripts that are in your library.

---

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 10, 2024, 5:37pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/10 "2024-05-10T17:37:40Z")

</div>

I suspected that was the case. thank you again!

---

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 24, 2024, 5:36pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/11 "2024-05-24T17:36:49Z")

</div>

I moved my script into a library script per these recommendations. Now when I save my project, my table gets an overlay error until the process is ran and it updates. Is there a way to avoid this?

---

<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:** [May 24, 2024, 5:42pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/12 "2024-05-24T17:42:55Z")

</div>

I haven't used it but have a look at the Table component's [emptyMessage.noData](https://docs.inductiveautomation.com/docs/8.1/appendix/components/perspective-components/perspective-display-palette/perspective-table#emptymessagenodata-properties) documentation.

---

<div class="post-metadata">

**Author:** ![Luke\_Smith1](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@Luke\_Smith1](https://forum.inductiveautomation.com/u/Luke_Smith1)\
**Post date:** [May 24, 2024, 5:44pm UTC](https://forum.inductiveautomation.com/t/inserting-plc-values-into-a-table-with-a-gateway-event-tag-change-script/90235/13 "2024-05-24T17:44:18Z")

</div>

Perfect, Thank you Transistor
