Templates in automation push notification message

Currently when one of the doors in my house are open I send a mobile_app notification with the following data:


{
  "message": "FYI: door/gate open for 30+ minutes
}

I’d like it to actually say which sensor triggered the automation, eg.

In pseudocode:
if triggered == binary_sensor.0x00fdghgfhgfh5646c_contact
“message”: "FYI: front door open for 30+ minutes

if triggered == binary_sensor.0x00158d05446545416_contact
“message”: "FYI: kitchen door open for 30+ minutes

Additionally, I’d like to append, e.g. if both are open once the second one triggers, it should say
“message”: "FYI: front door, kitchen door open for 30+ minutes

I’m not too familiar with jinja templating (yes I’ve looked at the template wiki), an example from someone who did something similar is way more useful :slight_smile:

what are the friendly names for those sensors?

Something like?

"{{ trigger.entity_id.attributes.friendly_name }} has been open for 30+ minutes."
1 Like

Yeah. But it could be different depending on your trigger type.

contact: true
linkquality: 21
battery: 100
voltage: 3005
friendly_name: Xi Door Kitchen_contact
device_class: door

That would almost do the trick, however for the WAF I’d like to have just Kitchen Door, i.e. a defined even friendlier string per entity. Is it possible to add a custom attribute for this to the entity or do I need to resort to an if else clause

Edit: Adding a custom attribute in the Customizations could work in this case. This doesn’t solve the appending of open doors question though.

I mean, gotta know what trigger you plan on using. Triggers have different properties and that will change how this template will work.

Also, appending the doors in question shouldn’t be a top priority. I’ve been using door notifications for 3 ears and have something similar to this and I have never hit a situation where 2 doors are open at the same time.

The trigger I am using is a state trigger, if a door contact is ‘on’ (i.e. open) for 30min, push a notification.

you keep the doors open for 30 minutes at a time?

Anyways, a notification without the combination would be

- alias: test
  trigger:
    - platform: state
      entity_id: binary_sensor.0x00fdghgfhgfh5646c_contact
      to: 'on'
      for:
        minutes: 30
    - platform: state
      entity_id: binary_sensor.0x00158d05446545416_contact
      to: 'on'
      for:
        minutes: 30
  action:
    - service: notify.notify
      data_template:
        message: >
          {% set names = {
            '0x00158d05446545416_contact':'kitchen door',
            '0x00fdghgfhgfh5646c_contact':'front door',
            } %}
          {{ names[trigger.object_id] }} open for 30+ minutes

Adding both names would complicate this quite a bit. You’d need to come up with a way to track which entities have been ‘on’ for 30+ minutes.

1 Like

Trying this out now and getting the following error:

Error rendering data template: UndefinedError: 'str object' has no attribute 'attributes'

My automation:

- id: 'Test trigger template'
  alias: Alert Doors Open TEST
  trigger:
  - entity_id: binary_sensor.0x00158d0002c6698c_contact
    for: 00:00:05
    platform: state
    to: 'on'
  - entity_id: binary_sensor.0x00158d00029b6ed4_contact
    for: 00:00:05
    platform: state
    to: 'on'
  - entity_id: binary_sensor.0x00158d00029c4938_contact
    for: 00:00:05
    platform: state
    to: 'on'
  - entity_id: binary_sensor.0x00158d0002d70a9f_contact
    for: 00:00:05
    platform: state
    to: 'on'
  condition: []
  action:
   - service: notify.mobile_app_skyg6
     data_template:
        message: >
          "{{ trigger.entity_id.attributes.friendlier_name }} has been open for 30+ minutes."

Also tried without the double quotes, same error. Any ideas?

p.s. I created a custom attribute ‘friendlier_name’ to show a proper name (using friendly_name gives the same error)

   - service: notify.mobile_app_skyg6
     data_template:
        message: "{{ trigger.to_state.attributes.friendlier_name }} has been open for 30+ minutes."
1 Like

Doesn’t work. “trigger.entity_id.attributes.friendlier_name” seems empty as all I get is

has been open for 30+ minutes.

Edit: It does work :slight_smile: I had one entity that did not have a friendlier_name attribute, and of course it was this one :wink:

This works great but how can I send a notification once it was closed again after it was 30min+ open?

First thing that comes to mind is either using stats/history stats and measure the time it is open (max 40 min in the past or so), if you then have an automation with a trigger when the status changes to closed, check in the condition whether the (history) stats is 30+min.

Probably easier to just flip a boolean to on when something is open for 30 min, use that in the condition and next to the message push you flip the boolean back to off.