Do not run an automation if one ran just earlier ? (tricky)

I’ve got these 2 automations :

This is notifying me if the alarm goes to “Home” mode, which can be happen manually (via the keypad) or via another HomeAss automation.

- alias: Alarm armed Home
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: alarm_control_panel.home_alarm
      to: armed_home
  action:
    - delay: '00:00:3'
    - service: notify.ha_telegram
      data_template:
        message: >
          {% if states.sensor.last_armed_by_user.state == ''  %}
             Someone has armed the alarm 'Home'
          {% else %}
            {{ states.sensor.last_armed_by_user.state }} has armed the alarm 'Home'
          {% endif %}

This will set the alarm to “Home” after 11pm when no motions for 20 min :

- alias: alarm set to 'home' after 11pm
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id:
      - binary_sensor.hallway_pir
      - binary_sensor.landing_pir
      - binary_sensor.lounge_pir
      - binary_sensor.kitchen_pir
    to: 'off'
    for:
      minutes: 20
  condition:
  - condition: time
    after: '23:00:00'
    before: '06:00:00'
  - condition: state
    entity_id: alarm_control_panel.home_alarm
    state: 'disarmed'
  action:
  - service: alarm_control_panel.alarm_arm_home
    entity_id: alarm_control_panel.home_alarm
  - service: notify.ha_telegram
    data_template:
      message: "Night alarm triggered"

Any idea how could I still receive notifications when the alarm goes to “home” EXCEPT if the trigger was done by the 11PM automation ?

Currently at 11PM I get two notifications :

  • Night alarm triggered
  • Someone has armed the alarm ‘Home’

Ideally I’d prefer to only receive :

  • Night alarm triggered

Try adding this condition to your first automation:

condition:
- condition: template
  value_template: >
    {{ not trigger.to_state.context.parent_id }}

I believe that parent_id is populated with a value that should link to an automation. If the parent_id is empty, it came from a user press or physical arm.

Not sure if it will work, haven’t played with this aspect much.

Or perhaps use an input_boolean “helper” as a flag.

I’ve added this in the message to see its value, and it’s None.
So it can’t be used I guess, as the two automation aren’t really in a parent/children dependancy…

Not 100% sure what you mean, if I set a boolean to true following the 11pm trigger, and set the other automation to not message if the boolean is true?
If that’s what you mean, when do I put the boolean back to false?

Another idea :
Could I maybe add a step before and after sending the 11pm notification, to disable temporarily the other automation maybe and then activate it back?

service: automation.turn_off
entity_id: automation.alarm_armed_home

Send the 11pm message 

service: automation.turn_on
  entity_id: automation.alarm_armed_home

You’re saying it’s none when the other automation triggers it? Try again but remove the .parent_id. The context object has 3 separate ids that should point to the context.

Edit: the context might only be available in the from_state too.

could I print everything under “trigger” in my telegram message ? so I could see if there’s anything useful in the object ?

I’d just do the to_state.context and the from_state.context. The rest would be info you already know.

FWIW, I carried out a simple test (with 0.111.4) using an automation triggered by an input_boolean (it reports trigger.to_state_context).

  • When the input_boolean is turned on via the UI, trigger.to_state.context.user_id contains a string identifier that represents my user account.

  • When the input_boolean is turned on via another automation, trigger.to_state.context.user_id contains the string None.

Therefore trigger.to_state.context.user_id can be used to determine if the automation was triggered by a user or by Home Assistant (i.e. an automation, script, or scene).

If you want to demonstrate it for yourself, create two input_booleans named flipper and toggler and use the two automations shown below.

  • If you toggle flipper via the UI, it triggers the “context” automation. It will report it was triggered by “user”.
  • If you toggle toggler via the UI, it triggers the “indirection” automation which causes the “context” automation to trigger. Given that “context” was triggered by the “indirection” automation, it will report that it was triggered by “Home Assistant”.
- alias: 'indirection'
  trigger:
    platform: state
    entity_id: input_boolean.toggler
  action:
    service_template: input_boolean.turn_{{trigger.to_state.state}}
    entity_id: input_boolean.flipper

- alias: 'context'
  trigger:
    platform: state
    entity_id: input_boolean.flipper
  action:
    service: persistent_notification.create
    data_template:
      title: 'Triggered {{ now() }}'
      message: >
        ({{ trigger.to_state.context.user_id }})
        Triggered by {{ 'user' if trigger.to_state.context.user_id != None else 'Home Assistant' }}

Screenshot from 2020-07-14 10-14-43

Thanks, I’ve tried various trigger.from_state / to_state to see what I could gather but didn’t go far.

I’ve managed to fix my problem by using :

- alias: alarm set to 'home' after 11pm
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id:
      - binary_sensor.hallway_pir
      - binary_sensor.landing_pir
      - binary_sensor.lounge_pir
      - binary_sensor.kitchen_pir
    to: 'off'
    for:
      minutes: 20
  condition:
  - condition: time
    after: '23:00:00'
    before: '06:00:00'
  - condition: state
    entity_id: alarm_control_panel.home_alarm
    state: 'disarmed'
  action:
  - service: automation.turn_off # no alarmed notif
    entity_id: automation.alarm_armed_home
  - service: alarm_control_panel.alarm_arm_home
    entity_id: alarm_control_panel.home_alarm
  - delay: '00:00:60' # this is because the alarm takes some time to become armed
  - service: automation.turn_on # alarmed notif activated again
    entity_id: automation.alarm_armed_home
  - service: notify.ha_telegram
    data_template:
      message: "Night alarm triggered"

Now I’ve got one single notification at 11pm :slight_smile:

1 Like