Turn off switch affected by another automation

I’m an HA Noob, and would really appreciate help with the following automations.
They work as expected other than the last switch (switch.sonoff_100067cf36) not turning off when the “- id: goodnight_lights_off” automation triggers.

I have a feeling it has something to do with the “- id: motion_outside_lights_on” automation possibly keeping the status of the switch high, but I don’t know why.

###############################################################
## Toggle bedtime status
###############################################################

- id: bedtime_status_on
  alias: Turn on bedtime status at bedtime setpoint
  trigger:
  - platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.bedtime.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
      
  action:
  - service: homeassistant.turn_on
    entity_id: 
      - input_boolean.bedtime_status
            
- id: bedtime_status_off
  alias: Turn off bedtime status at sunrise
  trigger:
  - platform: sun
    event: sunrise
      
  action:
  - service: homeassistant.turn_off
    entity_id: 
      - input_boolean.bedtime_status
      
###############################################################
## Lights on at Sunset and off at bedtime
###############################################################

- id: sunset_lights_on
  alias: Turn on lights at sunset
  trigger:
  - platform: sun
    event: sunset
    offset: "-01:00:00"                        #1hour before sunset
  
  action:
  - service: homeassistant.turn_on
    entity_id: 
      - switch.81358500cc50e3cebafd            #Study Lamp
      - switch.sonoff_1000641867               #Garden Room Lamp
      - switch.81358500cc50e3ce7ef0            #Utility Lamp
      - switch.sonoff_100067cf36               #Outside Lights
          
- id: goodnight_lights_off
  alias: Turn off lights when input_boolean.bedtime turns on
  trigger:
  - platform: state
    entity_id: input_boolean.bedtime_status
    to: 'on'
    
  action:
  - service: homeassistant.turn_off
    entity_id: 
      - switch.81358500cc50e3cebafd            #Study Lamp
      - switch.sonoff_1000641867               #Garden Room Lamp
      - switch.81358500cc50e3ce7ef0            #Utility Lamp
      - switch.sonoff_100067cf36               #Outside Lights
   
###############################################################
## Outside Lights on motion
###############################################################
  
- id: motion_outside_lights_on
  alias: Turn on outside lights when the courtyard or cartlodge doorbell sense motion
  trigger:
  - platform: state
    entity_id: binary_sensor.ring_courtyard_motion, binary_sensor.ring_cartlodge_motion
    to: 'on'

  condition:
    - condition: state
      entity_id: input_boolean.bedtime_status
      state: 'on'

  action:
  - service: homeassistant.turn_on
    entity_id: switch.sonoff_100067cf36
    
  - service: timer.start
    entity_id: timer.outside_lights_trigger
    
- id: motion_outside_lights_off
  alias: Turn off outside lights 10 minutes after trigger
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.outside_lights_trigger
      
  condition:
    - condition: state
      entity_id: input_boolean.bedtime_status
      state: 'on'
    
  action:
  - service: homeassistant.turn_off
    entity_id: switch.sonoff_100067cf36

In order to help you, you will need to format the configuration you posted so that we can easily read it.

Put three consecutive back-ticks ``` on their own line.
Paste your configuration below the line with three back-ticks.
Put three consecutive back-ticks ``` on their own line below whatever you just pasted.

Sorry, complete novice.

If you trigger the automation from the services page, does the light turn off?

I just gave that a go, and Yes it does turn off when triggered from services

The condition spacing seems off, so I’m not 100% sure that would be the issue. How sensitive are the motion sensors? Could a tree be close enough that it would turn on the sensor?

The sensors are motion detection on 2x Ring doorbells, so are very stable.

I think I will replace the sunset tigger with an input_boolean to make testing the automations easier. I will come back to you.

Many thanks for your help.

automations don’t work like that. they only have any effect on devices at the moment they are told to run. If the automation turns on a light and one second later another automation turns the light off it will turn off regardless of the state of the first automation. The only time the first automation will effect the light again is if that automation re-triggers and only then will it turn the light back on.

Does the last light never turn off with that automation or is it just occassionally?

It is intermittent for both the on script and the off script, but it is always the last device in the list which is affected.

i.e These always turn on or off

 - switch.81358500cc50e3cebafd            #Study Lamp
 - switch.sonoff_1000641867               #Garden Room Lamp
 - switch.81358500cc50e3ce7ef0            #Utility Lamp

but the Outside Lights device turns on or off intermittently

- switch.sonoff_100067cf36               #Outside Lights

Should I try making the Outside Lights device the first to switch, and see if the intermittent nature moves with it?

You could try that and it could help to narrow down any connection issues.

However, I have heard of and seen once or twice myself that sometimes when operating a bunch of devices at once that HA gets “confused” and fails to switch them all. Usually it was only when operating around 10 or more things in my case tho.

The HA confusion is a bit disappointing. I will try changing the order and report back.

Do you have suggestions for a work around if it turns out to be a HA bug? Would you suggest creating multiple automations and split the switching of the devices?

if it comes down to that then you could list everything as it is in your automation right now then add a one second delay then call the same turn_off service with the devices in the reverse order as the first section. it’s kind of clunky but it will probably catch everything then.

I would be really surprised if HA choked on only four items in the list so I would definitely try to find another possible issue before it was determined to be HA. Maybe it’s just a network issue? Or a sonoff connection problem?

It might be worth trying this, put the switches in a group and switch the group.

Many thanks. I’ll try that too, and feedback.

I have tried creating a group in groups.yaml and switching the group, but the switching of the “Outside Lights” is still erratic.

I will try using a different switch in “outside lights” automation to see if that changes anything, and feedback.

Rather than listing the entity ids under the service try making a list of service calls, like this:

  action:
  - service: homeassistant.turn_on
    entity_id: switch.81358500cc50e3cebafd            #Study Lamp
  - service: homeassistant.turn_on
    entity_id: switch.sonoff_1000641867               #Garden Room Lamp
  - service: homeassistant.turn_on
    entity_id: switch.81358500cc50e3ce7ef0            #Utility Lamp
  - service: homeassistant.turn_on
    entity_id: switch.sonoff_100067cf36               #Outside Lights

Thanks very much Tom. That seems to be the answer. I have also added a delay between each call.

action:
  - service: homeassistant.turn_on
    entity_id: switch.sonoff_100067cf36               #Outside Lights
  - delay: '00:00:01'
  - service: homeassistant.turn_on
    entity_id: switch.81358500cc50e3cebafd            #Study Lamp
  - delay: '00:00:01'
  - service: homeassistant.turn_on
    entity_id: switch.sonoff_1000641867               #Garden Room Lamp
  - delay: '00:00:01'
  - service: homeassistant.turn_on
    entity_id: switch.81358500cc50e3ce7ef0            #Utility Lamp
  - delay: '00:00:01'
  - service: homeassistant.turn_on
    entity_id: switch.34455053cc50e3d64cf6            #Lounge Window Lamp
  - delay: '00:00:01'
  - service: homeassistant.turn_on
    entity_id: switch.53303740cc50e310bc75            #Lounge Door Lamp

These devices either use the sonoff or tuya integration via the cloud, so I was probably overloading the interface by switching them all at once.

Thank you everyone for your help.

Just for fun, try using Scenes to control the switches and see if it cures the problem of the lone straggler.

Your automations will use the scene.turn_on service to activate either scene.switches_on or scene.switches_off.

- id: sunset_lights_on
  alias: Turn on lights at sunset
  trigger:
  - platform: sun
    event: sunset
    offset: "-01:00:00"
  action:
  - service: scene.turn_on
    entity_id: scene.switches_on
          
- id: goodnight_lights_off
  alias: Turn off lights when input_boolean.bedtime turns on
  trigger:
  - platform: state
    entity_id: input_boolean.bedtime_status
    to: 'on'
  action:
  - service: scene.turn_on
    entity_id: scene.switches_off

Here are the two scenes:

scene:
  - name: switches_on
    entities:
      switch.sonoff_100067cf36: on
      switch.81358500cc50e3cebafd: on
      switch.sonoff_1000641867: on
      switch.81358500cc50e3ce7ef0: on
      switch.34455053cc50e3d64cf6: on
      switch.53303740cc50e310bc75: on
  - name: switches_off
    entities:
      switch.sonoff_100067cf36: off
      switch.81358500cc50e3cebafd: off
      switch.sonoff_1000641867: off
      switch.81358500cc50e3ce7ef0: off
      switch.34455053cc50e3d64cf6: off
      switch.53303740cc50e310bc75: off

I’m not sure how HA structurally (right word?) handles scenes compared to just regular switches or the generic “homeassistant.x” service call so I’m really just interested to know…

Is there something in the way HA handles things that would/should make any difference?

Ditto. The OP has discovered a strange corner-case where the usual approach (calling the service with a list of entities) seems to hiccup and omit the final entity. Let’s turn lemons into lemonade and use this odd situation for testing other ways of turning on/off multiple switches. What other approaches will work (or will not)?

Your suggestion to control each switch individually was proven to work.

Adding a 1- second delay between each service call gives the system more time to propagate each command but causes the nth light to respond n seconds later.

Another way would be to create a group containing the switches and turn the group on/off. Tom_I suggested trying this approach.

Yet another way is via scenes which provide a bit more flexibility because a scene can do more than simply turn an entity on or off. Although, admittedly, all that extra flexibility is not needed for this application. :slight_smile: