[SOLVED] Group for String item as a switch and Switch

That is by far the least useful use for Groups. Groups are most powerful when used in Rules and in Persistence. Look at the Design Pattern posts and you will find very few that do not utilize Groups.

And you can indeed control the members of a Group. When you send a command to a Group that command gets forwarded to all of it’s members. So, for example, one can create a Group:Switch:OR(ON,OFF) AllLights and if you AllLights.sendCommand(OFF) all members of AllLights will receive an OFF command.

Oh, and what’s the deal with the OR(ON,OFF)? Groups can aggregate the state of all of it’s members with a set of functions. In this example it means “if any member is ON, the Group’s state is ON, otherwise the Group’s state is OFF”. Put another way, the Group’s state will be ON if any member of the Group is ON.

@nakh_Home, I don’t think you can do this without a Rule. But we might be able to get this to work.

The big thing is that we cannot send ON or OFF as a command to a String Item. But we can send "ON" and "OFF" as a command to a Switch Item. And it does look like you tried to take that into account, at least on your sitemap. But I don’t think there is a way to aggregate the states of the two different types of Items on the one Group because the String will have state “ON” and the Switch ON and “ON” != ON.

In order to handle this I think you will need to create a Proxy Item (make it a Switch) that you send the command to and then a Rule to forward the proper version of the command to the members.

You will probably also want a Rule to update the Proxy Switch when any members of the Group change.

rule "Light's proxy received a command"
when
    Item AllLightsProxy received command
then
    glight.members.forEach[ light | light.sendCommand(receivedCommand.toString) ]
end

rule "Update AllLightsProxy"
when
    Member of glight changed
then
    AllLightsProxy.postUpdate(glight.members.map[ state.toString ].reduce[ result, st | if(result == "ON" || st == "ON") "ON" else "OFF" ])
end

See Design Pattern: Working with Groups in Rules for an explanation of the list operations performed above.

The first Rule loops through the members of glight and sends the received command sent to the proxy Item as a command in String format. In the case of Rules, the String will be parsed and converted to OnOffType for you. This is the part that is failing when you just send the command to the Group.

The second Rule triggers when any member of glight changes and loops through all the members of glight and updates the Proxy Item if any one of them is ON or “ON”.