Browse tags printing folder

Hello, I am trying to setup a client project script (let me know if gateway would be better). To reset nightly tags. It’s a decent amount of tags so I’d like to loop it.

Ideally, I loop all the the tags under a folder, and write 0 to all of them at midnight using some sort of if time = midnight checked hourly.

I can loop using below code:

tags = system.tag.browseTags(parentPath="System1/Efficiency", recursive=True)
for tag in tags:
    print tag.path

But my issue is it prints the folder names see below: Should I exclude them/how can I?

folderresults
folderstruct

Use .isFolder() to filter out folders, and also turn off recursion if you don’t want tags that are included in the folders.

tags = system.tag.browseTags(parentPath="System1/Efficiency", recursive = False)
for tag in tags:
    if not tag.isFolder():
       print tag.path

@lrose thank you! I want tag path because i want to reset them all all at midnight with something like the full code below:

# Grabs current Date and Time
getDateTime = system.date.now()
# Returns current hour in a 0-23 format
getHour = system.date.format(today, "H")
# Creat array to store tag paths
resetPaths = []
# Checks if time is Midnight
if getHour == 0:
# Gets all tags in folder sub structure and writes to an array
	tags = system.tag.browseTags(parentPath="System1/Efficiency", recursive = True)
	for tag in tags:
    if not tag.isFolder():
    	# Wrties tag path to array
       	resetPaths.append (tag.path)
       	# Writes a 0 to clear all tags in array
		system.tag.writeAll(resetPaths,0)

And I’ll run it in a timer script to check once an hour. Just not sure if client or gateway timer script would be better?

Definitely gateway timer script. A client timer script will 1. only work if there’s at least one client running, and 2. be run by each client you have open.

1 Like

Quick back track @PGriffith and @lrose I’m getting the below error with the above code:
error

All of the indentation must match. Meaning you can’t mix and match “spaces” and “tabs”.

When I typed in the script I used spaces, so you probably just need to fix that.

1 Like

In your script the system.tag.writeAll() should be outside of the for loop. You only want to call it once. You will also need to provide it with a list of values, that matches list of paths.

# Grabs current Date and Time
getDateTime = system.date.now()
# Returns current hour in a 0-23 format
getHour = system.date.format(today, "H")
# Creat array to store tag paths
resetPaths = []
# Checks if time is Midnight
if getHour == 0:
# Gets all tags in folder sub structure and writes to an array
	tags = system.tag.browseTags(parentPath="System1/Efficiency", recursive = True)
	for tag in tags:
        if not tag.isFolder():
    	    # Wrties tag path to array
       	    resetPaths.append (tag.path)
    # Writes a 0 to clear all tags in array
    system.tag.writeAll(resetPaths,[0] * len(resetPaths))

Thank you, I went line by line and found it.

@lrose as I ponder this could I remove the 0 from the array to set them ‘blank’ I just used 0 because I assumed a value had to be written.

You could potentially use None if that is what you mean. I suppose it just depends on the types of the tags you are writing. For integers None is essentially 0.

fair enough, i guess a 0 works more universally per say.

@lrose Hey so I cam back today to no tags reset. It’s in a gateway event timer script set to 3,600,000 fixed rate.

I tried manually running script in script console replacing 0 with current hour, but no tags got cleared.

I tried moving the reset array to a var. but still no luck. Not sure why it’s not resetting anything.

Also there are only string and integer type tags. I’d assume the writing a 0 would work. I don’t get any errors.

Do you have any logging in the script? Do you see any errors in the Wrapper Logs? What are the types of the tags you are resetting and are they all the same type?

@lrose No logging in the script. Nothing in the console logs which i believe is the same as the wrapper log.

some are string and integers
some are opc, memory and expression

I did try adding some logging at the bottom. Not seeing anything. Maybe I’m doing it wrong lol idk.

I appreciate the help.

0 is not the same as "" is not the same as null/None - you probably have to write the correct initial value type for the tag’s type. You could read the data type and have a simple dictionary of tag types : initial tag values.

@PGriffith I tired this code below with no avail:

# Grabs current Date and Time
getDateTime = system.date.now()
# Returns current hour in a 0-23 format
getHour = system.date.format(getDateTime, "H")
# Creates array to store tag paths
intPaths = []
stringPaths = []
# Checks if time is Midnight
if getHour == 7:
# Gets all tags in folder sub structure and writes to an array
    tags = system.tag.browseTags(parentPath="System1/Efficiency", recursive = True)
    for tag in tags:
        if not tag.isFolder():
            if tag.dataType == Int4:
    	    # Wrties tag path to array
       	        intPaths.append (tag.path)
       	    elif tag.dataType == String:
       	        stringPaths.append (tag.path)
    # Creates array for reset values
    resetInt = [None] * len(intPaths)
    resetStr = [""] * len(stringPaths)
    # Writes a 0 to clear all tags in array
    system.tag.writeAll(intPaths,resetInt)
    system.tag.writeAll(stringPaths,resetStr)

I think it has something to do with the for loop. because right now I can not print tag paths right under for tag in tags.

When I delete the code above the for loop and remove indents i can print tag.path to the console. but not with the above code.

The console log is not the same as the wrapper log.

Here is a small example of using a logger.

This:

if tag.dataType == Int4:

Doesn’t do what you think it does. Int4 is a type, tag.dataType returns a string value returns a com.inductiveautomation.ignition.common.sqltags.model.types.DataType. The two can never be equivalent.

if tag.dataType == 'Int4':
    #do something
elif tag.dataType == 'String':
   #do something else
I would also use 0 for the default value for Integers. Also, instead of calling writeAll twice, combine the lists first and then call writeAll ``` paths = intPaths + stringPaths values = resetInt + resetStr system.tag.writeAll(paths,values) ```

Edit: See @PGriffith’s code below

from com.inductiveautomation.ignition.common import TypeUtilities

# Grabs current Date and Time
getDateTime = system.date.now()
# Returns current hour in a 0-23 format
getHour = system.date.getHour24(getDateTime)
# Creates list to store tag paths
paths = []

# Checks if time is Midnight
if getHour == 7:
	# Gets all tags in folder sub structure and writes to an array
    tags = system.tag.browseTags(parentPath="folder", recursive = True)
    for tag in tags:
        if not tag.isFolder():
        	paths.append((tag.path, TypeUtilities.getInitValueForClass(tag.dataType.javaType)))
        	
    paths, values = list(zip(*paths))
    system.tag.writeAll(paths, values)
2 Likes

@PGriffith that worked like a charm! Now if I can just learn from your code and understand what you did. lol. If you could explain a little more in depth how your code works I’d be very appreciative.

There are just a few tags I’ll need to work on including that im sure are somewhere else. maybe write that tag to another one in a subfolder so this works 100% for me. But that should be easy.

I’d just really like to do some googling and understand it so i’m capable of doing it in the future.

1 Like