Capture Counter Tag Last Value Before Reset

Hi,

Found few topics here regarding production output counters, but logically, every case is specific and there is no universal approach.
In my case, I need to log end of batch counter value, then reset of that counter, since new batch will be started.
It can be done easily by adding 2 HMI buttons (and the respecive PLC program updates) - one for end of batch and one for reset. However, the machines are many, running 24/7, and a scheduled PLC&HMI program change will take a lot of time.
Is it possible to capture the previous (last) value of the counter before reset, and how the script will look like? Any help appreciated,

BR

I would set up an additional tag in the same folder as the batch counter.
2020-04-20_9-55-40
Then use a gateway tag change script.

if not initialChange: 
	currentValue = event.getCurrentValue().getValue()
	previousValue = event.getPreviousValue().getValue()

	if currentValue == 0 and previousValue != 0:
		logger = system.util.getLogger('batchCountResetMonitor')
		path = str(event.getTagPath().getParentPath()) + '/Previous Batch Count' 
		system.tag.writeBlocking([path],[previousValue])
		info = 'Batch Count Reset. Value of ' + str(previousValue) + ' written to ' + path
		logger.info(info)

Changes in current batch values:
2020-04-20_10-45-21 2020-04-20_10-45-38

Log Entry:

For every other batch counter, just add it to the Tag Path(s) list in the script configuration. Adding a logger entry gives you a visual that the script is working correctly.

Many thanks, JordanCClark, I will try this approach and revert asap.

So, the script is running as desired, no issues. In order to complete this task, I have created query tag to pick up the start count value for the day, and then, I would need a tag-flag (called, for example, BatchResetBit) , showing that batch has been reset (since if the batch has been reset, the formula for the daily output will change). Is it possible to write value 1 to that tag in the same script? How it will look like?

Just add it to the lists used in writeBlocking.

if not initialChange: 
	# Get current and previous values
	currentValue = event.getCurrentValue().getValue()
	previousValue = event.getPreviousValue().getValue()

	if currentValue == 0 and previousValue != 0:
		# Create logger
		logger = system.util.getLogger('batchCountResetMonitor')
		# Get the parent path
		basePath = str(event.getTagPath().getParentPath())
		# Create list of tags
		tagPaths = [basePath + '/Previous Batch Count', basePath + '/BatchResetBit']
		# Create list of values
		values = [previousValue, 1]
		# Write values to tags
		system.tag.writeBlocking(tagPaths,values)
		# Send information to logger
		info = 'Batch Count Reset. Value of ' + str(previousValue) + ' written to ' + tagPaths[0]
		logger.info(info)

Fantastic, JordanCClark, thank you very much indeed! The scripts are very powerful tool, but they need practice. In your opinion, for PLC&HMI guy like me, what is a good point to start with? Induction University only obviously is not enough.

I recommend Python. Ignition scripting uses Jython, which is a Java implementation of Python. This means that you have access to the Java libraries on top of the 'standard Python' ones.

Python is one of the easiest programming languages to learn. Tutorials abound for python in the interwebs, so there is a lot of information ripe for plucking. :slight_smile:

Udemy has some nice online courses available. Be aware, most are Python 3, while Jython is at 2.7, and there are a few minor differences.

We also have this thread of Favorite Resources: