# INSERT Query binding to a Tag value and it should insert for every 1 minute

**URL:** <https://forum.inductiveautomation.com/t/insert-query-binding-to-a-tag-value-and-it-should-insert-for-every-1-minute/59921>\
**Category:** Ignition\
**Tags:** perspective, scripting, database\
**Created:** [May 23, 2022, 5:51pm UTC](https://forum.inductiveautomation.com/t/insert-query-binding-to-a-tag-value-and-it-should-insert-for-every-1-minute/59921 "2022-05-23T17:51:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shaheryar\_Ahmad](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@Shaheryar\_Ahmad](https://forum.inductiveautomation.com/u/Shaheryar_Ahmad)\
**Post date:** [May 23, 2022, 5:51pm UTC](https://forum.inductiveautomation.com/t/insert-query-binding-to-a-tag-value-and-it-should-insert-for-every-1-minute/59921/1 "2022-05-23T17:51:44Z")

</div>

Hi Everyone.

I have a machine of which getting its ‘start process’ boolean status over OPCUA. From that tag, I can see when the machine is running or not.  
From that tag status, I want to INSERT some machine related data into MySQL table.

Condition: ( **When the machine is running, the machine’s ‘start process’ tag value changes to 1 and than the INSERT query has to run for inserting machine data into database _for every 1 minute interval_. When the machine stopped running, the Insert query has to stop. Same process start, when again machine starts running** )

Please guide how can I do this 😕

- I am able to execute INSERT query on a ‘button’ pressed event. But, here I want to do this on a tag value.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/4/642eadf48dad0d3b91a0f5dfa2af0f9410f02499.png)

- Already prepared a named query for INSERT operation.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 23, 2022, 5:58pm UTC](https://forum.inductiveautomation.com/t/insert-query-binding-to-a-tag-value-and-it-should-insert-for-every-1-minute/59921/2 "2022-05-23T17:58:48Z")

</div>

The easiest way would be a transaction group.

The next best way would probably be a gateway timer script (running on a 1 minute interval) that first checks your ‘trigger tag’, and then conditionally runs your named query.

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [May 23, 2022, 6:02pm UTC](https://forum.inductiveautomation.com/t/insert-query-binding-to-a-tag-value-and-it-should-insert-for-every-1-minute/59921/3 "2022-05-23T18:02:27Z")

</div>

What @paul-griffith said. But if you require the data be recorded exactly when the tag changed to true and exactly 1 minute intervals starting from that point and want to do your inserts with a named query, you may want to consider incrementing an integer every minute while the machine is running and using a tag change script to run your query.

---

<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 23, 2022, 7:22pm UTC](https://forum.inductiveautomation.com/t/insert-query-binding-to-a-tag-value-and-it-should-insert-for-every-1-minute/59921/4 "2022-05-23T19:22:09Z")

</div>

Here’s how to do the tag change.

 ![Start process latch](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/1/d169de8867120add3f95f845b49357ea7f8abdbc.png)

In the _Script_ tab you would check that the tag is true before executing your query.

If you want your 1 minute time to start counting from the batch start (rather than the system clock rolling over to :00 seconds, etc.) then you could create a memory tag.

![1-minute timer tag](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/1/4119bc0654816b164c3a7b5c0731689f24b5fff9.png)

Put this code into the expression:

```auto
// Start counting minutes when the batch starts.
// Return a zero if the batch has stopped.
if(
	{[~]Start_Process_Latch},
	round((now() - {[~]Start_Process_Latch.Timestamp}) / 1000 / 60, 0),
	0
)

```

(You need to browse for your _Start\_Process\_Latch_ tag so that you get the full path.)

Change the `/ 60` to something like 3 s to speed up debugging.

---

<div class="post-metadata">

**Author:** ![Shaheryar\_Ahmad](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@Shaheryar\_Ahmad](https://forum.inductiveautomation.com/u/Shaheryar_Ahmad)\
**Post date:** [June 19, 2022, 5:42pm UTC](https://forum.inductiveautomation.com/t/insert-query-binding-to-a-tag-value-and-it-should-insert-for-every-1-minute/59921/5 "2022-06-19T17:42:05Z")

</div>

@paul-griffith Thankyou so much for the guide. I have used the transaction group technique for inserting values to database on tag change.And, I find it quite efficient, what you say? as I have to implement it for a long duartion.

- Extremely sorry, I completely forgot to reply on this post.
