# Tag Event Scripts vs Gateway Tag Change Scripts

**URL:** <https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098>\
**Category:** General Discussion\
**Created:** [April 21, 2017, 12:27am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098 "2017-04-21T00:27:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [April 21, 2017, 12:27am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098/1 "2017-04-21T00:27:52Z")

</div>

Hi all,

Just looking for some advice/ recommendations about which script handle to use for doing an action on a Tag Change event.

At this stage, I am trying to run a SQL query when a boolean tag value goes True.

There are two places to define such a script:

1. Gateway Event Scripts -\> Tag Change
2. Tag-\> Tag Events-\>Value Changed

Are there any benefits to using one over the other?  
Having them all in one place in the Gateway Event Scripts would be easy to see and manage in one place…  
But the Tag Event scripts look like they have more parameters to utilize.

---

<div class="post-metadata">

**Author:** ![accyroy](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/accyroy/32/3386_2.png) [@accyroy](https://forum.inductiveautomation.com/u/accyroy)\
**Post date:** [April 21, 2017, 12:44am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098/2 "2017-04-21T00:44:21Z")

</div>

We use the Tag-\>Tag Events-\>Value Changed. Mainly because you can then utilize UDT’s with tag change scripts for multiple items. It also keeps everything to do with tags in the tag folders, so we can easily move tags between systems and retain all scripting.

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [April 21, 2017, 4:41am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098/3 "2017-04-21T04:41:34Z")

</div>

Thanks for the reply, thats a good idea about the UDT's.  
Just had a look at what i've been doing, and decided I need to group my 5 individual tags into a UDT.

Just wondering, how do you reference UDT Properties or UDT Tags in a Tag Event Script?

I've tried to create a memory tag in my UDT to contain some parameter values, but i haven't got it working.

My SQL Query:

```auto
query = """INSERT INTO QATestRecord (TestType) VALUES (?)"""
args = ["[.]TestType"]
system.db.runPrepUpdate(query, args, db)

```

SQL table result for column TestType:

> [.]TestType

I cant get rid of the " " because then the Python compiler isn't happy.

---

<div class="post-metadata">

**Author:** ![Paul.Scott](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul.scott/32/4578_2.png) [@Paul.Scott](https://forum.inductiveautomation.com/u/Paul.Scott)\
**Post date:** [April 21, 2017, 5:56am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098/4 "2017-04-21T05:56:57Z")

</div>

Hi there!

Correct me if I’m wrong, but it sounds like you’re trying to insert the value of the “TestType” tag into your database table, but the script is taking the string literal and inserting that into the database. Is this correct? If so, then the problem is with the argument.

In a script, you can read the value of a tag with a call to system.tag.read(), as opposed to using a string literal of the tag path (which is the correct syntax for Ignition’s expression language). Also, when specifying a tag path in a gateway scoped script, it’s a good practice to be explicit with the tag provider. Try modifying your code to something like the following (assuming the tag is located in the provider named ‘default’):

```auto
query = "INSERT INTO QATestRecord (TestType) VALUES (?)"
args = [system.tag.read([default]TestType).value]
system.db.runPrepUpdate(query, args, db)

```

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [April 21, 2017, 6:13am UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098/5 "2017-04-21T06:13:33Z")

</div>

Hi,

You’re on the right track, I am writing a Tag Event change script for the following UDT tag on the **TestNow** tag:

**Gisborne01/ProdSystems/QATests/QA\_BConcTest**

But the tag i want to insert into my query is part of the same UDT as the tag change script. ie: tag **TestType**. So i was hoping there was a neater way rather than using

```auto
system.tag.write()

```

This is the UDT i am using:

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/b/b563d34ea569f02fb497cd03d623c5e702d11819.PNG)

---

<div class="post-metadata">

**Author:** ![accyroy](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/accyroy/32/3386_2.png) [@accyroy](https://forum.inductiveautomation.com/u/accyroy)\
**Post date:** [April 21, 2017, 10:32pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098/6 "2017-04-21T22:32:55Z")

</div>

To reference UDT properties you must create an expression tag in your UDT, so in your example if you want the UDT TestType tag to contain the contents of the TestType parameter, the tag in the UDT must be an expressiontag and the expression is “{TestType}”.  
To reference tags inside your UDT from the tag event scripts you use [.].  
So if you want to read the contents of the TestType tag in the event script you need:  
testtype = system.tag.read("[.]TestType")  
Then you use testtype.value in your code.

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [April 23, 2017, 8:48pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098/7 "2017-04-23T20:48:15Z")

</div>

That worked great, thanks!

For another function im trying to write on the same UDT, how can i write to an expression tag “TestTime”?

The following code doesn’t seem to work.

```auto
	system.tag.write("[.]TestTime", system.date.now())

```

Neither does adding tag provider “[Default]”.

---

<div class="post-metadata">

**Author:** ![accyroy](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/accyroy/32/3386_2.png) [@accyroy](https://forum.inductiveautomation.com/u/accyroy)\
**Post date:** [April 24, 2017, 5:27pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098/8 "2017-04-24T17:27:33Z")

</div>

That code should definitely work, can you double check your TestTime tag is of the datatype Date.

---

<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:** [April 24, 2017, 10:05pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098/9 "2017-04-24T22:05:56Z")

</div>

Why are you trying to write to an expression tag? They get their value from their expression on every scan. Did you mean a memory tag?

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [April 25, 2017, 8:11pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098/10 "2017-04-25T20:11:54Z")

</div>

Yes my TestTime tag is a Date datatype.

And sorry I had a typo there.  
I am trying to write into a memory tag (TestTime), using a different expression tag (TestNow).

**Edit:** Solved it, was a combination of a couple of small silly mistakes. Typo in the tag name i was trying to write, and the Tag Change script on the tag instance being overridden, so recent changes weren’t pushed through.

---

<div class="post-metadata">

**Author:** ![jsorlie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jsorlie/32/3645_2.png) [@jsorlie](https://forum.inductiveautomation.com/u/jsorlie)\
**Post date:** [April 27, 2018, 6:09pm UTC](https://forum.inductiveautomation.com/t/tag-event-scripts-vs-gateway-tag-change-scripts/14098/12 "2018-04-27T18:09:43Z")

</div>

Thanks PTurmel. Learn something every day!
