# Tag counter

**URL:** <https://forum.inductiveautomation.com/t/tag-counter/4172>\
**Category:** Ignition\
**Created:** [March 19, 2012, 6:04pm UTC](https://forum.inductiveautomation.com/t/tag-counter/4172 "2012-03-19T18:04:20Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![yopy.yogie](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yopy.yogie](https://forum.inductiveautomation.com/u/yopy.yogie)\
**Post date:** [March 19, 2012, 6:04pm UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/1 "2012-03-19T18:04:20Z")

</div>

I have a digital opc tag which connect to a photo sensor, and have true value for several second and then go to false value. And I want to have a tag which count how many that digital tag has true value. What is the good solution to accomplish that?

Thank you, sorry for my bad English.

---

<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:** [March 19, 2012, 7:04pm UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/2 "2012-03-19T19:04:46Z")

</div>

The best way would be with a transaction group as an Event Counter.

If you don’t have access to transaction groups (i.e you have Panel Edition), it get’s a little more complicated.

I’ll post an example in a little bit…

---

<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:** [March 19, 2012, 7:21pm UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/3 "2012-03-19T19:21:05Z")

</div>

Actually, it’s going to be longer than a bit. Have a meeting to go to. 😆

---

<div class="post-metadata">

**Author:** ![yopy.yogie](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yopy.yogie](https://forum.inductiveautomation.com/u/yopy.yogie)\
**Post date:** [March 20, 2012, 1:52am UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/4 "2012-03-20T01:52:58Z")

</div>

Assumed, I have an access to Transaction group.  
Ok, I will wait for the sample.

Best Regards

---

<div class="post-metadata">

**Author:** ![yopy.yogie](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yopy.yogie](https://forum.inductiveautomation.com/u/yopy.yogie)\
**Post date:** [March 20, 2012, 4:27am UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/5 "2012-03-20T04:27:57Z")

</div>

Update …  
Actually i do not have a transaction group.  
But i am really appreciate if you provide a sample for both case studies.  
With and without transaction group.

Best Regards

---

<div class="post-metadata">

**Author:** ![yopy.yogie](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yopy.yogie](https://forum.inductiveautomation.com/u/yopy.yogie)\
**Post date:** [March 21, 2012, 1:52am UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/6 "2012-03-21T01:52:48Z")

</div>

Hello Mr. Jordan,  
I am waiting for your help, because I will perform demo to potential customer about Ignition.

Best Regards

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [March 21, 2012, 4:21am UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/7 "2012-03-21T04:21:27Z")

</div>

Lets assume your OPC tag is called “Magic Tag”

Create a DB Tag Called “Counter”

Create a Tag Change Gateway Event Script. You can call it whatever you want. In “Tag Paths”, enter the path to Magic Tag

For the script, enter the following

```auto
counter = system.tag.getTagValue("Counter") #Gets the current value of the counter
if newValue.value == 1: #If this event was caused because the magic tag was set true
	counter += 1 #Increment the counter value
system.tag.writeToTag("counter",counter) #write the new counter value to the "Counter" tag
```

Save the project and count away

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [March 21, 2012, 4:40am UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/8 "2012-03-21T04:40:20Z")

</div>

If you want a cumulative true time,. try the following in addition to the previous code.

Create 2 more tags, "LastChangeTimestamp (DateTime) and CumulativeTrueTimeInSeconds(Int8)

```auto
counter = system.tag.getTagValue("Counter")
if newValue.getValue() == 1:
	counter += 1
	system.tag.writeToTag("LastChangeTimestamp",newValue.getTimestamp())
else:
	time = system.tag.getTagValue("CumulativeTrueTimeInMilliseconds")
	lastFallingTransition = system.tag.getTagValue("LastChangeTimestamp")
	time += newValue.getTimestamp().getTime() - lastFallingTransition.getTime()
	system.tag.writeToTag("CumulativeTrueTimeInMilliseconds",time)
system.tag.writeToTag("counter",counter)
```

---

<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:** [March 21, 2012, 10:18am UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/9 "2012-03-21T10:18:21Z")

</div>

Sorry, Yopie, I’ve been away for a bit.

Kyle has is exactly right. This is the best way to go about it without a transaction group.

Using a transaction group, you can set any item in the group to be a counter by using the “Event meter option”

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/1X/933b3b60beb47632441003fee72152b6df3a6989.png)

---

<div class="post-metadata">

**Author:** ![yopy.yogie](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yopy.yogie](https://forum.inductiveautomation.com/u/yopy.yogie)\
**Post date:** [March 22, 2012, 12:40am UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/10 "2012-03-22T00:40:24Z")

</div>

Superbb, i am just searching for tag change event.  
I think that is the best way to perform counter calculation.

I’ve also have try using Transaction Group.

Thank you Kyle and Jordan.  
I am really appreciate it.

---

<div class="post-metadata">

**Author:** ![yopy.yogie](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yopy.yogie](https://forum.inductiveautomation.com/u/yopy.yogie)\
**Post date:** [March 28, 2012, 7:23am UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/11 "2012-03-28T07:23:45Z")

</div>

I have successfully create the counter, but  
i have trouble displaying LastChangeTimestamp on the label control.  
The label did not updated when “Magic Tag” changed.

[quote=“Kyle Chase”]If you want a cumulative true time,. try the following in addition to the previous code.

Create 2 more tags, "LastChangeTimestamp (DateTime) and CumulativeTrueTimeInSeconds(Int8)

`
counter = system.tag.getTagValue("Counter")
if newValue.getValue() == 1:
	counter += 1
	system.tag.writeToTag("LastChangeTimestamp",newValue.getTimestamp())
else:
	time = system.tag.getTagValue("CumulativeTrueTimeInMilliseconds")
	lastFallingTransition = system.tag.getTagValue("LastChangeTimestamp")
	time += newValue.getTimestamp().getTime() - lastFallingTransition.getTime()
	system.tag.writeToTag("CumulativeTrueTimeInMilliseconds",time)
system.tag.writeToTag("counter",counter)
`[/quote]

---

<div class="post-metadata">

**Author:** ![yopy.yogie](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yopy.yogie](https://forum.inductiveautomation.com/u/yopy.yogie)\
**Post date:** [April 3, 2012, 1:34am UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/12 "2012-04-03T01:34:05Z")

</div>

Dear Kyle,  
I have successfully create the counter, but  
i have trouble displaying LastChangeTimestamp on the label control.  
The label did not updated when “Magic Tag” changed.

Is there something I’ve missed?

Regards

---

<div class="post-metadata">

**Author:** ![yopy.yogie](https://avatars.discourse-cdn.com/v4/letter/y/4bbf92/32.png) [@yopy.yogie](https://forum.inductiveautomation.com/u/yopy.yogie)\
**Post date:** [April 3, 2012, 1:48am UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/13 "2012-04-03T01:48:27Z")

</div>

One more thing :

I’ve created two counter tag, InputCounter and OutputCounter.  
All the counter are updated through Gateway Script Tag Change Event.  
Here are the script :

InputCounter (updated when AInput tag changed)

[color=#0000FF]counter = system.tag.getTagValue(“InputCounter”)  
if newValue.value == 1:  
counter += 1

system.tag.writeToTag (“InputCounter”, counter) [/color]

OutputCounter (updated when NInput tag changed)  
[color=#0000FF]  
counter2 = system.tag.getTagValue(“OutputCounter”)  
if newValue.value == 1:  
counter2 += 1

system.tag.writeToTag (“OutputCounter”, counter2)[/color]

The problem is only InputCounter are updated, the OutputCounter always have zero value although the NInput tag value changed.

Also i try to get timestamp value when AInput value changed through the following script :

[color=#0000FF]  
system.tag.writeToTag (‘GlassDigitalStatus\tAInput’, newValue.getTimestamp())[/color]

Tag ‘GlassDigitalStatus\tAInput’ has TimeStamp data type.

But, the tAInput tag always got null value.  
My goal is getting time difference between NInput and AInput.

Is there any idea how to solve this?

Best Regards

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [April 3, 2012, 3:24pm UTC](https://forum.inductiveautomation.com/t/tag-counter/4172/14 "2012-04-03T15:24:03Z")

</div>

My guess is there is a problem around:`if newValue.value == 1:
    counter2 += 1`I would put print statements or add an else to see what is going on:`if newValue.value == 1:
    counter2 += 1
else:
    counter2 = 10`You have to make sure the script is firing and the value is what you expect. If you do print to the console you can check the print out in the wrapper.log file located in:

c:\program files\inductive automation\ignition\logs
