Creating a counter via Gateway Tag Change Event Script

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

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.

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.

2 Likes

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:

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.

1 Like

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.