Getting the room name of the device

There are sensors on doors and windows in every room. Although the names of these are in the form of “door” or “window”, I separate them according to their room names.

I have prepared an automation for all sensors in the system on a single automation as follows. It works smoothly.

But I want to get the name of the room in the warning title or message content and I just couldn’t do it. Can I access this information from device information or what route should I follow.

I would be very glad if you can forward with the sample.

thanks in advance.

alias: Security - Door and Window Sensors
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.kapi
  - platform: state
    entity_id: binary_sensor.hareket_sensoru_mutfak
condition: []
action:
  - service: notify.notify
    data:
      title: Güvenlik Sistemi
      message: >-
        {% if trigger.to_state.attributes.device_class == 'motion' %} {% set
        true_value='hareket algılandı' %}{% set false_value='temiz' %} {% elif
        trigger.to_state.attributes.device_class == 'opening' %} {% set
        true_value='açık' %}{% set false_value='kapalı' %} {% endif %} {{
        trigger.to_state.name }}{%- if trigger.to_state.state == 'on' %}
        {{true_value}} !!!{% elif trigger.to_state.state == 'off' %}
        {{false_value}}.{% else %}{{ trigger.to_state.state }} {% endif -%}
mode: single

Where is the room’s name?

If you are included the room’s name in the entity’s object_id, using a consistent format, then you can extract it. For example, if given this:

binary_sensor.hareket_sensoru_mutfak

We can extract mutfak with this:

{{ trigger.to_state.object_id.split('_')[2] }}

However, in the example you posted, the first binary_sensor has no room name. All the binary sensors would need to follow a consistent naming format.


Instead of adding the room’s name to the entity’s object_id, consider adding it as a custom attribute. Configuration > Customization

It then becomes simple to get the room’s name. Assuming you call the attribute room it’s as easy as this:

(( trigger.to_state.attributes.room }}

Thank you very much for your answer.
Hopefully someday we can easily receive the device room information as you convey below.

{{ trigger.to_state.attributes.room }}

good work

Is there an easier and healthier way as below, or will it be added?
In case of a change in room name etc., it seems more healthy to manage from one place.

{{ trigger.to_state.attributes.room }}

thanks.

In order to use this template in an automation’s action

{{ trigger.to_state.attributes.room }}

the two binary_sensors in the automation’s trigger

trigger:
  - platform: state
    entity_id: binary_sensor.kapi
  - platform: state
    entity_id: binary_sensor.hareket_sensoru_mutfak

must have room attributes. You must create the room attribute and assign it a value.

To create the room attribute for each binary_sensor, you can use Configuration > Customization or define it in the customize section of your configuration.yaml file as described here:

And finally the requested feature came with core-2021.9.0…

{{ area_name(trigger.entity_id) }}