Rules - Looping through nested Group Items

OH Version: 3 stable

Hello

I am trying to turn off the music when there is no presence on the floor.

For that i have this rules that work fine:

rule "Alexa Pause wenn OG verlassen wird"
when
    Member of OG_Praesenz changed
then
    if (OG_Praesenz.state == OFF)
    {
        OG_Echo_Player.members.forEach[ i | i.sendCommand(PAUSE) ]
    }
end

rule "Alexa Pause wenn EG verlassen wird"
when
    Member of EG_Praesenz changed
then
    if (EG_Praesenz.state == OFF)
    {
        EG_Echo_Player.members.forEach[ i | i.sendCommand(PAUSE) ]
    }
end

rule "Alexa Pause wenn KG verlassen wird"
when
    Member of KG_Praesenz changed
then
    if (KG_Praesenz.state == OFF)
    {
        KG_Echo_Player.members.forEach[ i | i.sendCommand(PAUSE) ]
    }
end

I tried something like this:

rule "Alexa Pause wenn Stockwerk verlassen wird"
when
    Member of gPraesenzStockwerk changed
then
    if (triggeringItem.state == OFF)
    {
        val Stockwerk_Echo = ScriptServiceUtil.getItemRegistry?.getItem(triggeringItem.name.substring(0, triggeringItem.name.lastIndexOf('_'))+"_Echo_Player")
        Stockwerk_Echo.members.forEach[ i | i.sendCommand(PAUSE) ]

    }
end

But i am getting an error. I think it is because of the item i created. This approach works well if i want to get an item (creating the val/var and sending messages to the corresponding item). but it seems not to work for groups. It is working with the 3 rules but for future rules it would be nice to know why it is not possible with one.
Here is the error:

Here are my items:

Switch    KG_Praesenz                                     "KG Präsenz"                                                         (gPraesenzStockwerk)          {channel = "knx:device:bridge:Umweltsensoren:KG_Praesenz"}
Switch    EG_Praesenz                                     "EG Präsenz"                                                         (gPraesenzStockwerk)          {channel = "knx:device:bridge:Umweltsensoren:EG_Praesenz"}
Switch    OG_Praesenz                                     "OG Präsenz"                                                         (gPraesenzStockwerk)          {channel = "knx:device:bridge:Umweltsensoren:OG_Praesenz"}


Group                      KG_Echo_Player                                                                                      
Group                      EG_Echo_Player                                                                                      
Group                      OG_Echo_Player 
Player                     EG_Essbereich_Echo_Player                   "Player"                                                    (EG_Essbereich_Echo_Gruppe, EG_Echo_Player, Haus_Echo_Player)                     {channel="amazonechocontrol:echo:Jonas:EG_Essbereich_Echo:player"}
.
.
.

Why loop? When you send a command to a Group Item, it forwards that command to all it’s members. So make sure that OG_Echo_Player is a Group:Player and you can change the loop to

OG_Echo_Player.sendCommand(PAUSE)

OK, an now that you are trying to get clever, knowing that you can send a command to a Group to send it to all it’s members coupled with the knowledge that there is a sendCommand action that takes the name of the Item as a String your new rule could use

sendCommand(triggeringItem.name.replace('Praesenz', 'Echo_Player'), 'PAUSE')

No need to mess with the ItemRegistry at all.

I can’t read screen shots of error logs on my phone so I can’t say what the actual problem is but I’d guess that your sunstring is not correct and you are not constructing the name of the Group Item correctly. Always log stuff out in cases like this so you know what’s going on. Never assume you know what’s working. If your assumptions were correct, it would be working.

1 Like

Thank you so much, Mr. Koshak!

That replace function had such a big impact on my textual rule configuration that you cannot imagine :wink:

I literally used that string manipulation of the item registry on 50 % of my rules. Now i am switching. It makes it so much easier. Not only for lines to write but also for understanding whats going on at a later state.