# Capture Counter Tag Last Value Before Reset

**URL:** <https://forum.inductiveautomation.com/t/capture-counter-tag-last-value-before-reset/34048>\
**Category:** General Discussion\
**Created:** [April 20, 2020, 1:45pm UTC](https://forum.inductiveautomation.com/t/capture-counter-tag-last-value-before-reset/34048 "2020-04-20T13:45:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [April 20, 2020, 1:45pm UTC](https://forum.inductiveautomation.com/t/capture-counter-tag-last-value-before-reset/34048/1 "2020-04-20T13:45:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 20, 2020, 2:54pm UTC](https://forum.inductiveautomation.com/t/capture-counter-tag-last-value-before-reset/34048/2 "2020-04-20T14:54:14Z")

</div>

I would set up an additional tag in the same folder as the batch counter.  
 ![2020-04-20_9-55-40](https://us1.discourse-cdn.com/inductiveautomation/original/2X/b/b05024a6bc7dee3284aa7f6ab39aff9c960461b0.png)  
Then use a gateway tag change script.

```python
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)

```

 ![2020-04-20_10-43-41](https://us1.discourse-cdn.com/inductiveautomation/original/2X/2/2418e9adefdad39e35b6e5e67f0ef219817ae190.png) ![2020-04-20_10-43-29](https://us1.discourse-cdn.com/inductiveautomation/original/2X/0/0925cead9a82126391f566fe5316908470be2235.png)

Changes in current batch values:  
 ![2020-04-20_10-45-21](https://us1.discourse-cdn.com/inductiveautomation/original/2X/a/ae835cd8f3657c0a7144d882ac4c93d707196ae7.png) ![2020-04-20_10-45-38](https://us1.discourse-cdn.com/inductiveautomation/original/2X/6/6489fd9afeccaeea8170bd100e615b3e02b0b1b1.png)

Log Entry:

 ![2020-04-20_10-48-34](https://us1.discourse-cdn.com/inductiveautomation/original/2X/5/561d2a2192c5591f126e2739147c1dfdefdc3496.png)

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.

---

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [April 20, 2020, 3:19pm UTC](https://forum.inductiveautomation.com/t/capture-counter-tag-last-value-before-reset/34048/3 "2020-04-20T15:19:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![d.vasilev](https://avatars.discourse-cdn.com/v4/letter/d/df705f/32.png) [@d.vasilev](https://forum.inductiveautomation.com/u/d.vasilev)\
**Post date:** [April 22, 2020, 6:19am UTC](https://forum.inductiveautomation.com/t/capture-counter-tag-last-value-before-reset/34048/4 "2020-04-22T06:19:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 22, 2020, 11:19am UTC](https://forum.inductiveautomation.com/t/capture-counter-tag-last-value-before-reset/34048/5 "2020-04-22T11:19:59Z")

</div>

Just add it to the lists used in writeBlocking.

```python
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)
```

---

<div class="post-metadata">

**Author:** ![d.vasilev](https://avatars.discourse-cdn.com/v4/letter/d/df705f/32.png) [@d.vasilev](https://forum.inductiveautomation.com/u/d.vasilev)\
**Post date:** [April 22, 2020, 12:12pm UTC](https://forum.inductiveautomation.com/t/capture-counter-tag-last-value-before-reset/34048/6 "2020-04-22T12:12:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 22, 2020, 12:38pm UTC](https://forum.inductiveautomation.com/t/capture-counter-tag-last-value-before-reset/34048/7 "2020-04-22T12:38:42Z")

</div>

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. 🙂

[Udemy](https://www.udemy.com/topic/python/) 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:

> [@Favorite Resources](http://forum.inductiveautomation.com/t/favorite-resources/1108):
>
> Inductive Automation prides itself on building software centered around open and accessible technologies. Of course, this means that in order to be as effective as possible it is necessary to have a good understanding of these technologies. Outside resources such as books and websites can be invaluable. This thread is dedicated to sharing your favorite and most useful resource. To get things started, here are some books that are permanent fixtures on the desks here at Inductive Automation: Get…
