Alert with templated message

I’m trying to setup an alert to notify me when a door is left unlocked. I’d like the message (name) to be dynamic based on what lock triggered the alert. I’m not sure what variables are available here. It doesn’t seem to be the same as templates for automations. I like the repeat feature of alerts, specifically the dynamic delay times (not used in example), if its possible to do that with an automation instead?

I’ve tried many different variations of this, but can’t seem to figure the correct variables for the name template:

alert:
  lock_left_unlocked:
    name: '{{ trigger.to_state.attributes.friendly_name }} door is unlocked'
    entity_id: lock.all_locks
    state: 'unlocked'
    repeat: 5
    skip_first: true
    notifiers:
      - ios_ryans_iphone
      - html5_notifier

You can’t use ‘trigger’ with alert. You can’t make it dynamic either unless you pair it with a binary_sensor. Even then, you’ll have to list out your entities so that the sensor updates when any door updates. If you group them and choose not to list them out, the sensor referencing the group will only update when the group updates. It will not update when the items in the group update. This caveat alone makes your request complicated. You could use the group and have it update based on the sensor.time platform, but the problem there is that it would only update every minute. Not exactly when a door goes from locked to unlocked.

So with that being said, I would list them out individually for the binary sensor template. And then use an automation to display the names, or use alert to give a basic message:

binary_sensor:
  - platform: template
    sensors:
      doors_unlocked:
        value_template: >
          {% set locks = [ states.lock.lock1, states.lock.lock2 ] %}  #replace these names with your lock names.
          {% set unlocked = locks | selectattr('state','eq','unlocked') | list %}
          {{ unlocked | length >= 1 }}
        friendly_name: 'Are doors unlocked?'

Automation:

- alias: Door alert
  trigger:
    - platform: state
      entity_id: binary_sensor.doors_unlocked
      state: 'on'
      for:
        minutes: 5
  action:
    - service: notify.nofity
      data_template:
        message: >
          {% set unlocked = states | selectattr('entity_id', 'in', state_attr('lock.all_locks','entity_id')) | selectattr('state','eq','unlocked') | map(attribute='name') | join(', ') %}
          The following doors are unlocked: {{ unlocked }}
          

or Alert:

alert:
  door_alert:
    name: Door(s) are open.
    entity_id: binary_sensor.doors_unlocked
    repeat: 5
    skip_first: true
    notifiers:
      - ios_ryans_iphone
      - html5_notifier

If you want dynamic names to appear, you cannot use the alert component. The alert component does not allow templating ({{}}, {%%}).

2 Likes

I have a quick question that comes up from time to time. What is the difference between data: and Data_Template. For example, I use pushbullet for my notifications. I’ve built notifications over the past few months and notice that I mostly use data:, but also have a few with data_template. But they seem to be used interchangably (I probably copied them, Like I’m doing to yours here. :slight_smile:

For example:

  - service: notify.pushbullet_notifications
    data:
    message: "{{ trigger.to_state.attributes.friendly_name }} is offline" 

and

  - service: notify.pushbullet_notifications
    data_template:
    message: " At {{ as_timestamp (now()) | timestamp_custom('%I:%M %p') }} on {{ now().strftime('%d %b %Y') }} " 

Both work. Is it pushbullet, or is data depricated? Thanks in advance

There was a small typo in this an extra ’ at the end of the 1st line

{% set unlocked = states | selectattr('entity_id', 'in', state_attr('lock.all_locks','entity_id')) | selectattr('state','eq','unlocked') | map(attribute='name') | join(', ') %}
The following doors are unlocked: {{ unlocked }}

works. Thanks again for this.

1 Like

The difference between data and data_template is simply the ability to place a template in any sub-keyword.

Your first example:

is actually invalid because you have a template inside data.

The fact that it works with pushbullet is odd. It shouldn’t.

Either way, if you keep everything a data_template, it shouldn’t ever matter if you use templating or not.

I’ll make the edit to my post

Sounds like for the most part I should just use data_template, and not have to worry about it in the future. Just to make sure I’m not misunderstanding. This is fine too??

action:
  - service: input_select.select_option
    entity_id: input_select.house_mode
    data_template:
      option: 'Away'
  - service: input_select.select_option
    entity_id: input_select.security_system
    data_template:
      option: 'Armed (Away)'

Because I could go back in the future and put a template for the option: field., right?

Yeah, that works. I’m not sure if templates are any slower though. Either way, all you need to remember is that data doesn’t allow templating, data_templates does.