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:
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 }}
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.
For example:
- service: notify.pushbullet_notifications
data:
message: "{{ trigger.to_state.attributes.friendly_name }} is offline"
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 }}
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??
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.