# What is the best way to iterate through components or properties?

**URL:** <https://forum.inductiveautomation.com/t/what-is-the-best-way-to-iterate-through-components-or-properties/108301>\
**Category:** Ignition\
**Tags:** ignition81, vision, scripting\
**Created:** [July 10, 2025, 1:49am UTC](https://forum.inductiveautomation.com/t/what-is-the-best-way-to-iterate-through-components-or-properties/108301 "2025-07-10T01:49:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 10, 2025, 1:49am UTC](https://forum.inductiveautomation.com/t/what-is-the-best-way-to-iterate-through-components-or-properties/108301/1 "2025-07-10T01:49:15Z")

</div>

Hi all,

Currently we have a script in a toggle button event `itemStateChanged` that iterates over 19 components, checking the current, previous high and previous low values, then sets a new previous high or low. So that's 57 values involved.

```python
listOfZones = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19']
if event.source.selected:
	for item in listOfZones:
		currentValue=event.source.parent.parent.getComponent('CZ '+item+' Current Value').value
		if event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousHigh == 1e-2:
			event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousLow = currentValue / 5
			event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousHigh = event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousLow * 10
		if currentValue < event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousLow * 1.18 or currentValue > event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousHigh*.9:
			event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousLow = currentValue / 5
			event.source.parent.parent.getComponent('CZ '+item+' Current Value').previousHigh = event.source.previousLow * 10

```

I was reading some best practice tips on this thread ([Do's and Don'ts when developing First Project with Ignition? - #2 by danielps1818](https://forum.inductiveautomation.com/t/dos-and-donts-when-developing-first-project-with-ignition/93592/2)) and am wondering if it would be reasonable to try and stuff this into a project script. It will only run on a radio button selection, so once or twice while the user is interacting with the screen.  
While considering this, to try and move away from the `getComponent()` stuff, I thought I would use custom props on the root container. That would mean, currently, 57 custom props and I would need a way to iterate over those based on the name as shown in the `listOfZones` above. Something like `event.source.parent._{zoneName}currentValue` equating to `event.source.parent._01currentValue` or something similar. Then retreiving and or modifying those values.

Do you have any recommendations on this? Am I better off leaving it as is?  
Thanks

---

<div class="post-metadata">

**Author:** ![danielps1818](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@danielps1818](https://forum.inductiveautomation.com/u/danielps1818)\
**Post date:** [July 10, 2025, 7:28am UTC](https://forum.inductiveautomation.com/t/what-is-the-best-way-to-iterate-through-components-or-properties/108301/2 "2025-07-10T07:28:19Z")

</div>

Let my understand this. You want to iterate throw 19 components to set new high and low values? but only when a button is press (a toggle button).

The first solution I'm thinking (without testing) would be creating a custom propertie that is a dict.  
Then you could link each component with it's ID key and set it's high and low value. Then make that link bidirectional so whenever you press the button you can simply write in that dictionary.

---

<div class="post-metadata">

**Author:** ![fakcior](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@fakcior](https://forum.inductiveautomation.com/u/fakcior)\
**Post date:** [July 10, 2025, 9:10am UTC](https://forum.inductiveautomation.com/t/what-is-the-best-way-to-iterate-through-components-or-properties/108301/3 "2025-07-10T09:10:50Z")

</div>

Seems like Message Handler is perfect fit for this task:

> **[Component Message Handlers | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/ignition-modules/perspective/scripting-in-perspective/component-message-handlers)**
>
> In Perspective, Component Message Handlers are the preferred way to pass parameters between components or Views. Doing this involves the system.perspective.sendMessage function. There are typically two steps involved:

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 10, 2025, 11:36am UTC](https://forum.inductiveautomation.com/t/what-is-the-best-way-to-iterate-through-components-or-properties/108301/4 "2025-07-10T11:36:01Z")

</div>

Use intermediate variables to hold containers and components. And don't initialized variables if you aren't going to use them. Like so:

```python
if event.source.selected:
	container = event.source.parent.parent
	listOfZones = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19']
	for item in listOfZones:
		component = container.getComponent('CZ '+item+' Current Value')
		currentValue=component.value
		if component.previousHigh == 1e-2:
			component.previousLow = currentValue / 5
			component.previousHigh = component.previousLow * 10
		if currentValue < component.previousLow * 1.18 or currentValue > component.previousHigh*.9:
			component.previousLow = currentValue / 5
			component.previousHigh = event.source.previousLow * 10

```

The script will run faster and be easier to maintain.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 10, 2025, 3:57pm UTC](https://forum.inductiveautomation.com/t/what-is-the-best-way-to-iterate-through-components-or-properties/108301/5 "2025-07-10T15:57:59Z")

</div>

This is Vision, not Perspective.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [July 10, 2025, 8:24pm UTC](https://forum.inductiveautomation.com/t/what-is-the-best-way-to-iterate-through-components-or-properties/108301/6 "2025-07-10T20:24:52Z")

</div>

I like this a lot better. And, for my mind's sake, it probably does not make any sense to use a library script here, as it seems it would make this much more complicated. Many values to pass back and forth for each component.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 10, 2025, 8:58pm UTC](https://forum.inductiveautomation.com/t/what-is-the-best-way-to-iterate-through-components-or-properties/108301/7 "2025-07-10T20:58:17Z")

</div>

I would use a library script, passing the event object.
