# Writing Values to PLC UDT from Gateway

**URL:** <https://forum.inductiveautomation.com/t/writing-values-to-plc-udt-from-gateway/78910>\
**Category:** Ignition\
**Tags:** ignition81, perspective, scripting, database\
**Created:** [September 8, 2023, 1:37pm UTC](https://forum.inductiveautomation.com/t/writing-values-to-plc-udt-from-gateway/78910 "2023-09-08T13:37:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [September 8, 2023, 1:37pm UTC](https://forum.inductiveautomation.com/t/writing-values-to-plc-udt-from-gateway/78910/1 "2023-09-08T13:37:29Z")

</div>

Hello all,

I'm working on a project where I will need to loop through our unit info database and input data into a UDT on the PLC so the Engineers can look through it and do different things with the tags on the PLC side of things. The database will be changing, so I will need to use some for loops to accomplish this and plan to just run this script every 15 minutes from the gateway. There is a max of 500 in the UDT; however, we only have 87 entries currently.

The way the tags are separated is "Unit\_BOM\_x/Unit\_BOM\_000/../..", where Unit\_BOM\_000 goes all the way to "Unit\_BOM\_499" and within each of these folders are 15 different named folders named "Base\_Model", "Base\_Platform", "Basket\_Model", "Basket\_Platform", "Cab\_Model", "Cab\_Platform", "Drive\_Model", "Drive\_Platform", "Model\_Number", "Platform", "Spare1\_Model", "Spare1\_Platform", "Spare2\_Model" and "Spare2\_Platform" for different parts model/platform. Inside of those folders is DATA and LEN, I will only be using the DATA bit. The folders are in the same order and follow the same naming conventions for all 500 UDT entries.

I'm pretty stuck on this one, I'm not too experienced with writing to this many tags at once and spreading them out to other addresses. I'm lost on how I can sparse out the info to the specific tags for all 87 units we currently have. I am currently only going to be writing to "Basket\_Model/DATA", "Basket\_Platform/DATA", "Drive\_Model/DATA" and "Drive\_Platform/DATA". The rest is for expansion in the future.

I created a script to loop through all of the "Unit\_BOM\_xxx" records to create a path for the tag writing inside of the for loop but get stuck when having to dig further, as the other folders have different names, then I have to dig into that for the DATA bit.

```auto
def onScheduledEvent():
	for tagPath in range(500):
		title = "Unit_BOM_x/Unit_BOM_"
		digit = str(tagPath)
		title = title + digit

```

I guess I'm basically just asking for help on if anyone knows how I would proceed. I need to run a for loop on my query then disperse that data to these paths. For that loop, I have the following query:

```auto
def onScheduledEvent():
	for row in range(returnedDataset.getRowCount()):
		ModelNumber = returnedDataset.getValueAt(row, "ModelNum")
		DrivePlatform = returnedDataset.getValueAt(row, "DrivePlatform")
		BasketPlatform = returnedDataset.getValueAt(row, "BasketPlatform")

```

Any help or pointing in the right direction is highly appreciated. Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [September 8, 2023, 2:43pm UTC](https://forum.inductiveautomation.com/t/writing-values-to-plc-udt-from-gateway/78910/2 "2023-09-08T14:43:31Z")

</div>

To begin, you can use [system.dataset.filterColumns(dataset, columns)](https://docs.inductiveautomation.com/display/DOC81/system.dataset.filterColumns) (link to docs) to get a reduced dataset that has just the columns you want. Something like

```auto
	reduced_dataset = system.dataset.filterColumns(returnedDataset, ["ModelNum", "DrivePlatform", "BasketPlatform"]) #etc.

```

Then you can convert that to a flat list of values by looping over it as a pyDataset with list comprehension:

```auto
	reduced_pydataset = system.dataset.toPyDataset(reduced_dataset)
	#values_to_write = [value for value in row for row in reduced_pydataset] wrong order to iterate
	values_to_write = [value for row in reduced_pydataset for value in row] 

```

Once you have that, you can use list comprehension to quickly generate the list of tag (or OPC) paths to write to.

```auto
	path_template = "Unit_BOM_x/Unit_BOM_{:03d}/{}/DATA"
	sub_items = ["Basket_Model", "Drive_Platform", "Basket_Platform"] #etc.
	tag_paths = [path_template.format(unit_number, sub_item) for unit_number in range(88) for sub_item in sub_items] #1 + max number

```

I used `format` to generate the leading zeros in the tag path.

Make sure your `sub_items` list correctly matches the list you used to filter your dataset in the same order. Then it's a matter of `system.tag.writeAsync(tag_paths, values_to_write, callback)` where `callback` is some function (defined by you) that will loop over the returned results and make sure all 87\*3 tags were written correctly.

EDIT: Fixed list comprehension for future reference, see post below.

---

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [September 8, 2023, 5:35pm UTC](https://forum.inductiveautomation.com/t/writing-values-to-plc-udt-from-gateway/78910/3 "2023-09-08T17:35:03Z")

</div>

Wow Felipe, it's impressive how you made light work of this. I'm going to give it a whirl and see how things go. Thank you so much for explaining it in detail as I'm still learning every day. I really appreciate the comments so I can understand how it all works!

---

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [September 8, 2023, 6:55pm UTC](https://forum.inductiveautomation.com/t/writing-values-to-plc-udt-from-gateway/78910/4 "2023-09-08T18:55:58Z")

</div>

This is the error I'm getting:

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

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 8, 2023, 7:25pm UTC](https://forum.inductiveautomation.com/t/writing-values-to-plc-udt-from-gateway/78910/5 "2023-09-08T19:25:12Z")

</div>

You will need to post your script as you have it written. Essentially you have used a variable 'row' prior to assigning it a value.

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [September 8, 2023, 7:28pm UTC](https://forum.inductiveautomation.com/t/writing-values-to-plc-udt-from-gateway/78910/6 "2023-09-08T19:28:49Z")

</div>

My bad, I mixed up the order of the comprehension in that line.  
It's a little counter-intuitive (at least to me) but it should actually be

```auto
values_to_write = [value for row in reduced_pydataset for value in row] 

```

That way it generates `row` before it tries to iterate it.

It makes sense if you think of it as nesting the loops from left to right.

---

<div class="post-metadata">

**Author:** ![Robert\_Hrynciw](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@Robert\_Hrynciw](https://forum.inductiveautomation.com/u/Robert_Hrynciw)\
**Post date:** [September 11, 2023, 12:49pm UTC](https://forum.inductiveautomation.com/t/writing-values-to-plc-udt-from-gateway/78910/7 "2023-09-11T12:49:27Z")

</div>

Works flawlessly. I added a count query to replace that digit so that when it grows/shrinks it will automatically read those new edits. Thank you so much Felipe, you have no idea how helpful you were with this. This will help me a ton with future projects. I appreciate it!
