Count number of players in a group with state "PLAY"

Hi all,

I would like to count the number of players in a group that have the state PLAY. I tried two different ways, but none of them worked:

rule "Anzahl der eingeschalteten Radios"
when
    Item GroupRadioControl changed
then
    ItemRadioPlayerCount.postUpdate(GroupRadioControl.members.filter[state == PLAY].length)
end

or

Group:Player:COUNT(PLAY) GroupRadioControl

Is there a way to achieve this - or do I approach this from a wrong angle?
I’m using OH 2.5 with the Sonos binding.

with kind regards,
Patrik

Does your rule ever run? i.e. does thestate of the Group Item change - you can see that in your events.log

Thanks :slight_smile:, the rule was indeed not executed. But I noticed that the simple approach without a rule also works:

Group:Player:COUNT(PLAY) GroupRadioControl "[%d]" <radio> (GroupDebug) 

(There was a mistake in my item definition).

with kind regards,
Patrik

1 Like

Just for the record: You could also use a rule rule for this, but it’s slightly different:

rule "Anzahl der eingeschalteten Radios"
when
    Member of GroupRadioControl changed
then
    ItemRadioPlayerCount.postUpdate(GroupRadioControl.members.filter[i|i.state == PLAY].size)
end

So, it’s another trigger, the filter needs to use an object and as the result of the filtering of a list is a list, you need the size of the list, not the length.

:wink: