# How to generalize this script to include all tags in a UDT instead of listing it one by one

**URL:** <https://forum.inductiveautomation.com/t/how-to-generalize-this-script-to-include-all-tags-in-a-udt-instead-of-listing-it-one-by-one/60507>\
**Category:** Ignition\
**Tags:** scripting, vision\
**Created:** [June 8, 2022, 6:29pm UTC](https://forum.inductiveautomation.com/t/how-to-generalize-this-script-to-include-all-tags-in-a-udt-instead-of-listing-it-one-by-one/60507 "2022-06-08T18:29:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemanthkramakrishnan](https://avatars.discourse-cdn.com/v4/letter/h/e79b87/32.png) [@hemanthkramakrishnan](https://forum.inductiveautomation.com/u/hemanthkramakrishnan)\
**Post date:** [June 8, 2022, 6:29pm UTC](https://forum.inductiveautomation.com/t/how-to-generalize-this-script-to-include-all-tags-in-a-udt-instead-of-listing-it-one-by-one/60507/1 "2022-06-08T18:29:41Z")

</div>

```auto
def calc_avg():
	paths = ["[default]Machines/20902_L9_OD_GRINDER_1/Cycle_Time", "[default]Machines/20903_L9_OD_GRINDER_2/Cycle_Time", "[default]Machines/20904_L9_OD_GRINDER_3/Cycle_Time", "[default]Machines/20905_L9_OD_GRINDER_4/Cycle_Time"]
	value = [0] * len(paths)
	for x in range(len(paths)):
		endTime = system.date.now()
		startTime = system.date.addMinutes(endTime, -30)
		dataSet = system.tag.queryTagCalculations(paths=[paths[x]], startDate=startTime, endDate=endTime, calculations=["Average"])
		value[x] = dataSet.getValueAt(0, 1)
	outpaths =["[default]Machines/20902_L9_OD_GRINDER_1/CycleTimeAverage","[default]Machines/20903_L9_OD_GRINDER_2/CycleTimeAverage","[default]Machines/20904_L9_OD_GRINDER_3/CycleTimeAverage","[default]Machines/20905_L9_OD_GRINDER_4/CycleTimeAverage"]
	system.tag.writeBlocking(outpaths, value, 1000)

```

I’d like to know how to link the paths to a UDT property so that I don’t have to list individual path and value tags.

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [June 9, 2022, 6:15am UTC](https://forum.inductiveautomation.com/t/how-to-generalize-this-script-to-include-all-tags-in-a-udt-instead-of-listing-it-one-by-one/60507/2 "2022-06-09T06:15:01Z")

</div>

you can try something with this i guess  
[https://docs.inductiveautomation.com/display/DOC81/system.tag.browse](https://docs.inductiveautomation.com/display/DOC81/system.tag.browse)

wildcard on the name `*Machines/20903_L9_OD_GRINDER_*` or something

---

<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:** [June 9, 2022, 11:08am UTC](https://forum.inductiveautomation.com/t/how-to-generalize-this-script-to-include-all-tags-in-a-udt-instead-of-listing-it-one-by-one/60507/3 "2022-06-09T11:08:47Z")

</div>

There are a few things that can be improved here.

```python
for x in range(len(paths)):

```

Whenever you find yourself using `range(len(something))`, there’s a better way.

```python
for path in paths:

```

is the pythonic way of looping through an iterable (list, set, dict…)

```python
	endTime = system.date.now()
	startTime = system.date.addMinutes(endTime, -30)

```

Why do this in a loop ? Do you need to recalculate the start and end time each and every time ?

```python
		dataSet = system.tag.queryTagCalculations(paths=[paths[x]], startDate=startTime, endDate=endTime, calculations=["Average"])

```

`system.tag.queryTagCalculations` can take a list of paths, to avoid having to call it for each tag you need calculations on.

If we put all this together:

```python
def calc_avg():
	paths = [...]
	outpaths = [...]
	end_time = system.date.now()
	start_time = system.date.addMinutes(end_time, -30)
	calculations = system.tag.queryTagCalculations(paths, startDate=start_time, endDate=end_time, calculations=["Average"])
	# I don't use datasets directly and I don't have ignition on this machine to test this, so it might need some adjusting.
	values = [calculations.getValueAt(0, col) for col in range(calculations.getColumnCount())][1:]
	system.tag.writeBlocking(outpaths, values, 1000)

```

Now, if you need to makes the paths dynamic, `system.tag.browse` can help you as @victordcq suggested, but I’m not sure you can get UDT properties with it, you’ll have to try.  
If it doesn’t work, `system.tag.getConfiguration` might be what you need.
