# I'm attempting to count the number of tags that have a certain value

**URL:** <https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123>\
**Category:** General Discussion\
**Tags:** perspective\
**Created:** [August 24, 2024, 9:48pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123 "2024-08-24T21:48:54Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![BDe\_Loof](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@BDe\_Loof](https://forum.inductiveautomation.com/u/BDe_Loof)\
**Post date:** [August 24, 2024, 9:48pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/1 "2024-08-24T21:48:54Z")

</div>

I'm using the follow 'onclick' script of a button But keep getting an error tagpath not defined

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/e/1e4a8853ff96fa39d6f5e6bd1e4a8a00723c06f7.png)

```python
def runAction(self, event):
	
	# Specify the root tag path
    tagPath = "[default]NewFolder"

try:

    # Create a logger to log messages
    logger = system.util.getLogger("TagValueCounter")

    # Browse the tag folder
    browseResults = system.tag.browse(tagPath)

    # Initialize counter for tags with a defined value
    count = 0

    # Iterate over each tag and read its value
    for resultTag in browseResults.getResults():
        if resultTag['hasChildren']: 
            # Skip if it's a folder
            continue
        
        # Read the value of the tag
        tagValue = system.tag.readBlocking([str(resultTag['fullPath'])])[0].value
        
        # Check if the tag has a defined (non-null) value
        if tagValue is not None:
            count += 1

    # Print the count of tags with a defined value
    system.perspective.print("Tags with a defined value: " + str(count))
    # self.custom.count("Tags with a defined value: " + str(count))
    
except Exception as e:
    # Log any errors that occur
    logger.error("Error: " + str(e))

```

---

<div class="post-metadata">

**Author:** ![BDe\_Loof](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@BDe\_Loof](https://forum.inductiveautomation.com/u/BDe_Loof)\
**Post date:** [August 24, 2024, 9:58pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/2 "2024-08-24T21:58:04Z")

</div>

the tag path  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/c/ccd683a2f628de94c2a504a245c9269df2facffb.png)

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [August 24, 2024, 10:03pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/3 "2024-08-24T22:03:23Z")

</div>

Bits of your code aren't formatted as code so we're missing indentation, etc. See [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831) and then you can edit your post to fix it.

---

<div class="post-metadata">

**Author:** ![BDe\_Loof](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@BDe\_Loof](https://forum.inductiveautomation.com/u/BDe_Loof)\
**Post date:** [August 24, 2024, 10:20pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/4 "2024-08-24T22:20:51Z")

</div>

thank you I will do that

---

<div class="post-metadata">

**Author:** ![BDe\_Loof](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@BDe\_Loof](https://forum.inductiveautomation.com/u/BDe_Loof)\
**Post date:** [August 24, 2024, 10:24pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/5 "2024-08-24T22:24:12Z")

</div>

```python
def runAction(self, event):
	
	# Specify the root tag path
    tagPath = "[default]NewFolder"

try:

    # Create a logger to log messages
    logger = system.util.getLogger("TagValueCounter")

    # Browse the tag folder
    browseResults = system.tag.browse(tagPath)

    # Initialize counter for tags with a defined value
    count = 0

    # Iterate over each tag and read its value
    for resultTag in browseResults.getResults():
        
        if resultTag['hasChildren']: 
            # Skip if it's a folder
            continue
        
        # Read the value of the tag
        tagValue = system.tag.readBlocking([str(resultTag['fullPath'])])[0].value
        
        # Check if the tag has a defined (non-null) value
        if tagValue is not None:
            count += 1

    # Print the count of tags with a defined value
    system.perspective.print("Tags with a defined value: " + str(count))
    # self.custom.count("Tags with a defined value: " + str(count))
    
except Exception as e:
    # Log any errors that occur
    logger.error("Error: " + str(e))

```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 24, 2024, 10:37pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/6 "2024-08-24T22:37:14Z")

</div>

Your entire `try`/`except` block is on the wrong indentation level and not part of the `runAction` function.

---

<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 25, 2024, 12:15am UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/7 "2024-08-25T00:15:25Z")

</div>

Taking a step back from the problems with your code - what are you actually attempting to accomplish with this script? Running recursive browsing functions in a Perspective button click is going to potentially put a lot of load on your gateway server for questionable benefit. What's the thing you're hoping to derive with this count of non-null values?

Also, just in case... Be very careful (or, more succinctly, don't use) LLMs like ChatGPT/etc to help you write Ignition specific scripts. They'll happily, confidently, lie to you about what system functions are available, what arguments they may take, what scope they're in, etc. LLMs can be useful for general algorithms and getting language syntax correct, but cannot be trusted with extremely specific domains like Ignition specific scripting functions or expressions.

---

<div class="post-metadata">

**Author:** ![BDe\_Loof](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@BDe\_Loof](https://forum.inductiveautomation.com/u/BDe_Loof)\
**Post date:** [August 25, 2024, 1:09am UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/8 "2024-08-25T01:09:01Z")

</div>

Thank you for your comments. My Goal is I have set of tags that have a recipe value in them. I wanted to create a button that would count the tags that had a certain value. so the "operator" didn't have to count them up. The script would only run when they click on the button. Yes I did use CHATgpt to start the process. I'm very "GREEN" on coding with scripts. I thought it would be easier to do in ignition than writing in the plc.Thank you again for your comments and concerns.

---

<div class="post-metadata">

**Author:** ![BDe\_Loof](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@BDe\_Loof](https://forum.inductiveautomation.com/u/BDe_Loof)\
**Post date:** [August 25, 2024, 1:31am UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/9 "2024-08-25T01:31:09Z")

</div>

Thank you Kevin.. I will look into that i'm very new to scripting.

---

<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:** [August 25, 2024, 3:24pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/10 "2024-08-25T15:24:39Z")

</div>

Are you already displaying a list of those recipes ?  
If you didn't implement that functionality, how would the operator count them ?

---

<div class="post-metadata">

**Author:** ![BDe\_Loof](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@BDe\_Loof](https://forum.inductiveautomation.com/u/BDe_Loof)\
**Post date:** [August 25, 2024, 4:06pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/11 "2024-08-25T16:06:36Z")

</div>

The term recipe is a bit loose basically I have about 120 tags that have 1,2,3 or 4 as a value. I just want to count all 1's ect and display that value. Currently the objects turn different colors on the screen. thats how they can tell how many of the "1" product and so on.

---

<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:** [August 25, 2024, 4:26pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/12 "2024-08-25T16:26:10Z")

</div>

Well if they're already on the screen, that means you're already pulling the value. So you don't need to go through the tags themselves.

Are you displaying them in a table ?

---

<div class="post-metadata">

**Author:** ![BDe\_Loof](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@BDe\_Loof](https://forum.inductiveautomation.com/u/BDe_Loof)\
**Post date:** [August 25, 2024, 5:00pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/13 "2024-08-25T17:00:52Z")

</div>

No I am not currently displaying them in a table. They are Label Components with dynamic links attached to color and text. I just want to count the tags using what there dynamic color value is. in the picture green is one product cyan is another

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/6/162854f294a133374a4542374911d30d30d3edbe.jpeg)

---

<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:** [August 25, 2024, 6:32pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/14 "2024-08-25T18:32:37Z")

</div>

What's behind those labels ?  
Did you hardcode individual tag bindings on each of them ?

---

<div class="post-metadata">

**Author:** ![BDe\_Loof](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@BDe\_Loof](https://forum.inductiveautomation.com/u/BDe_Loof)\
**Post date:** [August 25, 2024, 6:40pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/15 "2024-08-25T18:40:33Z")

</div>

yes I did hard code each one... it was my first week working with ignition  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/2/d2d8f0930c756dcde27cedfc05f0e1e7758932e3.jpeg)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/c/6c94540565c7364ddc0652feeb7a219ca2d44d7d.jpeg)

I plan on redoing it later maybe a table

---

<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:** [August 25, 2024, 7:11pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/16 "2024-08-25T19:11:00Z")

</div>

Yes I suggest you use a table. You'll have to pull all the data at once, which will allow you to easily count how many of each values are in your data structure.

---

<div class="post-metadata">

**Author:** ![BDe\_Loof](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@BDe\_Loof](https://forum.inductiveautomation.com/u/BDe_Loof)\
**Post date:** [August 25, 2024, 7:15pm UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/17 "2024-08-25T19:15:10Z")

</div>

Thank you I will start working on that ... Thank you again for your time and comments

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 26, 2024, 1:08am UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/18 "2024-08-26T01:08:30Z")

</div>

It looks like this is all done in a coordinate View as well, when it's more suited to a flex View. You will have far better control and simpler setup with a flex.

If you're unfamiliar with flex containers, you would need to add flex containers within flex containers. e.g.

Where:

- red = the root flex container itself
- blues = 2 flex containers added into the root. Top = column, Bottom = column
- magentas = 2 flex containers added into the bottom blue. Left = row, Right = row

I would be putting the legend colours on the single row.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/d/fda2f451da97cd4e22e263499f8471661f3fa7ed.jpeg)

---

<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:** [August 26, 2024, 6:30am UTC](https://forum.inductiveautomation.com/t/im-attempting-to-count-the-number-of-tags-that-have-a-certain-value/95123/19 "2024-08-26T06:30:08Z")

</div>

Wait a bit before diving in.

There are differences between labels and tables. The main one is that to populate the table, you'll likely use a script and `system.tag.readBlocking` to read your tags in bulk. This is VERY different from the bindings you use there: the bindings update automatically, but a script won't.  
There are ways to handle this.

You could make it refresh every x seconds for example, using `now(x*1000)` in an expression binding  
Or you could use a template view for each tag. Make it take a tag path as parameter, and use it in an indirect binding, which will update automatically. Then you can use that template view with a table or a flex repeater, using a pre-generated list of tags to populate the table's row or the repeater's instances.

This kind of thing doesn't come naturally and it takes some time to see "_the way_", but we can help you kickstart your ignition experience.
