# Dynamic referencing in property Change script

**URL:** <https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803>\
**Category:** Ignition\
**Tags:** vision, scripting, ignition81\
**Created:** [April 25, 2022, 8:49am UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803 "2022-04-25T08:49:08Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas.Stokke](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@Thomas.Stokke](https://forum.inductiveautomation.com/u/Thomas.Stokke)\
**Post date:** [April 25, 2022, 8:49am UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/1 "2022-04-25T08:49:08Z")

</div>

Hi,

Im fairly new to text programming and was wondering if there was a function or notation in scripting that works the same way as in indirect tags.

Where in indirect tags I would use {1} to reference another variable.

Im trying to do a simple if statement in a propertyChange script.

example:

```auto
if event.propertyName == "DeviceChange"

  DynamicDeviceName = "DeviceName" #F1,F2,F3,F4,F5,F6,F7,F8 and so on........
  #The DynamicDeviceName has over 40 different possibilities.

  if event.source.Service."DynamicDeviceName" == 1: #DynamicDeviceName is defined by which DeviceName is present.
     print "Good Vibes only"

  if event.source.Service.F1 == 1:
     print "SomeCoolStuff"
  #F1 is a device but it can also be: 
  if event.source.Service.F2 == 1:
     print "SomethingElse"
  #since "F2" is also a Device in the udt

  # event.source.Service is a reference to a Template property called Service which is a UDT that contains 42 booleans. 
  # So to call upon the wanted information I Wish to call upon event.source.Service.F1 when the DeviceName is F1

```

Thanks,  
Thomas

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 25, 2022, 10:50am UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/2 "2022-04-25T10:50:42Z")

</div>

Can you edit your question (pencil icon), select each piece of code and use the `</>` code formatting button to format your code? This will preserve indentation, apply syntax highlighting and make it much easier to read.

---

<div class="post-metadata">

**Author:** ![louagejasper](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/louagejasper/32/86311_2.png) [@louagejasper](https://forum.inductiveautomation.com/u/louagejasper)\
**Post date:** [April 25, 2022, 10:51am UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/3 "2022-04-25T10:51:53Z")

</div>

I don’t really understand the question very well, could you rephrase it a bit?

You can use variables in Python like this:

```auto
myVar = 5
print myVar
#This will print '5' 

```

---

<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:** [April 25, 2022, 11:32am UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/4 "2022-04-25T11:32:13Z")

</div>

There is possibly a way to accomplish what you are trying to do, but you haven’t given enough information.

What type is `event.source.Service`? I say that because as far as I am aware no component has a `Service` property.

What is the overall goal here, there may be a much better way to achieve whatever it is you are trying to achieve.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [April 25, 2022, 12:48pm UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/5 "2022-04-25T12:48:39Z")

</div>

> [@Thomas.Stokke](#):
>
> `event.source.Service."DynamicDeviceName"`

Unless service is a property on a custom component from a module, I don't think this is valid. Though even if its a custom component from a module, I don't think this is valid python regardless.

If you are new to Ignition I highly recommend going through the following - [Inductive University Course List - Learn Ignition Free](https://inductiveuniversity.com/courses/ignition-overview/8.1)

If you can explain what end goal you're trying to achieve exactly, it will help us better to help you.

---

<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:** [April 25, 2022, 12:56pm UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/6 "2022-04-25T12:56:37Z")

</div>

> [@Thomas.Stokke](#):
>
> `event.source.Service is a reference to a Template property called Service which is a UDT that contains 42 booleans`

UDT is not a valid Python Type.

Can you run something like:

```auto
print type(event.source.Service)

```

And let us know what is printed in the console?

---

<div class="post-metadata">

**Author:** ![Thomas.Stokke](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@Thomas.Stokke](https://forum.inductiveautomation.com/u/Thomas.Stokke)\
**Post date:** [April 25, 2022, 1:10pm UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/7 "2022-04-25T13:10:41Z")

</div>

\<type ‘com.inductiveautomation.factorypmi.application.binding.UDTProperty’\>

---

<div class="post-metadata">

**Author:** ![Thomas.Stokke](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@Thomas.Stokke](https://forum.inductiveautomation.com/u/Thomas.Stokke)\
**Post date:** [April 25, 2022, 1:24pm UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/8 "2022-04-25T13:24:43Z")

</div>

The goal is to set a status based on a boolean that declares if the machine is out for service

I created a user defined tag UDT containing the different tags.

I basically want to insert a string containing the DeviceName into my if statement “event.source.Service.” and from there reach the “position” in the UDT (User defined Tag)and get the False/True from the tag.

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/0/4044750228b1016a7b568e352f79f6805273d969.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:** [April 25, 2022, 1:56pm UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/9 "2022-04-25T13:56:22Z")

</div>

This:

```auto
if event.source.Service.F1:

```

is not going to work. Because the type UDTProperty doesn’t have an F1(or whatever) method.

Something like this might work though (Not even close to being tested):

```auto
if event.source.Service.TagPath.getItemName() == 'F1':
    if event.source.Service.getValue(event.source.Service.TagPath).value:
         print "Some Cool Stuff"

```

Honestly though, I’m not sure you’re approaching this correctly? Is one indicator going to show the status of 42 different booleans? Why won’t an indirect tag binding work?

I would consider using two properties on the template One that holds the base path to the UDT and another that holds the Device name. Then use those to build and indirect tag binding for the status indicator. No script needed.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [April 25, 2022, 2:07pm UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/10 "2022-04-25T14:07:23Z")

</div>

What propertyChange event is this firing on? I would recommend against putting this on any vision component’s property change. Seems like this should be a gateway tag change event or in the gateway context of some sort.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 25, 2022, 4:21pm UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/11 "2022-04-25T16:21:47Z")

</div>

It can be done using eval(), but, just because it _can_ be done doesn’t mean that it _should_ be done. eval() can be very dangerous to use as it will try to execute anything you put into it. Even bad stuff…

```python
service = event.source.parent.getComponent('LH Container').Service

deviceList = ['F1', 'F2', 'F3']
for device in deviceList:
	print eval('service.{}'.format(device))

```

I would add memory tags to your UDT, write a gateway timer script to poll the tags, and then set the memory tags to whatever status you need.

---

<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:** [April 25, 2022, 4:23pm UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/12 "2022-04-25T16:23:43Z")

</div>

Surely, use `getattr` before `eval`, unless I’m missing something about the problem statement:  
[https://docs.python.org/2.7/library/functions.html#getattr](https://docs.python.org/2.7/library/functions.html#getattr)

(I _do_ agree with others that there’s probably a better way to architect this whole thing, though)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 25, 2022, 4:30pm UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/13 "2022-04-25T16:30:10Z")

</div>

> [@paul-griffith](#):
>
> Surely, use `getattr` before `eval`

Huh. I thought I had had tried it earlier, but looking back at my notes, I had tried ` __getattribute__ `. A subtle but important difference.

---

<div class="post-metadata">

**Author:** ![Thomas.Stokke](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@Thomas.Stokke](https://forum.inductiveautomation.com/u/Thomas.Stokke)\
**Post date:** [April 26, 2022, 6:49am UTC](https://forum.inductiveautomation.com/t/dynamic-referencing-in-property-change-script/58803/14 "2022-04-26T06:49:51Z")

</div>

Greetings,

I’ve Taken your recommendations into account and bound the tag directly as a Template Parameter.

so instead of the UDT being bound to the template, now instead it is the boolean that is a parameter.

thank you for your replies!
