Problem with group of switches

Hi,

System: HassOS 2.12
Deployment: production
Home Assistant 0.101.2

Problem with automatically switching off a group of switches

TL;DR; Only the first switch in a group is switched off.

This problem only occours when trying to switch off a group of switches using the

service: homeassistant.turn_off

method. I have a group group.garage_lights which is defined as:

garage_lights:
  icon: mdi:lightbulb
  name: Garage
  entities:
    - switch.sonoff_1000000000_1
    - switch.sonoff_1000000000_2
    - switch.sonoff_1000000000_3

There is an automation to turn on these lights when my wife comes home:

- id: garage-lights-on-when-gail-comes-home
  alias: "Garage lights on when Jane comes home"
  initial_state: true
  trigger:
    platform: state
    entity_id: person.jane_doe
    to: 'home'
  condition:
    condition: state
    entity_id: group.garage_lights
    state: 'off'
  action:
    - service: timer.start
      entity_id: timer.garage_lights_on_switch_bounce
    - service: homeassistant.turn_on
      entity_id: group.garage_lights

The switch bounce timer was implemented because I noticed at times (when the external service controlling the switches was busy) the lights would fail to turn on. The timeout on updating the switch status is 10 seconds, so the bounce timer is set to 12 seconds and implemented like this:

- id: garage_lights-on-bounce-timer-finish
  alias: "Check for garage lights on switch bounce"
  initial_state: true
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.garage_lights_on_switch_bounce
  condition:
    condition: state
    entity_id: group.garage_lights
    state: 'off'
  action:
    - service: timer.start
      entity_id: timer.garage_lights_on_switch_bounce
    - service: homeassistant.turn_on
      entity_id: group.garage_lights

So, if the lights are off when the bounce timer finished, the timer is started again and the lights turned on. This all works perfectly.

So, when the garage lights are turned on a 5 min timer is started to turn them off.

- id: garage-lights-on
  alias: "Start timer when the garage lights are turned on"
  initial_state: true
  trigger:
    platform: state
    entity_id: group.garage_lights
    to: 'on'
  condition:
    condition: state
    entity_id: timer.garage_lights_timer
    state: 'idle'
  action:
    service: timer.start
    entity_id: timer.garage_lights_timer

And when this timer finishes…

- id: garage-lights-on-timer-finish
  alias: "Garage light timer finished"
  initial_state: true
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.garage_lights_timer
  condition:
    condition: state
    entity_id: group.garage_lights
    state: 'on'
  action:
    - service: timer.start
      entity_id: timer.garage_lights_off_switch_bounce
    - service: homeassistant.turn_off
      entity_id: group.garage_lights

and the corresponding off bounce timer:

- id: garage_lights-off-bounce-timer-finish
  alias: "Check for garage lights off switch bounce"
  initial_state: true
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.garage_lights_off_switch_bounce
  condition:
    condition: state
    entity_id: group.garage_lights
    state: 'on'
  action:
    - service: timer.start
      entity_id: timer.garage_lights_off_switch_bounce
    - service: homeassistant.turn_off
      entity_id: group.garage_lights

Here is where the problem lies…,

  • Lights are turned on, either manually or via automation, no problem
  • 5 min Timer starts
  • 5 min timer finishes
  • Only the first light in the group is turned off, however according to HA all three lights are off, and so is the group
  • So, at this point 2 lights in the garage are still on, but HA says they are all off.
  • After 1 minute the status of two of the garage lights changes back to on (they have been on the whole time)
  • So, the 5 minute time restarts
  • 5 minute timer finishes, switches off the group. Again exactly the same thing happens. The state of all three lights and the group is set to off, however the last two lights in the group remain on.
  • This sequence then repeats forever until the lights are turned off manually either in the HA using the Lovelace interface, eWeLink app or physically using the actual switch (shocker…)

Suggestions ?

Update, this also does not work, only the first switch in the list in switched off:

- id: garage-lights-on-timer-finish
  alias: "Garage light timer finished"
  initial_state: true
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.garage_lights_timer
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: switch.sonoff_1000524960_1
        state: 'on'
      - condition: state
        entity_id: switch.sonoff_1000524960_2
        state: 'on'
      - condition: state
        entity_id: switch.sonoff_1000524960_3
        state: 'on'
  action:
    - service: timer.start
      entity_id: timer.garage_lights_off_switch_bounce
    - service: switch.turn_off
      entity_id:
        - switch.sonoff_1000524960_1
        - switch.sonoff_1000524960_2
        - switch.sonoff_1000524960_3

- id: garage_lights-off-bounce-timer-finish
  alias: "Check for garage lights off switch bounce"
  initial_state: true
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.garage_lights_off_switch_bounce
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: switch.sonoff_1000524960_1
        state: 'on'
      - condition: state
        entity_id: switch.sonoff_1000524960_2
        state: 'on'
      - condition: state
        entity_id: switch.sonoff_1000524960_3
        state: 'on'
  action:
    - service: timer.start
      entity_id: timer.garage_lights_off_switch_bounce
    - service: switch.turn_off
      entity_id:
        - switch.sonoff_1000524960_1
        - switch.sonoff_1000524960_2
        - switch.sonoff_1000524960_3

Back to the drawing board…

OK, another variation that also does not work…

- id: garage-lights-on-timer-finish
  alias: "Garage light timer finished"
  initial_state: true
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.garage_lights_timer
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: switch.sonoff_1000524960_1
        state: 'on'
      - condition: state
        entity_id: switch.sonoff_1000524960_2
        state: 'on'
      - condition: state
        entity_id: switch.sonoff_1000524960_3
        state: 'on'
  action:
    - service: timer.start
      entity_id: timer.garage_lights_off_switch_bounce
    - service: switch.turn_off
      entity_id: switch.sonoff_1000524960_1
    - service: switch.turn_off
      entity_id: switch.sonoff_1000524960_2
    - service: switch.turn_off
      entity_id: switch.sonoff_1000524960_3

- id: garage_lights-off-bounce-timer-finish
  alias: "Check for garage lights off switch bounce"
  initial_state: true
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.garage_lights_off_switch_bounce
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: switch.sonoff_1000524960_1
        state: 'on'
      - condition: state
        entity_id: switch.sonoff_1000524960_2
        state: 'on'
      - condition: state
        entity_id: switch.sonoff_1000524960_3
        state: 'on'
  action:
    - service: timer.start
      entity_id: timer.garage_lights_off_switch_bounce
    - service: switch.turn_off
      entity_id: switch.sonoff_1000524960_1
    - service: switch.turn_off
      entity_id: switch.sonoff_1000524960_2
    - service: switch.turn_off
      entity_id: switch.sonoff_1000524960_3

And the insane solution is…

- id: garage-lights-on-timer-finish
  alias: "Garage light timer finished"
  initial_state: true
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.garage_lights_timer
  action:
    - service: timer.start
      entity_id: timer.garage_lights_off_switch_bounce
    - service: timer.start
      entity_id: timer.garage_lights_off_timer_1
    - service: switch.turn_off
      entity_id: switch.sonoff_1000524960_1

- id: garage_lights-off-timer-1-finish
  alias: "Garage Lights timer 1 finished"
  initial_state: true
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.garage_lights_off_timer_1
  action:
    - service: timer.start
      entity_id: timer.garage_lights_off_timer_2
    - service: switch.turn_off
      entity_id: switch.sonoff_1000524960_2

- id: garage_lights-off-timer-2-finish
  alias: "Garage Lights timer 2 finished"
  initial_state: true
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.garage_lights_off_timer_2
  action:
    - service: switch.turn_off
      entity_id: switch.sonoff_1000524960_3

@tescophil long post i might say, couldn’t quite read/comprehend it all but long story short: you cannot switch multiple sonoff devices in the same time (the app API literally doesn’t support it or i couldn’t figure it out at least ¯\_(ツ)_/¯ if anyone feels brave enough to find the solution i’ll merge the fix right away!)
meanwhile i strongly suggest to take a look here
ps: fyi won’t fix this issue anytime soon as i don’t find it a huge blocker

Interesting, this has been working fine for the last 6 months, and indeed switching on multiple devices works perfectly, it’s just switching off that doesn’t.

@tescophil try this technique I found someone else was using…and borrowed it.
Works flawlessly for my needs…maybe right for you as well?
PS: put the value you want in the payload: ’ ’

  action:
  - data:
      payload: ''
      topic: cmnd/sonoff20/Power1
    service: mqtt.publish
  - data:
      payload: ''
      topic: cmnd/sonoff20/Power2
    service: mqtt.publish 
  - data:
      payload: ''
      topic: cmnd/sonoff19/Power2
    service: mqtt.publish