# Writting values to a Text list looking at a DB

**URL:** <https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [November 30, 2021, 4:22pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575 "2021-11-30T16:22:10Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [November 30, 2021, 4:22pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/1 "2021-11-30T16:22:10Z")

</div>

Hi,

I have a big DataBlock consisting of integer Tags(R001-R985 tag format). Each tag represents a value that I would like to write to with a multi-State button that writes 0 1 or 2. Is there a quick way first of all to create a text list displaying each tag of the DB so the user can navigate and select which tag to write to?

I am currently trying with text fields and numeric entries but doesn’t seem to be working. I have set biderectional indirect tag property on the button looking at a text field that the user can write the name of the tag but not able to write on the tag itself.

I.E user writes R0056 on the Text Field and then by pressing the 1of the 3 buttons next to it,should write the value 2 for instance on the R0056 Tag.

` [default]/TestTags/{text}` is binded on the Multi-State Button Request parameter i have created to write 0 1 or 2 for each of the 3 buttons.

I hope this makes sense.

If there is a different easier way to design this would be much appreciated.

Thanks

---

<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:** [November 30, 2021, 4:52pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/2 "2021-11-30T16:52:29Z")

</div>

I would just use a dropdown list for this, unless you really need something more complex.  
Based on your test tag path, I set this up:  
 ![screenshotiupEUU](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/5/85ecccbdd8509b4e50da9734cf716b2412abd745.gif)  
The multi-state button has a straightforward indirect binding:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/f/7f8a13dace29df49d16d61e4547414c1e23e0aef.png)  
Formatting the data for the dropdown is the ‘hardest’ part, but isn’t actually too complicated. I’m using a feature of Python called a list comprehension here, but you can do it ‘imperatively’ as well:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/1/118ab5f724a8aeef668f3f9e24552f1bb98ee989.png)

```python
	return [
		{
			"value": tag,
		 	"label": tag
		}
		for tag in ("R{:03d}".format(i) for i in range(1, 985, 1))
	]

```

```python
	ret = []
	for i in range(1, 985, 1):
		tag = "R{:03d}".format(i)
		ret.append({
					"value": tag,
				 	"label": tag
		})
	return ret

```

---

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [November 30, 2021, 5:50pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/3 "2021-11-30T17:50:35Z")

</div>

> [@paul-griffith](#):
>
> ```auto
> return [
> {
> "value": tag,
> "label": tag
> }
> for tag in ("R{:03d}".format(i) for i in range(1, 985, 1))
> ]
> 
> ```

Are the 2 scripts seperate transfrom scripts binded on the options property of the Dropdown?

---

<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:** [November 30, 2021, 6:09pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/4 "2021-11-30T18:09:51Z")

</div>

It’s two separate scripts that do the same thing; the first is the way I’d do it (more idiomatic Python) but may be less readable if you’re not used to Python. The second is the more ‘imperative’ way, where you create the list, then modify it.

You only need one or the other.

---

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [November 30, 2021, 6:13pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/5 "2021-11-30T18:13:23Z")

</div>

That looked very helpful indeed. However I might need to include more factors here. In my DB I have tags starting with different letters and not all of them increment properly. I think it is easier if I share my tag folder to see what I mean.  
[tags.json](https://forum.inductiveautomation.com/uploads/short-url/bu39f0po5EirVdthcwAhtAOF7I3.json) (303.5 KB)

Would it still be possible to implement that the same way or would it matter as my DB starts with a few Tags starting with letter A then further on with M and R etc. , considering positioning looking at the certain Tag starting on different letter.

For some reason starting from the MS button it game me InvalidPathSyntax Error as you see on the screenshot:

 ![Capture](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/4/14af31ed514f58b4a2131fc5483c4ad53850db04.png)

As you see my Tags also have more numbers(not R001 but R0001) would that be affected?

At the moment I am not able to do any changes on the DB so if not I will go the basic way creating 1024 Multi-State buttons 😂

---

<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:** [November 30, 2021, 6:43pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/6 "2021-11-30T18:43:15Z")

</div>

If you don’t expect the tags to change (often) you can do the easy thing; use a script to generate a hardcoded list of paths, copy it to the clipboard, then paste it onto the dropdown component’s value property:

```auto
import json

options = []
for tag in system.tag.browse("[default]Maint Requests"):
	options.append({"value": tag["name"], "label": tag["name"]})

print json.dumps(options)

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/e/9eace43a384a57540c48ebf591b8cef395adb112.png)  
You can directly paste any JSON string from the clipboard onto a Perspective property.

---

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [November 30, 2021, 8:34pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/7 "2021-11-30T20:34:02Z")

</div>

Hi Paul,

I appreciate your time on this but I’m not sure if I’m doing this properly.

I am not sure where to paste your script: ```import json

options =   
for tag in system.tag.browse("[default]Pick Tower/Maint Requests"):  
options.append({“value”: tag[“name”], “label”: tag[“name”]})

print json.dumps(options, indent = 2)``` as it throws me an error if I paste it onto “options” or “value”.

However I selected all 1024 tags from my Tagfolder I need to display on the dropdown component, copied as JSON, and pasted it onto the options as your screenshot. I do have all tags there not sure if on the very right format though. However I do see an error on the top right corner saying  
" options:object found,array, dataset expected ".

Would that be why when I simulate it I am not able to find any of my tags when I search for them?

Would the first property of my dropdown “value” need any binding or editing?

Where would i have the MultiState button looking at in order to write “0”,“1” or “2” on any tags of the dropdown selected?

Thanks for your time

---

<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:** [November 30, 2021, 8:38pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/8 "2021-11-30T20:38:28Z")

</div>

Take the script I posted and paste it into the script console.  
Grab the output (_not_ the `>>>` part before or after) and copy it to the clipboard.  
Right click the 'options' element in the dropdown list and paste. If what you copied to the clipboard was correct, it should automatically create an option in the dropdown for each tag in the `Maint Requests` folder.

That's what I mean by doing it manually; this isn't an automated process where adding new tags will automatically update the dropdown. But, if you don't expect to add or remove tags very frequently, this is the easy way to get the information you want into the dropdown.

> [@alexm](#):
>
> Where would i have the MultiState button looking at in order to write “0”,“1” or “2” on any tags of the dropdown selected?

That would be handled by the indirect binding explained above. Notice that the 'Bidirectional' binding is checked. A normal binding will update its target property when the source (in this case, the tag) changes. A bidirectional binding does that, _and_ the other direction - when the target property changes, _so does the tag_.

---

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [November 30, 2021, 8:51pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/9 "2021-11-30T20:51:33Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/5/75796fa0ce5b6683079455e7b91375adb080c240.png)

Can’t use cntr+A to mark it all and the output script ends like that so not sure If I am able to copy the right part?

---

<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:** [November 30, 2021, 8:52pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/10 "2021-11-30T20:52:36Z")

</div>

Try removing the `indent = 2` part of the code snippet so it doesn’t try to pretty-print the output.

---

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [November 30, 2021, 8:57pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/11 "2021-11-30T20:57:50Z")

</div>

import json

options =   
for tag in system.tag.browse("[default]/Maint Requests"):  
options.append({“value”: tag[“name”], “label”: tag[“name”]})

print json.dumps(options)

No luck…

---

<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:** [November 30, 2021, 9:04pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/12 "2021-11-30T21:04:30Z")

</div>

![screenshotiVGh9S](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/2/02c5b05da4e588110918abc544fc25c3d9d0c288.gif)  
[options.json](https://forum.inductiveautomation.com/uploads/short-url/ZcD2RcFp6Q652j4yLXFs8yOM1Z.json) (69.8 KB)

---

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [December 1, 2021, 3:49pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/13 "2021-12-01T15:49:11Z")

</div>

Would this be implemented the exact same way if the dropdown was a multiselection one as I would need to select several tags and write them statically 0 1 or 2 and not change them until I stop them from the MSbutton if that makes sense? I believe that’s the reason I get error on my Multistate button on the indirect tag see below:

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

However it works perfectly only with single selection dropdown list.

---

<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:** [December 1, 2021, 4:25pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/14 "2021-12-01T16:25:10Z")

</div>

If you want a multiselect dropdown, things immediately get a lot more complicated.

For one, what ‘Indicator Value’ would you display? You’re selecting multiple (possibly/probably contradictary) values - which one ‘wins’ and gets display on the HOA?  
Also, as you noticed, writing will no longer work via a simple bidirectional binding. You _have_ to use scripting to do the writing in that case.

A somewhat different option for the UI here:  
Use a table, with an HOA button in a subview:  
[hoa-table.zip](https://forum.inductiveautomation.com/uploads/short-url/qp3TgJnN4J41NVlyVJpUhPoyVC1.zip) (30.3 KB)  
 ![screenshotJYsVZ2](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/d/dd78edb4f539670b03fda44e30f7a57263606743.gif)

This might not suit your needs, but there’s no ‘perfect’ paradigm to display (and control) ~1050 tags on the same screen, in my opinion.

---

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [December 1, 2021, 4:51pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/15 "2021-12-01T16:51:12Z")

</div>

In fact, I will be able to use the single selection dropdown, it can be changed from the PLC code easily. thanks for the info though.

Last question for this, regarding on the script for inserting the integers from a DB onto a dropdown’s option property. Would that still work with a “document” data type as well? If not how would the script be changed?

Thanks for your support , it has been great help indeed!

---

<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:** [December 1, 2021, 5:05pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/16 "2021-12-01T17:05:21Z")

</div>

> [@alexm](#):
>
> Last question for this, regarding on the script for inserting the integers from a DB onto a dropdown’s option property. Would that still work with a “document” data type as well? If not how would the script be changed?

You could modify that script to write the options directly to a document tag, yes. That might be a good way to run the browse operation periodically (so you always have an accurate list of tags in the dropdown) without too much performance impact (you don't want to be continuously calling a browse operation on a large flat folder _for each Perspective session you have open_).

---

<div class="post-metadata">

**Author:** ![alexm](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexm](https://forum.inductiveautomation.com/u/alexm)\
**Post date:** [December 17, 2021, 11:15am UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/17 "2021-12-17T11:15:04Z")

</div>

Hi,

I am looking to create a table displaying the state of the tags. The table needs to display the name of the Tags as well as their states. So if a tag=0 print “OFF”, if tag=1 print “Slow Speed” and when tag=2 print “Production Speed”. Is there a quick way to implement this on the 1049 tags? Possibly by modifying the script above? If not would there be another way to achieve this task? Thanks

---

<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:** [December 17, 2021, 3:29pm UTC](https://forum.inductiveautomation.com/t/writting-values-to-a-text-list-looking-at-a-db/53575/18 "2021-12-17T15:29:47Z")

</div>

Yes, it would be _possible_ using the script above, but it’s somewhat tricky to bring in ‘live’ values in that case. I would recommend hardcoding the list of tagpaths somewhere, (i.e. in a script library) instead of running `browse` each time you need to list out these tag’s values.  
To get updated tag values you should issue a single `system.tag.readBlocking` for all 1049 values; this is vastly more efficient than reading one at a time.  
So, you should have a structure in a project script (let’s call it `constants`):

```auto
maintenanceTags = [
    "R001",
    "R002",
    etc
]

```

Then on a table component, you could bring in the data in a variety of ways.  
One would be an expression binding with a script transform. The expression isn’t very important, it’s basically just being used to fire a script.  
The script would be something like this:

```python
states = {
	0: "OFF",
	1: "Slow Speed",
	2: "Production Speed"
}

values = [qv.value for qv in system.tag.readBlocking(constants.maintenanceTags)]

options = []
for tag, value in zip(constants.maintenanceTags, values):
	options.append({
		"name": tag,
		"state": states.get(value, "Unknown")
	})

return options

```

You use a separate dictionary to encode the integer : state description mapping.
