# Help with forEach command to grab one item and loop to another Item

**URL:** <https://community.openhab.org/t/help-with-foreach-command-to-grab-one-item-and-loop-to-another-item/74390>\
**Category:** Beginners\
**Created:** [May 14, 2019, 2:52am UTC](https://community.openhab.org/t/help-with-foreach-command-to-grab-one-item-and-loop-to-another-item/74390 "2019-05-14T02:52:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fals3illusion](https://community.openhab.org/user_avatar/community.openhab.org/fals3illusion/32/18884_2.png) [@fals3illusion](https://community.openhab.org/u/fals3illusion)\
**Post date:** [May 14, 2019, 2:52am UTC](https://community.openhab.org/t/help-with-foreach-command-to-grab-one-item-and-loop-to-another-item/74390/1 "2019-05-14T02:52:30Z")

</div>

So i am wondering if this scenario is possible using a forEach code. Here is how i am set up.

I have some SONOS One’s and Echo Dot with Alexa integrated to it. I also have rules that when my doorbell is rung or washer is complete, etc it notifies me over TTS.

I am currently using a forEach command to set the volumes to a constant number of the SONOS / Echo Dot state and looping it to everything in the group with the forEach command.

What I am wondering, is it possible to use a forEach code to grab each individual volume and set it to another item in a totally different group.

```csv
Dimmer Echo_TV_Room_TTS_Volume "TV Room Text to Speech Volume [%s]"	(gTVRoom_EchoDot, gSonos_TTS_Vol) 																			
Dimmer Echo_TV_Room_Music_Vol "TV Room Music Volume [%s]" (gTVRoom_EchoDot, g Sonos_Music_Vol)

```

So what i want is to loop through the members and and grab the \_Music\_Vol from each item then assign it to to same name’s \_TTS\_Volume.

I don’t know if i am asking this correctly or if this is even possible. I am looking to save myself from writing a lot of small code for each individual sonos player / Echo Dotso whenever one item is updated it updates the other.

Thanks!

---

<div class="post-metadata">

**Author:** ![delid4ve](https://community.openhab.org/user_avatar/community.openhab.org/delid4ve/32/48527_2.png) [@delid4ve](https://community.openhab.org/u/delid4ve)\
**Post date:** [May 14, 2019, 4:19am UTC](https://community.openhab.org/t/help-with-foreach-command-to-grab-one-item-and-loop-to-another-item/74390/2 "2019-05-14T04:19:49Z")

</div>

So you need variables declared first:

```csv
var String ItemName
var String VolumeItem
var String TTSItem
var Number Volume

```

Within the loop you would need to do something like:

```csv
ItemName = Echo_TV_Room_Music_Vol.substringBetween(“Echo_”,”_Room”)
VolumeItem = “Echo_” + ItemName + “_Room_Music_Vol”
Volume = Integer::parseInt(ItemName.state.toString)
TTSItem = “Echo_” + ItemName + “_Room_TTS_Vol”
TTSItem.postUpdate(Integer::parseInt(Volume))

```

You’ll just have to work out the best way to set your ItemName as without seeing them all it’s hard to say

Have a look at this for a way to loop over a group:

> [@\[Solved\] Loop through group which is member of group](https://community.openhab.org/t/solved-loop-through-group-which-is-member-of-group/27992):
>
> Hi! I’m trying to loop through a group (using ‘forEach’) where the group object is coming from another ‘forEach’ loop. Explanation: The item setup is something like this: Group grpProfiles Group grpProfile1 (grpProfiles) Group grpProfile2 (grpProfiles) Number Profile1\_Item1 (grpProfile1) Number Profile1\_Item2 (grpProfile1) Number Profile1\_Item3 (grpProfile1) Number Profile2\_Item1 (grpProfile2) Number Profile2\_Item2 (grpProfile2) Number Profile2\_Item3 (grpProfile2) Now a rule which is f…

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [May 14, 2019, 3:25pm UTC](https://community.openhab.org/t/help-with-foreach-command-to-grab-one-item-and-loop-to-another-item/74390/3 "2019-05-14T15:25:22Z")

</div>

There are a few ways to do this. Probably the simplest is documented in [Design Pattern: Associated Items](https://community.openhab.org/t/design-pattern-associated-items/15790).

Also see [Design Pattern: Working with Groups in Rules](https://community.openhab.org/t/design-pattern-working-with-groups-in-rules/20512) for a deeper discussion of what you can do with Group’s members.
