Automation not always triggering

I created an automation to automatically turn the garage lights on, when motion is detected and then turn them off 5 minutes after motion has stopped. I don’t want to turn them off, if they were manually turned on, which is what the input boolean is tracking.

The light automatically turns on properly, but it will not turn off (most of the time). What am I missing or done wrong?

Thanks in advance!

alias: Motion - Garage Lights
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.ratgdo32_2b11a0_motion
    to:
      - "on"
    from:
      - "off"
    id: "On"
  - trigger: state
    entity_id:
      - binary_sensor.ratgdo32_2b11a0_motion
    for:
      hours: 0
      minutes: 5
      seconds: 0
    from:
      - "on"
    to:
      - "off"
    id: "Off"
conditions:
  - condition: state
    entity_id: input_boolean.state_toggle_garage_light
    state:
      - "off"
actions:
  - if:
      - condition: trigger
        id:
          - "On"
    then:
      - type: turn_on
        device_id: e3758227c807d6a60c35097079b2d537
        entity_id: 200ee03e6b3d6b13cc9cc3186cf70988
        domain: switch
    else:
      - type: turn_off
        device_id: e3758227c807d6a60c35097079b2d537
        entity_id: 200ee03e6b3d6b13cc9cc3186cf70988
        domain: switch
  - action: input_boolean.turn_off
    metadata: {}
    target:
      entity_id: input_boolean.state_toggle_garage_light
    data: {}
mode: single
alias: State Toggle - Garage Light
description: ""
triggers:
  - type: turned_on
    device_id: e3758227c807d6a60c35097079b2d537
    entity_id: 200ee03e6b3d6b13cc9cc3186cf70988
    domain: switch
    trigger: device
    id: "On"
  - type: turned_off
    device_id: e3758227c807d6a60c35097079b2d537
    entity_id: 200ee03e6b3d6b13cc9cc3186cf70988
    domain: switch
    trigger: device
    id: "Off"
conditions: []
actions:
  - if:
      - condition: trigger
        id:
          - "On"
    then:
      - action: input_boolean.turn_on
        metadata: {}
        target:
          entity_id: input_boolean.state_toggle_garage_light
        data: {}
    else:
      - action: input_boolean.turn_off
        metadata: {}
        target:
          entity_id: input_boolean.state_toggle_garage_light
        data: {}
mode: single

You need to use context in order to get this done. If the context matches the automation, do not turn on/off the input boolean.

Thank you, this seems like what I need. I’ve been looking into it, trying to understand what’s going on in this forum post: How to use context

But still struggling to follow all that is going on. Do you have any other pointers I could look at? I’m fairly new to HA.

The first post highlights everything you need. You want to only trigger the input boolean on “Physical device”, which is the first choose. i.e.

alias: State Toggle - Garage Light
description: ""
triggers:
  - platform: state
    entity_id:
      - switch.whatever_this_is
    to: "on"
    from: "off"
    id: "On"
  - platform: state
    entity_id:
      - switch.whatever_this_is
    to: "off"
    from: "on"
    id: "Off"
conditions:
  - condition: template
    value_template: '{{ trigger.to_state.context.id != none }}'
  - condition: template
    value_template: '{{ trigger.to_state.context.parent_id == none }}'
  - condition: template
    value_template: '{{ trigger.to_state.context.user_id != none }}'
actions:
  - if:
      - condition: trigger
        id:
          - "On"
    then:
      - action: input_boolean.turn_on
        metadata: {}
        target:
          entity_id: input_boolean.state_toggle_garage_light
        data: {}
    else:
      - action: input_boolean.turn_off
        metadata: {}
        target:
          entity_id: input_boolean.state_toggle_garage_light
        data: {}
mode: single

Thank you. I think I’ve got it setup now. I’ll have to test it when I get home tonight. I was originally trying to add the condition to the motion automation and not to the state toggle.

EDIT: Realized I could trigger the motion sensor remotely with the developer tools. Automation worked as expected.