# Generate a value from a list on an Expression tag

**URL:** <https://forum.inductiveautomation.com/t/generate-a-value-from-a-list-on-an-expression-tag/95738>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [September 9, 2024, 4:24pm UTC](https://forum.inductiveautomation.com/t/generate-a-value-from-a-list-on-an-expression-tag/95738 "2024-09-09T16:24:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Claudio\_Burgos](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@Claudio\_Burgos](https://forum.inductiveautomation.com/u/Claudio_Burgos)\
**Post date:** [September 9, 2024, 4:24pm UTC](https://forum.inductiveautomation.com/t/generate-a-value-from-a-list-on-an-expression-tag/95738/1 "2024-09-09T16:24:51Z")

</div>

```python
I am trying to create an Expression tag to generate values randomly from a list for testing purposes. The code below works on the Scripting Console, but not when I create an Expression tag. Can you help me to see what I am missing, please? Thanks.

import random
import time

# Define a list of values
values = ['1', '2', '3', 'A', 'B', 'C']

# Generate a value from the list every second
while True:
    print random.choice(values) # Selects a random value from the list
    time.sleep(1) # Waits for 1 second

```

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [September 9, 2024, 4:34pm UTC](https://forum.inductiveautomation.com/t/generate-a-value-from-a-list-on-an-expression-tag/95738/2 "2024-09-09T16:34:50Z")

</div>

You'll need to create a script in your scripting library on the gateway configured gateway scripting project (this is key!!) If you don't have a gateway scripting project configured, you'll need one for this to work at all.

Next, you'll create the following script:

```python
import random

def getRandomSelection():
    # Define a list of values
    values = ['1', '2', '3', 'A', 'B', 'C']

    # Selects a random value from the list
    return random.choice(values)  

```

Then for your expression tag you'll need to call this function/script (assuming it's named "RandomValues" just for this example):

```python
runScript('RandomValues.getRandomSelection')

```

Set exectution mode to fixed rate, and define your execution rate (default is 1000ms)

Edit: moved the import out of the fuction per @paul-griffith suggestion.

---

<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:** [September 9, 2024, 4:42pm UTC](https://forum.inductiveautomation.com/t/generate-a-value-from-a-list-on-an-expression-tag/95738/3 "2024-09-09T16:42:50Z")

</div>

> [@michael.flagler](#):
>
> ```python
> def getRandomSelection():
> import random
> 
> ```

If you're in the project library, you should `import` _outside_ the function definition, so you're only doing it once, not every time the function is called.

Otherwise, stellar advice. You _could_ leave the tag in 'execution driven' and use the poll rate parameter on runScript to define your execution rate, but there's really no difference.

I'd also, just for personal taste, think that the selection function should accept an input list to sample over, but that's not in the original post so who knows their requirement.

---

<div class="post-metadata">

**Author:** ![cos](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cos/32/66963_2.png) [@cos](https://forum.inductiveautomation.com/u/cos)\
**Post date:** [September 9, 2024, 5:02pm UTC](https://forum.inductiveautomation.com/t/generate-a-value-from-a-list-on-an-expression-tag/95738/4 "2024-09-09T17:02:50Z")

</div>

There is also this [generic simulator program](https://www.docs.inductiveautomation.com/docs/8.1/ignition-modules/opc-ua/opc-ua-drivers/programmable-device-simulator#generic-simulator-program) if you need random values.

---

<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:** [September 9, 2024, 5:13pm UTC](https://forum.inductiveautomation.com/t/generate-a-value-from-a-list-on-an-expression-tag/95738/5 "2024-09-09T17:13:14Z")

</div>

Yea, I'd probably do this:

```python
from random import choice

def random_choice(*options):
	return choice(options)

```

---

<div class="post-metadata">

**Author:** ![Claudio\_Burgos](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@Claudio\_Burgos](https://forum.inductiveautomation.com/u/Claudio_Burgos)\
**Post date:** [September 9, 2024, 5:21pm UTC](https://forum.inductiveautomation.com/t/generate-a-value-from-a-list-on-an-expression-tag/95738/6 "2024-09-09T17:21:12Z")

</div>

![zz2024-09-09 13_19_52-EDGE_XYL1001_3 - TeamViewer](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/6/16c1f6ebf31251749f517c58af3edf407052584c.jpeg)  
OK, this is how far I get, but I am geeting an error on the Expression evaluation.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [September 9, 2024, 5:33pm UTC](https://forum.inductiveautomation.com/t/generate-a-value-from-a-list-on-an-expression-tag/95738/7 "2024-09-09T17:33:46Z")

</div>

Make sure your project (XYL1001) is set as the Gateway Scripting Project:

> **[Project Library | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-in-ignition/project-library#gateway-scripting-project)**
>
> Scripts under the Project Library are a project-based resource that allows user created Python scripts to be configured. Objects and functions created in a Project Library script can be called from anywhere in the project. Project Library scripts are...

Also, your datatype will need to be set to String since you're passing not only integers, but letters/characters.

---

<div class="post-metadata">

**Author:** ![Claudio\_Burgos](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@Claudio\_Burgos](https://forum.inductiveautomation.com/u/Claudio_Burgos)\
**Post date:** [September 9, 2024, 6:02pm UTC](https://forum.inductiveautomation.com/t/generate-a-value-from-a-list-on-an-expression-tag/95738/8 "2024-09-09T18:02:50Z")

</div>

> [@michael.flagler](#):
>
> Gateway Scripting Project:

That was it! It wasn't pointing to my project. Now it is working! Thanks a lot, everyone! I learned something new today! 🤓

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [September 9, 2024, 7:14pm UTC](https://forum.inductiveautomation.com/t/generate-a-value-from-a-list-on-an-expression-tag/95738/9 "2024-09-09T19:14:58Z")

</div>

Yep, that one's gotten me a couple times.

> **[Gateway Script Project Warning/Notification | Ignition Features and Ideas |...](https://inductiveautomation.canny.io/ignition-features-and-ideas/p/gateway-script-project-warning-notification)**
>
> Supply some sort of warning/notification that the gateway scripting project is blank (default gateway scripting project is "").
