Automation won't turn off other automation

When I go to sleep I enable the ‘sleep mode’ switch to change the lighting in my house. Now I also want HA to turn off my motion sensor automation in the hallway, so when the cats run around at night, I won’t get woken up. I believe I have written a correct automation for this (it gets triggered upon a change in the state of the sleep mode) but it does not turn off the automation. Can anyone advise me on what is going wrong ?

The script (written using the UI but this is the code):

    - id: '1646174120151'
      alias: Sleeping
      description: ''
      trigger:
      - platform: state
        entity_id:
        - switch.adaptive_lighting_sleep_mode_default
      condition: []
      action:
      - choose:
        - conditions:
          - condition: state
            state: 'On'
            entity_id: switch.adaptive_lighting_sleep_mode_default
          sequence:
          - service: automation.turn_off
            data: {}
            target:
              entity_id: automation.hallway_light
        - conditions:
          - condition: state
            state: 'Off'
            entity_id: switch.adaptive_lighting_sleep_mode_default
          sequence:
          - service: automation.turn_on
            data: {}
            target:
              entity_id: automation.hallway_light
        default: []
      mode: single

I’m not sure if one could do it like this. What’s the state reported by switch.adaptive_lighting_sleep_mode_default anyway if you check development tools?

as per Matte, your switch states should be

state: 'off' not state: 'Off'

I have similar Automations that follow this logic and work:

alias: New Automation
description: ''
mode: single
trigger:
  - platform: state
    entity_id:
      - switch.adaptive_lighting_sleep_mode_default
    to: 'on'
    id: switchstate_on
  - platform: state
    entity_id:
      - switch.adaptive_lighting_sleep_mode_default
    to: 'off'
    id: switchstate_off
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: switchstate_on
        sequence:
          - service: automation.turn_off
            data: {}
            target:
              entity_id: automation.hallway_light
      - conditions:
          - condition: trigger
            id: switchstate_off
        sequence:
          - service: automation.turn_on
            data: {}
            target:
              entity_id: automation.hallway_light
    default: []

Script would be relatively similar I guess

Thank you, it was indeed the capitalization!

Hi @distinq, can you please mark this topic as solved?

If you mark this thread as solved by selecting the answer which gave you the solution, other members will not jump in to try to help (like I just did) AND the topic can be useful for others having a similar issue.

If you wish, you can reduce the automation to this:

    - id: '1646174120151'
      alias: Sleeping
      description: ''
      trigger:
      - platform: state
        entity_id: switch.adaptive_lighting_sleep_mode_default
      condition: []
      action:
      - service: "automation.turn_{{ iif(trigger.to_state.state == 'on', 'off', 'on') }}"
        target:
          entity_id: automation.hallway_light
      mode: single

Why not just add this as the condition in automation.hallway_light:

condition:
  conditon: state
  state: 'off'
  entity_id: switch.adaptive_lighting_sleep_mode_default

So the hallway light automation doesn’t do anything unless the sleep mode switch is off.

Maybe its just me but having automations turn others on and off seems very confusing. Now you have an extra automation and you have to look in two different automations to have a complete understanding of the logic. Seems better to have all the logic in one place.

I agree with CentralCommand; it’s not a good practice to have one automation disable another as a normal operation.

If you want one script/automation to influence the behavior of another script/automation, consider using another entity, perhaps an input_boolean, to serve as a middleman. For example, one script/automation turns the input_boolean on or off, the other uses the input_boolean’s state, in a State Condition, to determine if it should proceed or not.

This strategy still allows you to manually disable an automation when needed without danger of having it automatically re-enabled by another automation.

2 Likes