IF, THEN, ELSE automation doesn't stop

I’m working on the automation below:

alias: Frigate work timed notifications
description: ''
trigger:
  - platform: mqtt
    topic: frigate/events
    payload: new
    value_template: '{{ value_json.type }}'
condition: []
action:
  - if:
      - condition: state
        entity_id: input_boolean.disable_person_notifications
        state: 'on'
    then:
      - stop: work-hours-person
        error: false
    else:
      - service: notify.mobile_app_ash_iphone
        data:
          message: >-
            A {{trigger.payload_json["after"]["label"]}} was tested on
            {{trigger.payload_json["after"]["camera"]}} camera.
          data:
            image: >-
              https://xxxxxxxxxxxxxxxx.ui.nabu.casa/{{trigger.payload_json["after"]["id"]}}/thumbnail.jpg
            tag: '{{trigger.payload_json["after"]["id"]}}'
            when: '{{trigger.payload_json["after"]["start_time"]|int}}'
            entity_id: camera.{{trigger.payload_json["after"]["camera"]}}
      - service: notify.tv
        data:
          message: >-
            A {{trigger.payload_json["after"]["label"]}} was tested on
            {{trigger.payload_json["after"]["camera"]}} camera.
      - service: notify.mobile_app_ashs_macbook_pro
        data:
          message: >-
            A {{trigger.payload_json["after"]["label"]}} was tested on
            {{trigger.payload_json["after"]["camera"]}} camera.
          title: Security
mode: single

My expectation is that when input_boolean.disable_person_notifications is on, the automation should stop.

However, it always executes the actions after the else, regardless of the state of input_boolean.disable_person_notifications.

Is my expectation or config in error?

You could do that a lot simpler by using that as a condition for the automation.

alias: Frigate work timed notifications
description: ''
trigger:
  - platform: mqtt
    topic: frigate/events
    payload: new
    value_template: '{{ value_json.type }}'
condition:
  - condition: state
    entity_id: input_boolean.disable_person_notifications
    state: 'on'
action:
  - service: notify.mobile_app_ash_iphone
    data:
      message: >-
        A {{trigger.payload_json["after"]["label"]}} was tested on
        {{trigger.payload_json["after"]["camera"]}} camera.
      data:
        image: >-
          https://xxxxxxxxxxxxxxxx.ui.nabu.casa/{{trigger.payload_json["after"]["id"]}}/thumbnail.jpg
        tag: '{{trigger.payload_json["after"]["id"]}}'
        when: '{{trigger.payload_json["after"]["start_time"]|int}}'
        entity_id: camera.{{trigger.payload_json["after"]["camera"]}}
  - service: notify.tv
    data:
      message: >-
        A {{trigger.payload_json["after"]["label"]}} was tested on
        {{trigger.payload_json["after"]["camera"]}} camera.
  - service: notify.mobile_app_ashs_macbook_pro
    data:
      message: >-
        A {{trigger.payload_json["after"]["label"]}} was tested on
        {{trigger.payload_json["after"]["camera"]}} camera.
      title: Security
mode: single

Thanks for the reply, @tom_l .

I have other stuff to add but can’t get the ‘stop’ to work and am trying to narrow down the problem.

On the face of it, does it seem correct?

I’m trying to do:

IF (x AND y)
OR z
THEN stop

ELSE execute the actions.

I believe tom_I’s suggestion is the better option.

  • The go/no-go decision to execute the action is made by the condition. If the decision is no-go then the action is not executed and the automation’s last_triggered value remains unchanged.

  • Your version makes the decision within the action so even if the automation effectively has nothing to do except stop, it must execute the action to achieve that (and so its last_triggered value is updated).

I guess if you want last_triggered to be updated (and a trace generated) even if the automation performs no actual work then your version is the one to use.

I ran a simplified version of the if-then action in your original post, which already seemed fine to me, and got the expected results(reached the “then” when the state was ‘on’, which then stopped the automation. Reached the “else” when the state was ‘off’). Try adding logbook.log actions to record the state(s) of things. Also check the traces to see if you can spot anything amiss.

if:
  - condition: state
    entity_id: light.attic
    state: 'on'
then:
  - service: logbook.log
    data:
      name: "If-Then-Else test"
      message: "Stopping.  Light state: {{ states('light.attic') }}"
      entity_id: light.attic
  - stop: work-hours-person
    error: false
else:
  - service: logbook.log
    data:
      name: "If-Then-Else test"
      message: "Continuing.  Light state: {{ states('light.attic') }}"
      entity_id: light.attic
1 Like