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

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 :confused:

  • I am able to execute INSERT query on a ‘button’ pressed event. But, here I want to do this on a tag value.

  • Already prepared a named query for INSERT operation.

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.

1 Like

What @PGriffith 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.


Here’s how to do the tag change.

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

Put this code into the expression:

// Start counting minutes when the batch starts.
// Return a zero if the batch has stopped.
	round((now() - {[~]Start_Process_Latch.Timestamp}) / 1000 / 60, 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.

1 Like

@PGriffith 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.