How fast are your boxes coming in?
I do sort of the same thing with contamination count buttons, but instead of trying to catch every button press I just have an internal data register in the PLC increment everytime one of the buttons is pressed. Then I made a block transaction group that looks to see when the data register value changes and I stuff that value into the database, then I aggregate later on with a SQL query. It would work good with multiple lines or products because you can call that out in your block group. This is how I have mine set up:
Then I just aggregate over the day with this query:
SELECT Bad_Reason as "Reason", count(Bad_Reason) as "Count"
where RP1_Running = 1
and t_stamp >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
and t_stamp < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1)
group by Bad_Reason
You could probably do something similar but you have to watch how fast your boxes come in and how quickly your OPC tag is updating and how quickly your transaction group is running. If you get two boxes per OPC read or transaction group cycle then you’ll miss boxes. Otherwise you could still use the incrementing data register and figure out with a bit of scripting how many boxes went past since the last read.
Hopefully that points you in the right direction.