# Counting tag

**URL:** <https://forum.inductiveautomation.com/t/counting-tag/6190>\
**Category:** Ignition\
**Created:** [April 9, 2013, 9:28pm UTC](https://forum.inductiveautomation.com/t/counting-tag/6190 "2013-04-09T21:28:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [April 9, 2013, 9:28pm UTC](https://forum.inductiveautomation.com/t/counting-tag/6190/1 "2013-04-09T21:28:30Z")

</div>

I have 2 tags.  
1 is a bool and one is an expression.  
I want the expression tag to keep track how many times the bool “becomes true”

Counter is something like:

```auto
if(hasChanged({[.]bool}) && {[.]bool} = 1, {[.]Counter} + 1, {[.]Counter} + 0)
```

This does not work and I really want to be able to track this over a thousand tags and don’t see the point of using a gateway script for this.  
Any suggestions?

---

<div class="post-metadata">

**Author:** ![dave.fogle](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@dave.fogle](https://forum.inductiveautomation.com/u/dave.fogle)\
**Post date:** [April 9, 2013, 10:38pm UTC](https://forum.inductiveautomation.com/t/counting-tag/6190/2 "2013-04-09T22:38:17Z")

</div>

Try modifying your expression to look like the following. This is assuming your expression tag is called Counter:

```python
if(hasChanged({[.]bool}) && {[.]bool} = 1,coalesce({[.]Counter}+1,0),coalesce({[.]Counter},0))

```

---

<div class="post-metadata">

**Author:** ![CPowell](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@CPowell](https://forum.inductiveautomation.com/u/CPowell)\
**Post date:** [April 10, 2013, 4:29pm UTC](https://forum.inductiveautomation.com/t/counting-tag/6190/3 "2013-04-10T16:29:58Z")

</div>

The only issue I see with that solution is the tag “Counter” value does not change to zero when reset.  
I have tried 2 ways:

1. Trying to write the value to zero from the designer.  
2)system.tag.writeToTag(“Counter”,0)

It does reset thou. You can refresh the tag database to show 0 or wait for the conditions to be true and it starts at 1 again.

If that is not an issue, then take solution should work. Otherwise, the Gateway Tag Change Script will work also.

Something like this:

```auto
def Count(b,c):
	import system
	bool = system.tag.getTagValue(b)
	Count = system.tag.getTagValue(c)
	if bool == 1:
		Count += 1
	else:
		pass
	system.tag.writeToTag(c,Count)

def ResetCount(c):
	import system
	system.tag.writeToTag(c,0)
```

Then just add the bool tag to the Gateway Tag Change Scripts to call the script.  
app.Test.Count(“bool”,“Counter”)

Hope that helps.  
Chris

---

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [April 20, 2013, 7:17pm UTC](https://forum.inductiveautomation.com/t/counting-tag/6190/4 "2013-04-20T19:17:25Z")

</div>

[quote=“dave.fogle”]Try modifying your expression to look like the following. This is assuming your expression tag is called Counter:

[code]  
if(hasChanged({[.]bool}) && {[.]bool} = 1,coalesce({[.]Counter}+1,0),coalesce({[.]Counter},0))

[/code][/quote]

I fixed this in the UDT but it looks like a question marked tag in each instance, I can write to it sometimes… it seems like it doesn’t like to equal itself in the else segment…

---

<div class="post-metadata">

**Author:** ![dave.fogle](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@dave.fogle](https://forum.inductiveautomation.com/u/dave.fogle)\
**Post date:** [April 22, 2013, 9:56pm UTC](https://forum.inductiveautomation.com/t/counting-tag/6190/5 "2013-04-22T21:56:22Z")

</div>

What version are you running because I'm not having a problem doing this in a UDT?

---

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [April 23, 2013, 2:35pm UTC](https://forum.inductiveautomation.com/t/counting-tag/6190/6 "2013-04-23T14:35:33Z")

</div>

7.5.5  
It is still giving me issues.  
The tag has a question mark by it.

---

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [April 24, 2013, 6:41pm UTC](https://forum.inductiveautomation.com/t/counting-tag/6190/7 "2013-04-24T18:41:47Z")

</div>

Thoughout my tag instances I have some Counters work. some have bad quality, some unknown quality.

---

<div class="post-metadata">

**Author:** ![Colby.Clegg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/colby.clegg/32/3282_2.png) [@Colby.Clegg](https://forum.inductiveautomation.com/u/Colby.Clegg)\
**Post date:** [April 24, 2013, 8:17pm UTC](https://forum.inductiveautomation.com/t/counting-tag/6190/8 "2013-04-24T20:17:00Z")

</div>

One thing is that expressions propagate up the worst of the qualities contained in them. So it sounds like the counter is passing along its initial unknown quality. Try this:

```auto
forceQuality(if(hasChanged({[.]bool}) && {[.]bool} = 1,coalesce({[.]Counter}+1,0),coalesce({[.]Counter},0)),192)
```

You could do it around each reference to Counter, but this should be sufficient.

Regards,

---

<div class="post-metadata">

**Author:** ![ignitionSL](https://avatars.discourse-cdn.com/v4/letter/i/9e8a1a/32.png) [@ignitionSL](https://forum.inductiveautomation.com/u/ignitionSL)\
**Post date:** [May 3, 2024, 5:47am UTC](https://forum.inductiveautomation.com/t/counting-tag/6190/9 "2024-05-03T05:47:23Z")

</div>

Hello everyone,  
How to count numbers of TRUE from 100 OPC Tags in Boolean data type.?

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 3, 2024, 7:47am UTC](https://forum.inductiveautomation.com/t/counting-tag/6190/10 "2024-05-03T07:47:19Z")

</div>

```python
tags = [qv.value for qv in system.tag.readBlocking(paths)]
count = sum(tags)

```

edit: note that if all you care about is the count, you can (and probably should) use a generator expression instead of creating the list:

```python
count = sum(qv.value for qv in system.tag.readBlocking(paths) if qv.quality.good)

```

Let's throw in a quality check, as nulls make `sum` go boink
