[SOLVED] Lighting rule based on group switch buggy

Hey guys, I made a simple rule based on a group switch state on/off to switch on/off my 2 bulbs. This is the rule:

when
    Item AlleLichten changed from OFF to ON
then
    sendCommand(KeukenLamp, ON)
    sendCommand(WoonkamerLamp, ON)
end

rule "Licht uit"
when
    Item AlleLichten changed from ON to OFF
then
    sendCommand(KeukenLamp, OFF)
    sendCommand(WoonkamerLamp, OFF)
end

items file:

Group:Switch:OR(ON, OFF) AlleLichten "Alle lampies" ["Switchable"]

Switch WoonkamerLamp "WoonkamerLamp" (AlleLichten) ["Lighting"] {channel="milight:rgbwwLed:F0FE6B3BD6A2:1:ledbrightness"}

Switch KeukenLamp "KeukenLamp" (AlleLichten) ["Lighting"] {channel="milight:rgbwwLed:F0FE6B3BD6A2:2:ledbrightness"}

Now when I hit the switch in ClassicUI or through Google Assistant, I see the switches hop on and off again rapidly en randomly stops with one light on and one off. When I hit it a few times they all go off or on. These are the logs:

2019-02-28 11:27:48.863 [GroupItemStateChangedEvent] - AlleLichten changed from OFF to ON through KeukenLamp

2019-02-28 11:27:48.871 [vent.ItemStateChangedEvent] - KeukenLamp changed from OFF to ON

2019-02-28 11:27:48.878 [ome.event.ItemCommandEvent] - Item ‘KeukenLamp’ received command OFF

2019-02-28 11:27:48.887 [ome.event.ItemCommandEvent] - Item ‘KeukenLamp’ received command ON

2019-02-28 11:27:48.896 [ome.event.ItemCommandEvent] - Item ‘WoonkamerLamp’ received command ON

2019-02-28 11:27:48.899 [ome.event.ItemCommandEvent] - Item ‘WoonkamerLamp’ received command OFF

2019-02-28 11:27:48.903 [vent.ItemStateChangedEvent] - WoonkamerLamp changed from OFF to ON

2019-02-28 11:27:48.906 [nt.ItemStatePredictedEvent] - KeukenLamp predicted to become OFF

2019-02-28 11:27:48.909 [nt.ItemStatePredictedEvent] - KeukenLamp predicted to become ON

2019-02-28 11:27:48.914 [vent.ItemStateChangedEvent] - KeukenLamp changed from ON to OFF

2019-02-28 11:27:48.917 [nt.ItemStatePredictedEvent] - WoonkamerLamp predicted to become ON

2019-02-28 11:27:48.919 [nt.ItemStatePredictedEvent] - WoonkamerLamp predicted to become OFF

2019-02-28 11:27:48.922 [vent.ItemStateChangedEvent] - KeukenLamp changed from OFF to ON

2019-02-28 11:27:48.924 [vent.ItemStateChangedEvent] - WoonkamerLamp changed from ON to OFF

This is how the UI looks:

When you send a command to a Group type Item by operating the switch on your UI, the group’s state remains unchanged and the command gets passed to all group members.

The group’s state is calculated from the state of its members, that’s the OR(ON,OFF) part in your case. Every time a member changes, state is recalculated.

Obviously, sending a command to group members might change their state and change the group state and so trigger one or other of your rules, depending just how things are at that moment, which issues more commands to your members which might trigger group changes which …

What you really want to do is delete your rules, and rely on the built-in group handling of commands.

Hey Ross thanks for reaching out to me. What exactly do you mean by relying on the built-in group? I tried several things but I just can’t get a simple switch to work on turning on/off 2 bulbs. I tried it with a group switch and 2 switch items (bulbs). But when I changed it’s state nothing happened. So I thought, the switch is only a state, you need to attach some rules to it so it knows what to do when a state is active.

Is there a straight on simple way to accomplish this? I just want 1 switch to turn on/off my 2 bulbs in ClassicUI and by Voice through Google Assistant.

I think there is a failing to appreciate that command is not state.

You can for example command a dimmer to increase, and expect its state to go from 80% to 85%.

A Group Item’s state is based on its members. Attempting to change a Group Items’ state is therefore pointless.

You can however send a command to a Group, and it will distribute that command to all members.

When you click a widget on a UI, it sends a command to the selected Item/Group.

This isn’t clear. A wall switch? Or a switch in your UI?

Let’s go with the easier UI version for now.

Put your two light switch Items in a Group.
Put your Group on your sitemap as a Switch.
You’ve already done this. That’s it, no rules needed.
When you operate the UI switch it will generate a command which will get passed out via the group to the member lights.

I’m not familiar with milights but I assume your individual Items actually respond to ON/OFF commands.

That really is it. Try it.
When you’re happy with that, we can look at the case where one light is already on and one already off.

1 Like

Blockquote
A Group Item’s state is based on its members. Attempting to change a Group Items’ state is therefore pointless.

Thanks for clearing that out, I got it.

Blockquote
This isn’t clear. A wall switch? Or a switch in your UI?

I use a switch in the UI indeed. Now I deleted the rules and tried to swtich it in ClassicUI. It works now out of the box. Only little strange thing is that the seperate switches for the bulbs each ain’t switching on/off.
So when I hit the group switch, one of the 2 switches for the bulbs are reacting and one is not. But the bulbs it’s lights are working ok, so this is still a UI issue.

Yes. UI refresh is imperfect. Keep a close eye on openhab.log though - I recently discovered that minor sitemap errors can allow the appearance of working but mess up refresh.

EDIT - notice your two lights already display differently. I think you might improve refresh by using [%s] format in labels, or [] if you want no state display.

There is also a use issue with group switch controls. The switch widget won’t let you send ON if the switch is already ON. (That’s about the widget, not the group.)
That’s a problem if your Group state is ON (because just one of it’s members is already on) and you want all-on.
Of course, you can dab off and then on and that is acceptable for some people.

An alternative is to use mapping in the sitemap switch widget to create two buttons, call them all-on and all-off.