Automation template display friendly name help

Hi Eveyone

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

Here is one way to accomplish what you have outlined in you example

alias: Notification - Anniversaries Test
description: ''
variables:
  anni_list: >
    {{ states.sensor | select("search", "sensor.anniversary_") 
    | map(attribute='entity_id') | list }}
  days: >
    {{ expand(anni_list) | map(attribute='state') | reject('in', ['unavailable', 'unknown']) 
    | map('int') | select('lt', 50) | list | join(',') }}
  anni_names: >
    {{ expand(anni_list) | selectattr('state', 'in', days) | map(attribute='name') | list }}
trigger:
  - platform: time
    at: '09:00:00'
condition:
  - condition: template
    value_template: |-
      {{ anni_names | count > 0 }}
action:
  - service: notify.mobile_app_adam_s21
    data_template:
      message: |-
        {{ anni_names }} is tomorrow
mode: single

Thank you, I have tried your code but all I get in the notification is the following:

{‘[object Object]’: None} is tomorrow

That’s my fault, there was a missing multiline quote symbol for the anni_names variable. I have corrected it above.

Hi Drew

Sorry, I now get the following when trying the use the adjusted code:

**Message malformed: invalid template (TemplateSyntaxError: expected token ',', got '_days') for dictionary value @ data['variables']['anni_names']**

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

Thanks Drew, it is now working!