# Creating a counter via Gateway Tag Change Event Script

**URL:** <https://forum.inductiveautomation.com/t/creating-a-counter-via-gateway-tag-change-event-script/37193>\
**Category:** Ignition\
**Created:** [July 29, 2020, 3:57pm UTC](https://forum.inductiveautomation.com/t/creating-a-counter-via-gateway-tag-change-event-script/37193 "2020-07-29T15:57:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dustin.corban](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@dustin.corban](https://forum.inductiveautomation.com/u/dustin.corban)\
**Post date:** [July 29, 2020, 3:57pm UTC](https://forum.inductiveautomation.com/t/creating-a-counter-via-gateway-tag-change-event-script/37193/1 "2020-07-29T15:57:13Z")

</div>

So i am trying to create a counter that increments by one each time a boolean OPC tag equals true. I found several examples, but for some reason cannot get mine to work. Here is what i have…

OPC Tag name: Timer\_1\_on  
Memory Tag name: Counter 1 (set to integer) and value = 0

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/b/abbeff06f2895bea0133c2627a466ba5dd264b10.png)

Gateway Change script:  
if newValue.getValue() == 1:  
counter = system.tag.readBlocking(["[default]Lab Testers/Counter 1"])[0].value  
counter += 1

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 29, 2020, 4:05pm UTC](https://forum.inductiveautomation.com/t/creating-a-counter-via-gateway-tag-change-event-script/37193/2 "2020-07-29T16:05:38Z")

</div>

Your incremented value is in a python variable at the end of your script, which is then thrown away. You’ll have to write it back to the tag.

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [July 29, 2020, 4:07pm UTC](https://forum.inductiveautomation.com/t/creating-a-counter-via-gateway-tag-change-event-script/37193/3 "2020-07-29T16:07:13Z")

</div>

Personally, if you want to set up a counter like this, I could do it in a tag event value change script. But one thing you don’t show is where you write the tag back into your counter. I would also do a check to make sure it isn’t an initial change. I would assume it should look like:

```auto
if not initialChange:
	if newValue.getValue():
		counter = system.tag.readBlocking(["[default]Lab Testers/Counter 1"])[0].value
		counter += 1
		system.tag.writeBlocking(["[default]Lab Testers/Counter 1"],[counter])

```

@pturmel beat me to the reply with essentially the same thing.

---

<div class="post-metadata">

**Author:** ![dustin.corban](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@dustin.corban](https://forum.inductiveautomation.com/u/dustin.corban)\
**Post date:** [July 29, 2020, 4:12pm UTC](https://forum.inductiveautomation.com/t/creating-a-counter-via-gateway-tag-change-event-script/37193/4 "2020-07-29T16:12:31Z")

</div>

Thank you both for this. I can’t believe i did not add the write back to the tag. Frustrating but simple. Thank you again.
