Prevent automation running if triggered by specified automation

Hi,

I have an automation ‘A’ that monitors the state of a binary sensor and runs scripts when it is either turned off or on. This works perfectly.

However, I now have a situation where I would like to have another automation ‘B’ change the state of the sensor but not trigger the automation ‘A’.

I have tested disabling ‘A’ as the first action when ‘B’ runs and then re-enabling it at the end. This works but feels clunky - is there a more elegant method? Thanks

Condition in automation A that checks automation B has not run in the last few seconds:

conditions:
  - condition: template
    value_template: "{{ (now() - state_attr('automation.B','last_triggered')).total_seconds() > 5 }}"

Substitute in the entity ID for B, and change 5 to whatever value suits.

Alternative is to create a Toggle helper (input_boolean) and get B to turn that on. Then set up an if/then or choose in A’s action to test the value of that toggle helper and run the normal action if it’s off, or turn it off if it’s on.

You might be able to use the trigger context

@Troon thanks for the speedy reply :slight_smile:

I considered the input boolean on B but didn’t want to add yet another entity if I could help it.

I do like the template route though - feels elegant - many thanks

@koying thanks - I didn’t know about trigger context. It looks useful but in this case I have several automations that change the boolean only one of which I don’t want to have it trigger on the change.

Then you should probably change the automation to check the context instead of the last the the other automation ran.

@petro thanks for the suggestion but I’m not sure I understand why you are recommending checking the context in this situation.

I have a input boolean which is monitored for changes (on or off) by automation ‘A’
I have more than 1 automation that change the input boolean and in these cases I do want A to trigger.
I have one new automation ‘B’ that changes the input boolean but in this case I don’t want it to trigger A.

As I understood it checking the context will in this situation always tell me that it was an automation that altered the input boolean, there would be no way to tell if it was B or not.

Please let me know if I have misunderstood - thanks

No, it will tell you exactly what automation toggled the entity.

{{ trigger.to_state.context.parent_id != states.automation.b.context.id }}
2 Likes

@petro Ah OK I did misunderstand, so context sounds like the way to go.

Having added the template you kindly provided to my test automation it seems to be working but the ‘other way around’ i.e.:
If I set != in the template then A runs when the boolean is toggled by B
If I set == in the template then A does not run when the boolean is toggled by B

I could just leave it like that as it appears to be working but I’d really appreciate it if you could explain why or if I’ve missed something (again)

Thanks

alias: Automation A
description: ""
triggers:
  - alias: Changes to ON
    entity_id:
      - input_boolean.pets
    to: "on"
    id: "on"
    trigger: state
  - alias: Changes to OFF
    entity_id:
      - input_boolean.pets
    to: "off"
    id: "OFF"
    trigger: state
conditions:
  - condition: template
    value_template: >-
      {{ trigger.to_state.context.parent_id !=
      states.automation.automation_b.context.id }}
actions:
  - alias: logbook ON
    if:
      - condition: trigger
        id:
          - "on"
    then:
      - action: logbook.log
        metadata: {}
        data:
          name: test ON
          message: ON ON
  - alias: Logbook OFF
    if:
      - condition: trigger
        id:
          - "OFF"
    then:
      - action: logbook.log
        metadata: {}
        data:
          name: test OFF
          message: off off
mode: single

alias: Automation B
description: ""
triggers: []
conditions: []
actions:
  - action: input_boolean.toggle
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.pets
mode: single