Simultaneous action of 3 switches

I’m helping a potential OH user that has 3x of these switches Moes ZTS-EU_1gang control via MQTT | Zigbee2MQTT

He wants to operate the switches as follows:

  1. If any switch is operated to become off, then all go off
  2. If any switch is operated to become on, then all go on

I think this can be done using “follow me” profiles, but I’m not 100% sure of the setup.

Or is it better to use rules in order to avoid loops ?

Or maybe a profile to operate switch 2 depending on switch 1, another profile to operate switch 3 depending on switch 2, and a rule to operate (sendCommand) switch 1 depending on switch 3 (to break loops)

Thanks

No, a rule will be needed for this. The problem is that the follow profile only goes one way. For example, if Channel B follows Channel A, updates from Channel A will flow to Channel B as commands. So far so good. However, updates from Channel B do not flow to Channel A as commands.

The follow profile only works if one Channel always follows another but never the other way around.

For this case you’ll need to use a rule.

Assuming this is even possible, which I’m pretty sure it’s not, it will be less complicated using Rules.

Put all the Switch Items into a Group. Trigger the rule with a Member of changed trigger. Inside the rule, command any member of the Group that isn’t already the state that was changed to. The rule will trigger N-1 for N switches but only the first trigger will have the rule do anything and by triggering on changes you will avoid loops.

Let me see if I understand

  1. one rule that fires when any of the 3 switches goes to ON and sends Command ON to the other two
  2. Another rule that fires when any of the 3 switches goes to OFF and sends Command OFF to the other two

No, just one rule is needed. You don’t care whether it changes to ON or OFF, just that it changed.

In Rules DSL it’d look something like this:

rule "Sync switches"
when
    Member of SwitchGroup changed
then
    SwitchGroup.members
               .filter[ item | item.state != newState ]
               .forEach[ item | item.sendCommand(newState) ])
end

In JS in the UI the action script would look something like this:

items.SwitchGroup.members
                 .filter(item => item.state != event.itemState.toString())
                 .forEach(item => item.sendCommand(event.itemState.toString());

In Blockly:

It’s really just a one liner. But the subtlety that makes it work is in the rule trigger and that you only send a command to Items that are not already in the same state. If you used received command or received update or just send the command to all the Items it wouldn’t work and you’s likely get into a loop or at a minimum trigger the rule a bunch more times than necessary.

2 Likes

In openHAB 4, there’s also triggeringGroup which is handy in this situation

rule "Sync switches"
when
    Member of SwitchGroup changed
then
    triggeringGroup.members
                   .filter[ item | item.state != newState ]
                   .forEach[ item | item.sendCommand(newState) ])
end

in JRuby, triggeringGroup is available as event.group

rule "Sync Switches" do
  changed SwitchGroup.members
  run do |event|
    event.group.members.ensure.command(event.state)
  end
end

Or in one line:

changed(SwitchGroup.members) { |event| event.group.members.ensure.command(event.state) }

It’s very useful when you have multiple groups, so you can use one rule

rule "Sync Switches" do
  changed SwitchGroup1.members
  changed HallwayGroup.members
  changed LivingRoomGroup.members
  run do |event|
    event.group.members.ensure.command(event.state)
  end
end

Or you can also write it as:

rule "Sync Switches" do
  changed SwitchGroup1.members, HallwayGroup.members, LivingRoomGroup.members
  run do |event|
    event.group.members.ensure.command(event.state)
  end
end
1 Like

Thanks. Any aggregation rule for SwitchGroup ? Or it is irrelevant ?

What do you mean?

Sorry, aggregation function of the group item

imagem

Oh the group function. No, the group function is irrelevant. The group function is only used to update the group's state. In the rules above, we’re dealing directly with the group’s members, and not using/checking the group’s state at all. So you don’t need to have any group function and just have a plain simple group.

1 Like