# system.tag.readAll COUNTIF

**URL:** <https://forum.inductiveautomation.com/t/system-tag-readall-countif/15214>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [August 31, 2017, 1:07am UTC](https://forum.inductiveautomation.com/t/system-tag-readall-countif/15214 "2017-08-31T01:07:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris\_d\_sanders](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chris_d_sanders/32/3359_2.png) [@chris\_d\_sanders](https://forum.inductiveautomation.com/u/chris_d_sanders)\
**Post date:** [August 31, 2017, 1:07am UTC](https://forum.inductiveautomation.com/t/system-tag-readall-countif/15214/1 "2017-08-31T01:07:21Z")

</div>

Ok, first things first, it's been awhile since I last posted, and it appears that my scripting skills have rusted over time.

I have 450 OPC Tags that are all integers. There are only 12 values that they can have. What I need is the equivalent of excel's **`COUNTIF`** function. I have been unsuccessful in using the **`system.tag.readAll`** scripting function.

Example of Tag Layout

There are 13 folders in the Curing folder:

> Tags-\> US9\_FIS-\> Curing-\>  
> 0000  
> 0100  
> ...  
> 1200

For 0000, there are 44 INT Tags. For 0100 - 1200 there are 34 INT Tags in each folder:

```auto
US9_FIS
      Curing
            0000
                0001_DTCode_Right
                0001_DTCode_Left
                0003_DTCode_Right
                0003_DTCode_Left
                .
                .
                .
                0041_DTCode_Right
                0041_DTCode_Left
            0100
                0101_DTCode_Right
                0101_DTCode_Left
                0103_DTCode_Right
                0103_DTCode_Left
                .
                .
                .
                0133_DTCode_Right
                0133_DTCode_Left
            .
            .
            .
            1200
                1201_DTCode_Right
                1201_DTCode_Left
                1203_DTCode_Right
                1203_DTCode_Left
                .
                .
                .
                1233_DTCode_Right
                1233_DTCode_Left

```

My idea is this, create 12 Memory tags to hold the count from each type of code that can exist. Then, loop through all the tags with a **`system.tag.readAll`** and count while looping through the tags. I also imagine that I would have to loop for each code, so 12 times? Any assistance would be greatly appreciated!!

Thank you all in advance.

---

<div class="post-metadata">

**Author:** ![brandon1](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@brandon1](https://forum.inductiveautomation.com/u/brandon1)\
**Post date:** [August 31, 2017, 1:51pm UTC](https://forum.inductiveautomation.com/t/system-tag-readall-countif/15214/2 "2017-08-31T13:51:13Z")

</div>

I believe you can do this in one loop by using if statements:

```auto
code1Count = 0
code2Count = 0
...
code12Count = 0
tags = ["Tags/US9_FIS/Curing/0000/0001_DTCode_Right","Tags/US9_FIS/Curing/0000/0001_DTCode_Right", etc....]
values = system.tag.readAll(tags)
for x in range(len(tags)):
   value = x.value
   if value==#(Put Code one Value here):
      code1Count += 1
  elif value==#(Put Code two Value here):
      code2Count += 1
...
  else code12Count += 1
         
#Update memory tags with count values
system.tag.write("Tags/code1Count",code1Count)
system.tag.write("Tags/code2Count",code2Count)
...
system.tag.write("Tags/code12Count",code12Count)

```

I hope this helps

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 31, 2017, 5:48pm UTC](https://forum.inductiveautomation.com/t/system-tag-readall-countif/15214/3 "2017-08-31T17:48:58Z")

</div>

Along the lines of Brandon’s suggestion, just avoiding the repetitive `if` statement:

```auto
codes = {}
tags = ["Tags/US9_FIS/Curing/0000/0001_DTCode_Right","Tags/US9_FIS/Curing/0000/0001_DTCode_Right", etc....]
values = system.tag.readAll(tags)

for i in range(len(tags)):
	if values[i].value:
		currentCode = tags[i].split('/')[3]
		if codes.get(currentCode) is not None:
			codes[currentCode] += 1
		else:
			codes[currentCode] = 1

```

Would output a dictionary like this:  
`{'0000': 1, '0100': 3, '0200': 0}` etc

---

<div class="post-metadata">

**Author:** ![chris\_d\_sanders](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chris_d_sanders/32/3359_2.png) [@chris\_d\_sanders](https://forum.inductiveautomation.com/u/chris_d_sanders)\
**Post date:** [August 31, 2017, 9:52pm UTC](https://forum.inductiveautomation.com/t/system-tag-readall-countif/15214/4 "2017-08-31T21:52:55Z")

</div>

Thank you for the responses. Is there a way build the tag list dynamically without having to list all 450 tags? That was also something that I was trying to avoid.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 31, 2017, 10:54pm UTC](https://forum.inductiveautomation.com/t/system-tag-readall-countif/15214/5 "2017-08-31T22:54:34Z")

</div>

```auto
root = "US9_FIS/Curing"
folders = ["%02d00" % i for i in range(13)]
suffix = "_DTCode_"

tags = []

def getpath(folder, i, suffix):
	return '/'.join([root] + [folder] + ["%s%02d%s%s" % (folder[:2], i, suffix, "Left" if i % 2 else "Right")])

for index, folder in enumerate(folders):
	if not int(folder):
		for i in range(44):
			tags.append(getpath(folder, i, suffix))
	else:
		for i in range(34):
			tags.append(getpath(folder, i, suffix))

for tag in tags:
	print tag

```

---

<div class="post-metadata">

**Author:** ![chris\_d\_sanders](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chris_d_sanders/32/3359_2.png) [@chris\_d\_sanders](https://forum.inductiveautomation.com/u/chris_d_sanders)\
**Post date:** [September 1, 2017, 3:35pm UTC](https://forum.inductiveautomation.com/t/system-tag-readall-countif/15214/6 "2017-09-01T15:35:17Z")

</div>

Paul,

Thank you for the great code. I need to modify it slightly.

Instead of returning, for example, the following:

```auto
US9_FIS/Curing/0000/0000_DTCode_Right
US9_FIS/Curing/0000/0001_DTCode_Left
US9_FIS/Curing/0000/0002_DTCode_Right
US9_FIS/Curing/0000/0003_DTCode_Left

```

I want it to return this:

```auto
US9_FIS/Curing/0000/0001_DTCode_Right
US9_FIS/Curing/0000/0001_DTCode_Left
US9_FIS/Curing/0000/0003_DTCode_Right
US9_FIS/Curing/0000/0003_DTCode_Left

```

I am digging into it myself right now and may come up with a solution on my own (which I would prefer), but any help is appreciated.

I hate feeling like a mooch and a HACK at this, but I only had a loose understanding of Python and Javascript when I first did this 9 years ago. I’ve been out of the game too long and no formal education on this stuff! LOL

Thanks again.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 1, 2017, 4:58pm UTC](https://forum.inductiveautomation.com/t/system-tag-readall-countif/15214/7 "2017-09-01T16:58:53Z")

</div>

To at least point you in the right direction, since my code’s pretty opaque:  
The piece you’ll need to modify is the second argument in the string replacement  
`(folder[:2], i, suffix, "Left" if i % 2 else "Right")])`  
So, in this case, `i`, which is just the loop index, needs to be adjusted so that it increases in steps of 2.  
I’d recommend looking at the modulo operator that I did to switch between Left and Right; in this case you can use modulo to add/subtract an offset to the loop index and get the return value you want.
