Dynamically read tags inside of a UDT from a script

Hi,

I want to read tags in a UDT dynamically from a script.

For example, if there are 4 tags in a UDT, I want to be able to put those in a list with the same code if there were 3.

Thanks.

Hi wfallon,

Not quite sure what you are trying to do, but maybe this will help.
It reads all tags in a folder or UDT and saves them to a dictionary.
Just modify it to suit your needs.
You just need to specify the folder path.

	# get a list of tags in the folder or UDT
	tags = system.tag.browseTags(parentPath=folder, recursive=1, sort='ASC')
		
	# Create a list of tag paths
	paths = []
	for tag in tags:
		paths.append(tag.fullPath)
				
	# read the tags all at once
	vals = system.tag.readAll(paths)

	# store the values in a dictionary
	values = {}
	for x in range(len(paths)):
		length = len(paths[x]) - len(folder)
		name = str(paths[x])[-length:]
		# add each tag to a dictionary
		values[name] = vals[x].value
1 Like

Thank you, this does what I need perfectly.

Now that system.tag.browseTags() is deprecated, I'm looking for a solution that uses the new system.tab.browse() function.

I have a couple instances of a UDT and I want to reach all of the members via scripting.

I believe this works the same as the one David provided, just changed it to use the new system.tag.browse()

# Get a list of tags in the folder or UDT
tags = system.tag.browse(folder, {"recursive":True, "tagType": "AtomicTag"}).getResults()

# Create a list of tag paths
paths = []
for tag in tags:
	paths.append(str(tag['fullPath']))

# read the tags all at once
vals = system.tag.readBlocking(paths)

#store the values in a dictionary
values = {}
for x in range(len(paths)):
	length = len(paths[x]) - len(folder)
	name = str(paths[x])[-length:]
	# add each tag to a dictionary
	values[name] = vals[x].value

The syntax for system.tag.browse() is entirely different. But I don't actually need to use it in this case (yet).

The jsonValues meta node works for folders and UDT instances and gives you an automatic recursive view of all the tags inside of a UDT:
So from a structure like this:

You can read the folder:

from pprint import pprint
v = system.tag.readBlocking("[default]udts.jsonValues")[0].value
pprint(v)
{
    "beetles": {
        "george": 4,
        "john": 1,
        "paul": 2,
        "ringo": 3
    },
    "ducks": {
        "dewey": 3.14,
        "huey": 123,
        "louie": "abc"
    },
    "family": {
        "folder": {
            "fourInstance": {
                "george": 4,
                "john": 1,
                "paul": 2,
                "ringo": 3
            },
            "threeInstance": {
                "dewey": 3.14,
                "huey": 123,
                "louie": "abc"
            }
        },
        "fourInstance": {
            "george": 4,
            "john": 1,
            "paul": 2,
            "ringo": 3
        },
        "threeInstance": {
            "dewey": 3.14,
            "huey": 123,
            "louie": "abc"
        }
    }
}

Or any UDT instance inside:

from pprint import pprint
v = system.tag.readBlocking("[default]udts/beetles.jsonValues")[0].value
pprint(v)
{
    "george": 4,
    "john": 1,
    "paul": 2,
    "ringo": 3
}