# Writing to Database from a Tag Value Change Script

**URL:** <https://forum.inductiveautomation.com/t/writing-to-database-from-a-tag-value-change-script/99389>\
**Category:** Ignition\
**Tags:** database, ignition81, perspective, scripting\
**Created:** [December 3, 2024, 11:16pm UTC](https://forum.inductiveautomation.com/t/writing-to-database-from-a-tag-value-change-script/99389 "2024-12-03T23:16:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Cooper1](https://avatars.discourse-cdn.com/v4/letter/c/9de053/32.png) [@Christopher\_Cooper1](https://forum.inductiveautomation.com/u/Christopher_Cooper1)\
**Post date:** [December 3, 2024, 11:16pm UTC](https://forum.inductiveautomation.com/t/writing-to-database-from-a-tag-value-change-script/99389/1 "2024-12-03T23:16:49Z")

</div>

I am trying to add rows to the database every time a tag changes state. Specifically, every time a Boolean tag becomes true, I want to take the name of that tag and add it into a new row in the database table.

I have tried using value change scrips on the tag itself, and inside of the script calling a named query.

I have also tried using query tags, but that did not work either.

Anyone have any advice on a better way to do this?  
Thanks

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [December 3, 2024, 11:25pm UTC](https://forum.inductiveautomation.com/t/writing-to-database-from-a-tag-value-change-script/99389/2 "2024-12-03T23:25:59Z")

</div>

Are you trying to watch/log multiple tags? If so, use a Gateway Tag Change event in a project. Add the target tag(s) to the monitored tag list, and configure your script to check that the value transitioned from 0 to 1 and pull the name of the tag (`event.tagPath.toStr()`) and insert it into the db table.

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

This gives you more flexibility than a transaction group, as a Gateway Tag Change event can be set to monitor all tags in a folder by using wildcards.

---

<div class="post-metadata">

**Author:** ![stevenson](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stevenson](https://forum.inductiveautomation.com/u/stevenson)\
**Post date:** [December 3, 2024, 11:32pm UTC](https://forum.inductiveautomation.com/t/writing-to-database-from-a-tag-value-change-script/99389/3 "2024-12-03T23:32:18Z")

</div>

Do you have a default gateway scoped project set up?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/5/f5e065d18b381e175e3b89d51d0b810bb972d514.png)  
Named queries are scoped to a project, and if the project isn't identified here then the tag event script would be unable to use the resource.

To simplify, use a simple runPrepUpdate on the tag event script.

```python
	if currentValue.value == 1: # rising
		system.db.runPrepUpdate(query, [], "DBConnName")

```

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [December 3, 2024, 11:36pm UTC](https://forum.inductiveautomation.com/t/writing-to-database-from-a-tag-value-change-script/99389/4 "2024-12-03T23:36:09Z")

</div>

Do **NOT** interact with the DB in a Tag Value Change script. You have a limited amount of threads (3) to handle all tag events. Any blocking work will cause issues with other tags. Tag Value Change scripts need to execute in the single digit milliseconds to avoid impacting the tag system.

Use a Gateway Tag Change Event instead. Those spawn threads as needed. They are also much more visible to other engineers (there is a dedicated page on the gateway showing active Gateway Tag Change Event scripts).

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [December 3, 2024, 11:44pm UTC](https://forum.inductiveautomation.com/t/writing-to-database-from-a-tag-value-change-script/99389/5 "2024-12-03T23:44:05Z")

</div>

In the same project as the Gateway Tag Change event, create a project library script with something along the lines of

```python
def onTagChanged(initialChange, executionCount, newValue, previousValue, event):
	""" """
	# If script just started or tag just initialized, don't do anything
	if initialChange or not previousValue:
		return

	# Catch Transition from 0 to 1 and log tag path to db
	if not previousValue.value and newValue.value:
		tagPath = str(event.tagPath)
		system.db.runNamedQuery("ThisProject", "InsertTagRecord", params={"tagPath": tagPath})

	return

```

and call that from the Gateway Tag Change event script, ie `onTagChanged(initialChange, executionCount, newValue, previousValue, event)`

---

<div class="post-metadata">

**Author:** ![Christopher\_Cooper1](https://avatars.discourse-cdn.com/v4/letter/c/9de053/32.png) [@Christopher\_Cooper1](https://forum.inductiveautomation.com/u/Christopher_Cooper1)\
**Post date:** [December 3, 2024, 11:52pm UTC](https://forum.inductiveautomation.com/t/writing-to-database-from-a-tag-value-change-script/99389/6 "2024-12-03T23:52:44Z")

</div>

I am just working on a demo project for now, so I'm only dealing with 3-5 tags I need to track. But I will definitely look into Gateway Tag Change Events, and thank you for linking the manual page, I was looking for that for a while.

---

<div class="post-metadata">

**Author:** ![Christopher\_Cooper1](https://avatars.discourse-cdn.com/v4/letter/c/9de053/32.png) [@Christopher\_Cooper1](https://forum.inductiveautomation.com/u/Christopher_Cooper1)\
**Post date:** [December 3, 2024, 11:53pm UTC](https://forum.inductiveautomation.com/t/writing-to-database-from-a-tag-value-change-script/99389/7 "2024-12-03T23:53:45Z")

</div>

I looked and I was missing that part entirely. After adding it back in, the script was able to execute. Thanks for the tip

---

<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:** [December 4, 2024, 8:53am UTC](https://forum.inductiveautomation.com/t/writing-to-database-from-a-tag-value-change-script/99389/8 "2024-12-04T08:53:15Z")

</div>

I think you've marked a bad answer as the correct solution. This will be misleading for anyone asking the same question and finding yours.
