Send "area_name" of an entity group to Telegram

In case of emergency I would like to send a Telegram message which door sensors of a door sensor group are open.
I managed to send all according device names of that group with the following code:

service: telegram_bot.send_message
data:
  message: >
    Open sensors: {{" \n -"}} {{ expand('binary_sensor.door_sensors') |
    selectattr('state', 'eq', 'on') | map(attribute='name') | list | join('\n')
    }} 
  target: -xxxxxxxxx

This works but I would like to attach the area_name of each device too, so the output would be as follows:

Open sensors:
    [deviceName] in [deviceArea]

Any idea on how to accomplish this?
Thanks in advance!

Open sensors:  {% set doors = expand('binary_sensor.door_sensors')
| selectattr('state', 'eq', 'on') | list %}
{%- for door in doors %}
{{-" \n -"}}{{ door.name }} in {{ area_name(door.entity_id)}}
{%- endfor %}

Thanks man!
Is it also possible to add area aliases instead of area_name itself?

AFAIK, aliases for areas are not currently directly accessible by templates. If you really want to use different names you can use a mapper approach in the message template or add a custom attribute to each of your door sensor entities.

For managing purposes and sharing stuff with this community I keep everything in english in the “backend”, which keeps everything consistent, maintainable and more understandable for others.
That’s the reason I have also set the main area names in english language and also numbered and not tied to a specific person (e.g. “TopFloor02_Bedroom03”).

Obviously this is confusing other family members, especially those which are not familiar with english language.

That’s the reason I want to make use of area name aliases, mainly for spreading helping informations to family members via Telegram.

Thanks for your help @Didgeridrew, I guess I will make a feature request on this.

I would recommend taking advantage of the new template macros to translate your area names to aliases.

{%- macro area_alias(area_name)%}
{%- set alias_map = {
'Living Room': 'la sala',
'Utility Room': 'el lavadero',
'Basement': 'el sótano',
'Kitchen': 'la cocina'
} %}
{%- if area_name in alias_map -%}
{{- alias_map[area_name] }}
{%- else %}
{{ area_name }}
{%- endif %}
{%- endmacro%}

Then the template for the doors would be something like:

Puertas Abiertas:  {% from 'YOUR_MACROS.jinja' import area_alias %}{% set doors = expand('binary_sensor.door_sensors')
| selectattr('state', 'eq', 'on') | list %}
{%- for door in doors %}
{{-" \n -"}}{{ door.name }} en {{ area_alias(area_name(door.entity_id)) }}
{%- endfor %}
1 Like

WOW! That worked like a charm, thank you so much mate, you don’t know how much you helped me :heart:

Edit: Unfortunately I could only send you a donation, but you should know that in addition I would shake your hand personally if I could :wink: