Automating 2 physical switches driving 2 lights so that they "act as one"

Hi,

I am newish to HA, have been playing with it for 1 month automating various parts of my home but I did not dabble yet in YAML and used mostly the graphical mode.

For one automation, I have been trying a lot of options, browsing all forums for possible solution but cannot seem to find a reliable one for this automation so posting for your help and insights.

In my bathroom, I have 2 lights (mirror and ceiling) each with their own physical switch (at different place of the bathroom) and I connected each of these switches to a Sonoff ZBMINIL2, with the idea of lighting up or switching off both lights, whatever button/switch I use.

It works with 2 simple automations and it is ok in 80-90% of cases:

  • Automation 1: if mirror light switch OR ceiling light switch is turned ON, then turn ON ceiling light + mirror light
  • Automation 2: if mirror light switch OR ceiling light switch is turned OFF, then turn OFF ceiling light + mirror light

The issue I often encounter is that, due to automation & Zigbee connectivity latency, a few fast switching action on 1 switch OR fast opposite actions on the 2 switches, leads to both automation going in a mad loop and my lights blinking indefinitely and only to stop it is to disable the 2 automations in HA…

I already tried to:

  • Separate each button’s automation (ending up with 4 automation instead of 2) so that for example, the mirror switch trigger only acts on the ceiling light, since the mirror light already had the physical command (by the physical switch) hence is already on
  • Have a single automation so use toggle condition in the trigger (instead of on/off) and toggle in the lights action (but automation fails as the physically connected light is already in the desired state so it does not work at all)
  • Add a “and if” condition that checks the state of the other automation (to make sure the called up automation only starts if the other one has finished to run
  • Same as above but added a “state time length” after the automation
  • Different automation mode (single, restart, etc…)
    etc…

I did not try to group the ZBMINIL2 as I do not think it will help (as each drive a separate light).

So far, nothing does it and I still get these glitches…

See below my more advanced automation (OFF variant) with the “running automation” status check.

Any idea on how to solve it easily?

alias: Upstairs - Bathroom Kids - Light Automation - Off
description: ""
trigger:
  - platform: device
    type: turned_off
    device_id: dea48dbcb359d295f99756cbc6924aa9
    entity_id: 2706bec097a6f6ac866ea92c2dd7d062
    domain: switch
  - platform: device
    type: turned_off
    device_id: c6ed975e319c7b181a32666dec280b1e
    entity_id: 261371dec1fdc51e20329124fcd6b695
    domain: switch
condition:
  - condition: state
    entity_id: automation.upstairs_bathroom_kids_light_automation_on
    attribute: current
    state: 0
    for:
      hours: 0
      minutes: 0
      seconds: 5
action:
  - type: turn_off
    device_id: dea48dbcb359d295f99756cbc6924aa9
    entity_id: 2706bec097a6f6ac866ea92c2dd7d062
    domain: switch
  - type: turn_off
    device_id: c6ed975e319c7b181a32666dec280b1e
    entity_id: 261371dec1fdc51e20329124fcd6b695
    domain: switch
mode: restart

First, general tip:

second :

  - delay: 00:00:01

as last action of the automation might help.

Thanks a lot for these tips… I will give it a try and come back to you.
Do you recommend I keep the “and if” check to see if the other automation is still running or is that no longer necessary now with this delay?

I would remove it. And set mode to single.

1 Like

Here is an automation that I use in owner bath to turn on and off shower light when I turn on sconce lighting. The switches are in two different locations. I use one automation to turn on and off the light. I will explain my logic: if the sconce switch goes for off to on, sconce is on, so turn on shower light. If sconce switch goes from on to off, sconce lighting is off so turn off shower light. thus the two triggers and two actions based on conditions. Hope this helps.

- id: 'shower switch on off'
  alias: Shower - Turn on-off Light when owner bath light turned on/off
  description: ''
  trigger:
  - platform: state
    entity_id: switch.owner_bath_sconces
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: switch.owner_bath_sconces
    from: 'on'
    to: 'off'
  condition: []
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: switch.owner_bath_sconces
        state: 'on'
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.shower_light
    - conditions:
      - condition: state
        entity_id: switch.owner_bath_sconces
        state: 'off'
      sequence:
      - service: switch.turn_off
        target:
          entity_id: switch.shower_light
    default: []
  mode: single

Tried it and while it helped (I get less issues), the ping-ponging between the 2 automations is still happening (tried also with longer values) from time to time…

tks a lot for that… not being an expert, I am wondering if I would apply this approach to my situation (2 physical light switches driving its own light), it would become a lot more complex?

In effect, I would have to check the states of 2 switches and take multiple actions depending on to 4 possible states of the 2 switches.

Which light is the one you turn on as you walk into the Bathroom? I would expect it to be the ceiling light. If you want them to work as a pair forget about the mirror light and use the ceiling light to turn on and off the mirror light. The ceiling light will always be controlled by the switch. You can also group the lights and use the group to turn on/off all the lights.

Here is the documentation on groups: Group - Home Assistant

Thanks for this idea and indeed I had thought about removing the 2nd switch completly and put a blanking plate, but unfortuantely, this ensuite bathroom is very small (2x3m) so while the logical switch is the ceiling one at the entrance of the bathroom, we often enter the bathroom, forget to press the entrance switch and press the mirror one instead (hence the idea to group and automate them).

Have you thought about a motion sensor and then you can do something like this. I use Shelly motion sensors, but there are ZigBee motion sensors.

- id: 'Bar Lights On and Off based on motion and switch'
  alias: Bar Lights - Turn on and off based on stairwell light.
  description: Turn on and off all the lights in the bar area
  trigger:
    - id: 'bar_on'
      platform: state
      entity_id:       
      - binary_sensor.bar_stairwell_motion_sensor_motion
      - binary_sensor.bar_main_motion_sensor_motion
      - binary_sensor.bar_ne_corner_motion
      - binary_sensor.shellymotionsensor_60a42395fc6a_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: switch.stairwell_light
      from: 'off'
      to: 'on'
    - id: 'bar_off'
      platform: state
      entity_id: 
      - binary_sensor.bar_stairwell_motion_sensor_motion
      - binary_sensor.bar_main_motion_sensor_motion
      - binary_sensor.bar_ne_corner_motion
      - binary_sensor.shellymotionsensor_60a42395fc6a_motion
      from: 'on'
      to: 'off'
      for: '00:02:30'
    - id: 'bar_off'
      platform: state
      entity_id: switch.stairwell_light
      from: 'on'
      to: 'off'
  condition: []
  action:
  - choose:
    - conditions:
        - "{{ trigger.id == 'bar_on' }}"
      sequence:
        - service: switch.turn_on
          target:
            entity_id:
            - switch.stairwell_light
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.area_cans
        - service: light.turn_on
          target:
            entity_id:
            - light.bar_pendant_light
            - light.bar_back_bar_cans
          data:
            brightness_pct: 50
    - conditions:
        - "{{ trigger.id == 'bar_off' }}"
      sequence:
        - service: switch.turn_off
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_off
          target:
            entity_id:
            - light.bar_back_bar_cans
            - light.bar_pendant_light
    default: []
  mode: queued

Yes, that is a possibility but I would like to avoid buying one more element (in fact 2 as I have 2 bathrooms with the same config) to circonvene a problem that should be able to be solved with a programming trick OR better algorythm.

To give it one more go, I tried to reverse engineer your program (I am not a coder :slight_smile:) and applied it to my needs. After many trials and erros, I managed to make it work, so I am very happy about that!!

Unfortuantely, it seems that it is has the exact same issue as before. Now that I understand it… it is pretty much the same concept of a “dual automation” but packaged differently (using a variable), so the two automation actions (on and off) can ping pong each other if one switch is pressed twice/three times too fast OR one switch is pressed just after was pressed before…

Any other ideas?

alias: NN BATHROOM KIDS AUTOMATION
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.upstairs_bathroom_kids_light_ceiling
      - switch.upstairs_bathroom_kids_light_mirror
    from: "off"
    to: "on"
    id: Bathroom_on
  - platform: state
    entity_id:
      - switch.upstairs_bathroom_kids_light_ceiling
      - switch.upstairs_bathroom_kids_light_mirror
    from: "on"
    to: "off"
    id: Bathroom_off
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.id == 'Bathroom_on' }}"
        sequence:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              device_id:
                - dea48dbcb359d295f99756cbc6924aa9
                - c6ed975e319c7b181a32666dec280b1e
      - conditions:
          - condition: template
            value_template: "{{ trigger.id == 'Bathroom_off' }}"
        sequence:
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              device_id:
                - dea48dbcb359d295f99756cbc6924aa9
                - c6ed975e319c7b181a32666dec280b1e
mode: single

I have one more - separate the triggers so they only operate on one entity. You will have two on and two off trigger . Make the new trigger ids Bathroom_on1 and and bathroom off1. Then only turn on/off the other light so you will have 4 actions 2 on and 2 off.

Thanks a lot for your help and additional suggestion.

After posting my last message, I tried one more thing using the new program (where the on/off actions are in the same automation) but leveraging the earlier suggestion from FrancisP (adding a delay).

Adding the delay on the On and Off individual actions (at the end) still did not work (ping pong effect was still there), but then I added it as an extra action after the on / off actions (as a separate action) and it worked!!!

I guess this delay, as an extra action after both on/off action branches, prevents the automation to re-start (also changed the automation mode to single). This would not have been possible on the previous approach (2 separate automations: on / off) as the 2 programs would bounce off each other, but, with the on/off automation now in the same program, that was no longer possible.

I am very happy and want to thank you all for your quick and very kind support!

I am pasting here my code (I still need to fix my entity ID issue => next step) + flow diagram, if it can help some others who may be in the same situation as I was:

image

alias: Upstairs - Bathroom Kids - Light Automation
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.upstairs_bathroom_kids_light_ceiling
      - switch.upstairs_bathroom_kids_light_mirror
    from: "off"
    to: "on"
    id: Bathroom_kids_light_on
  - platform: state
    entity_id:
      - switch.upstairs_bathroom_kids_light_ceiling
      - switch.upstairs_bathroom_kids_light_mirror
    from: "on"
    to: "off"
    id: Bathroom_kids_light_off
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.id == 'Bathroom_kids_light_on' }}"
        sequence:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id:
                - switch.upstairs_bathroom_kids_light_ceiling
                - switch.upstairs_bathroom_kids_light_mirror
      - conditions:
          - condition: template
            value_template: "{{ trigger.id == 'Bathroom_kids_light_off' }}"
        sequence:
          - service: switch.turn_off
            metadata: {}
            data: {}
            target:
              entity_id:
                - switch.upstairs_bathroom_kids_ceiling_light
                - switch.upstairs_bathroom_kids_mirror_light
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
mode: single

You should check if you can put that one in detached mode.
When in detached mode you can use a very simple automation (by toggling the light)

Tks, when you mean detached, you mean that I no longer “connect” the physical switch to the ZBMINIL2 switch input but use a Zigbee remote instead?

Yes
But I think you misunderstood. You can actually configure the device to detach the switch from the relay. This is called detached mode.
I am only not 100% sure it supports it (shelly does for sure)
But then it would look like this:

alias: "Upstairs - Bathroom Kids - Light Automation"
description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.upstairs_bathroom_kids_detached_light_switch_ceiling
      - binary_upstairs_bathroom_kids_detached_light_mirror
    from: ["off", "on"]
    to: ["off", "on"]
condition: []
action:
  - service: switch.toggle
    metadata: {}
    data: {}
    target:
      entity_id:
        - switch.upstairs_bathroom_kids_light_ceiling
        - switch.upstairs_bathroom_kids_light_mirror

Very interesting (I did not know) and I would have another automation case where that would be useful… I will dig into that to see if Sonoff ZBMINL2 supports this mode.

1 Like