Getting the value of a counter before it's reset

I have a tag called DailySweeps that has an integer values and is counting the number of sweeps a machine makes during the day. There is a 2nd tag called ResetSweeps that is currently False. When ResetSweeps goes True, the value of DailySweeps is reset to zero. This normally happens once a day, but could happen several times per day depending on production. I would like to capture the sweep count before it is reset. I am thinking that what I need is an action item that will write an alert to the alert table with the value of DailySweeps when ResetSweeps goes True.

Is this the correct way to go about this type of problem? If so, do I have to worry about DailySweeps be reset before I get the value? Also I have never written an action item before, so if you could give me a jumbo type coding hint, I would appreciate it.

I was originally going to do this in SQL, but I thought it may be much easier to handle in FSQL.

Well, let’s see… first off, are these actions being generated in the PLC, or (at least partially) in FactorySQL? I mean, setting the Reset bit to 1 and then setting the counter to 0. If you set the reset bit and then the PLC zeros the counter, you it would be unpredictable to try and get the count and the bit at the same time in a different group.

Second, is the counter value being stored regularly in the database? If so, your easiest route may be an Action Item in “query mode” (which means it’s just a sql query), who triggers on the reset bit, and just executes a query which inserts the last value into a history table. For example…

INSERT INTO counter_history (value, t_stamp) SELECT value, t_stamp FROM status_table WHERE id=0

Note, the syntax might be a tad off, but I think it’s right. Anyhow, that query will basically snapshot the current value of the status table into the history table.

Hope that helps,

The reset bit is actually something that gets set when the operator of the machine resets the counter. The latest value in the counter is being stored in the database every 30 seconds. I don’t see a way the operator could reset the counter before it has written the max value into the table. Sounds like the query mode will do just what I need.


You could also store the counter in another register before resetting it and have FSQL read from the register. That way you can be sure you have the correct amount of sweeps if one occurred in the 30 seconds before the reset.