Notification to State Which Sensor Triggered Manual Alarm

Hi All,

Looking for some help on how to send a notification which states which sensor tripped the manual alarm component, with the added complication of it being in a separate automation to the alarm trigger automation.
I want to keep the two separate so the notification is only sent when the alarm reaches the alarm triggered state, rather than the pending state.

My current code (simplified down to make it easier to read)

# ARMED AWAY TRIGGER
- id: alarm_trigger_away
  alias: Trigger Alarm While Armed Away
  trigger:
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d0001fd637f #Side Door
      to: 'on'
  condition:
    - condition: state
      entity_id: 'alarm_control_panel.home_alarm'
      state: armed_away
    - condition: state
      entity_id: 'input_boolean.bed_mode'
      state: 'off'
  action:
    - service: alarm_control_panel.alarm_trigger
      entity_id: alarm_control_panel.home_alarm
# ARMED AWAY TRIGGER NOTIFICATION  
- id: alarm_away_trigger_notification
  alias: Send notification when alarm away triggered
  trigger:
    - platform: state
      entity_id: alarm_control_panel.home_alarm
      to: 'triggered'
  action:
    - service: notify.notify
      data_template:
        title: "ALARM!"
        message: "The alarm has been triggered by the {{ trigger.to_state.attributes.friendly_name }} at {{now().strftime('%H:%M %d-%m-%Y')}}." 

Putting the {{ trigger.to_state.attributes.friendly_name }} in the original alarm automation will send the correct sensor (in this case the side door sensor), but moving it to the secondary automation the trigger is the home alarm itself.

Is there a way to have the sensor from the primary automation carry through?

Thanks

By ‘carry through’ you mean the trigger object of one automation referenced by another automation, the answer is no.

Why not just move the second automation’s action (notify.notify) to the first automation’s action? Then it will have access to the desired trigger object.

:thinking: Yeah, that’s what I meant by carry through.
I wanted to keep them separate as if they’re in the same automation I end up with notifications each time the alarm is triggered, regardless of if it goes to full trigger or the pending state.

You lost me there. Your first automation calls alarm_control_panel.alarm_trigger which triggers the second automation whose only task is to call notify.notify. That’s why I suggested you move notify.notify from the 2nd automation to the 1st one.

Apologies, I’ll try and make it a little clearer.

When the first automation calls alarm_control_panel.alarm_trigger this is using the Manual Alarm Control Panel platform.
When set at armed_away there is a pending time of 30 seconds before moving to the triggered state.
The first automation I posted is a slimmed down version of the full trigger automation I have which works off multiple door & motion sensors. If the notify.notify service is called in this automation, it ignores the pending time in the Manual Alarm Control Panel platform, and fires during the pending time.

Splitting this out in to the second automation allows the notify.notify service to only be called once the alarm passes through the pending state, to the triggered state.

The second automation has also been slimmed down to only show the notify.notify function, as it also calls a number of other services to flash lights, sound an alarm etc.

I store the trigger entity in of all things a dummy device tracker. I cant remember where I got the original idea from.

Device tracker definition:

# Device tracker masquerading as variable storage
device_tracker:
  - platform: mqtt
    devices:
      sensor_name: 'sensor/name'

Store a value (as well as trigger the next automation):

    action:
      - service: alarm_control_panel.alarm_trigger
        entity_id: alarm_control_panel.alarm
      - service: mqtt.publish
        data_template:
          payload: "{{ trigger.entity_id }}"
          topic: sensor/name
          retain: true

And finally retrieve the value in the next automation:

      - service_template: notify.alarms
        data_template:
          message: >
            Home alarm triggered - sensor: {{ states('device_tracker.sensor_name') }}
          target:
            - '+61412345678'

Thanks for taking the time to explain the details; I understand the challenge now.

The solution is to store the friendly_name of whatever triggers the first automation, then retrieve it in the second automation.

You can do that using chris_avfc’s zoogara’s suggestion using MQTT and a device_tracker. Alternately, you can store the friendly_name in an input_text.

Define the input-text entity:

input_text:
  trigger_source:
    name: Trigger Source

In the first automation, save the friendly_name of whatever triggered the automation to input_text.trigger_source:

  action:
    - service: input_text.set_value
      data_template:
        entity_id: input_text.trigger_source
        value: "{{ trigger.to_state.attributes.friendly_name }}"
    - service: alarm_control_panel.alarm_trigger
      entity_id: alarm_control_panel.home_alarm

In the second automation, simply retrieve the value of input_text.trigger_source:

  action:
    - service: notify.notify
      data_template:
        title: "ALARM!"
        message: "The alarm has been triggered by the {{ states('input_text.trigger_source') }} at {{now().strftime('%H:%M %d-%m-%Y')}}." 

4 Likes

Perfect, that’s exactly what I was looking for!
I just want sure on the way to store a value.

I’ve tested this out earlier today and it’s working perfectly!

Thanks very much.

Looks like you had the same idea as @zoogara.

Thanks ever so much for the help!

Yes, same idea (store the entity_id) but different implementation (store in input_text; no need for MQTT).

I’ve just tried implementing this but my sensor is shown as “unknown” in the notification… I’ve checked and all my sensors have friendly names setup… any ideas?

I am also getting unknown, not sure what I have done wrong

Full source automation code

- id: 'triggersourceauto'

  alias: Trigger Source Info

  trigger:

  - platform: state

    entity_id: binary_sensor.garage_door_contact

    to: 'on'

    from: 'off'

  - platform: state

    entity_id: binary_sensor.gate_garage_side_contact

    to: 'on'

    from: 'off'

  - platform: state

    entity_id: binary_sensor.gate_bedroom_side_contact

    to: 'on'

    from: 'off'

  - platform: state

    entity_id: binary_sensor.front_door_contact

    to: 'on'

    from: 'off'

  - platform: state

    entity_id: binary_sensor.back_door_contact

    to: 'on'

    from: 'off'

  - platform: state

    entity_id: binary_sensor.garage_sliding_internal_door_contact

    to: 'on'

    from: 'off'

  - platform: state

    entity_id: binary_sensor.sliding_door_sensor_contact

    to: 'on'

    from: 'off'

  condition:

  - condition: state

    entity_id: alarm_control_panel.home_alarm

    state: armed_home

  action:

    - service: input_text.set_value

      data_template:

        entity_id: input_text.trigger_source

        value: "{{ trigger.to_state.attributes.friendly_name }}"

    - service: alarm_control_panel.alarm_trigger

      entity_id: alarm_control_panel.home_alarm

Full notify automation code

- id: 'triggersourcenotify'

  alias: Trigger Source Notification

  trigger:

  - platform: state

    entity_id: binary_sensor.garage_door_contact

    to: 'on'

    from: 'off'

  - platform: state

    entity_id: binary_sensor.gate_garage_side_contact

    to: 'on'

    from: 'off'

  - platform: state

    entity_id: binary_sensor.gate_bedroom_side_contact

    to: 'on'

    from: 'off'

  - platform: state

    entity_id: binary_sensor.front_door_contact

    to: 'on'

    from: 'off'

  - platform: state

    entity_id: binary_sensor.back_door_contact

    to: 'on'

    from: 'off'

  - platform: state

    entity_id: binary_sensor.garage_sliding_internal_door_contact

    to: 'on'

    from: 'off'

  - platform: state

    entity_id: binary_sensor.sliding_door_sensor_contact

    to: 'on'

    from: 'off'

  condition:

  - condition: state

    entity_id: alarm_control_panel.home_alarm

    state: armed_home

  action:

    - service: notify.notify

      data_template:

        title: "ALARM!"

        message: "The alarm has been triggered by the {{ states('input_text.trigger_source') }} at {{now().strftime('%H:%M %d-%m-%Y')}}."

A little late, but I had the same problem when setting up just now and maybe you didn’t solved this yet.
When I replaced input_text.trigger_source by input_text.text3 it worked.

It was text3 because this is the number of the input_text in my configuration.yaml file

input_text:
  text1:
    name: some_other_stuff1
  text2:
    name: some_other_stuff2
  text3:
    name: alarm_trigger
1 Like

Thanks for the post @Jesse89 . I am not sure what happened with mine or what I did, but, it started working :crazy_face:

I will keep this in mind though in case it changes.

Hello. I tried implenting your code and it works great except I have a “Unkown” message instead of my entity name. Where do you write this code :
input_text:
trigger_source:
name: Trigger Source
on Configuration.yaml?
Thank you

Follow the instructions here:

Input Text - Home Assistant (home-assistant.io)

thank you very much

Hello,

Since new MQTT implementation,
it does not work anymore…

Could it be updated ?

Thank you

Use input_text - like in this post:

1 Like