In the following Automation I have a template which returns the data I want as a condition as well as having the template as part of the notification which works but I would like to return the friendly name in the notification component, is this possible? I am not very good at templating so any help would be appreciated.
alias: Notification - Anniversaries Test
description: ''
trigger:
- platform: time
at: '09:00:00'
condition:
- condition: template
value_template: |-
{% for sensor in states.sensor %}
{{ sensor.entity_id if "sensor.anniversary_" in sensor.entity_id and sensor.state|int < 50 }}
{% endfor %}
action:
- service: notify.mobile_app_adam_s21
data_template:
message: >
{% for sensor in states.sensor %} {{ sensor.entity_id if
"sensor.anniversary_" in sensor.entity_id and sensor.state|int < 50 }}
{% endfor %} is tomorrow
mode: single
It seems that there is something weird going on with variables in notify services. I tried a number of different ways, but it only works if the template is completely within 1 variable.
alias: Notification - Anniversaries
description: ''
trigger:
- platform: time
at: '09:00:00'
condition:
- condition: template
value_template: '{{ names != "" }}'
action:
- service: notify.mobile_app_adam_s21
data:
title: Upcoming Birthdays and Anniversaries
message: '{{ names }}'
variables:
names: >-
{% set period = 50 %}
{% set anni_list = states.sensor | select("search", "sensor.anniversary_")|
sort(attribute='state') | map(attribute='entity_id') | list %}
{% set days = expand(anni_list) | sort(attribute='state') |
map(attribute='state') | reject('in',['unavailable', 'unknown']) |
map('int') | select('lt', period)| list| join(',') %}
{%- for a in anni_list %}
{%- set d = states(a)%}
{%- if d in days %}
{{ state_attr(a, 'friendly_name') }} is {{"in "~d~" days" if d|int > 1 else ("today" if d|int == 0
else "tomorrow")}}{{"<br>"}}
{% endif %}{% endfor %}
mode: single