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

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.

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.


So you need variables declared first:

var String ItemName
var String VolumeItem
var String TTSItem
var Number Volume

Within the loop you would need to do something like:

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”

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:

There are a few ways to do this. Probably the simplest is documented in Design Pattern: Associated Items.

Also see Design Pattern: Working with Groups in Rules for a deeper discussion of what you can do with Group’s members.