How to access entity name within automation?

Hi team

Need a bit of help - and sure for the solution, but also for where I should be able to ‘read me to the answer’, I find it hard to see the logic in the documentation. So where should I be able to find the answer for my question ? Like which attributes are accessible where (like in automation) and how to get to it ? I would like to master HASS ;-))

But now to the question.
I have many windows, and would like to know when they are opened.
I would like to have just one automation.
I have a ‘sonos_say’ script that controls a sonos speaker - this works fine.

Now, what I would like to do, is in the ‘message’ string, to reference the friendly name of the entity. But I do not succeed with this. The speaker reads out the ‘code’ instead of resolving to the friendly name.

I have tried with:
message: ‘{{states(sensor.friendly_name)}} Window has been opened’
message: “{{trigger.entity_id.friendly_name}} Window has been opened”

I would like to go with out ‘ “ for the code part, but the editor says no :wink:

So, what is the proper way of doing this ?

The code:

- id: '1574603766995'
  alias: Window opened
  description: ''
  trigger:
  - device_id: f7a55006ff8e442a8a145292edc71ffc
    domain: binary_sensor
    entity_id: binary_sensor.masterbedroom_window_detector_sensor_2
    platform: device
    type: opened
  condition: []
  action:
  - data:
      delay: 00:00:05
      message: "{{trigger.entity_id.friendly_name}} Window has been opened"
      sonos_entity: media_player.kitchen
      volume: 0.3
    service: script.sonos_say

Regards Jørn

Can see that the solution is in templates…

- data_template:
      delay: 00:00:05
      message: '{{trigger.entity_id}} Window has been opened'
      sonos_entity: media_player.kitchen
      volume: 0.3

Is getting me closer to it…

"{{ trigger.to_state.attributes.friendly_name }}"

:wink:

3 Likes

Thanks…

The code:

- id: '1574603766995'
  alias: Window Opened
  description: ''
  trigger:
  - device_id: f7a55006ff8e442a8a145292edc71ffc
    domain: binary_sensor
    entity_id: binary_sensor.masterbedroom_window_detector_sensor_2
    platform: device
    type: opened
  - device_id: fe5df0a64c444dd3a8665a5cc28e020d
    domain: binary_sensor
    entity_id: binary_sensor.bathroom_window_detector_sensor
    platform: device
    type: opened
  condition: []
  action:
  - data_template:
      delay: 00:00:05
      message: '{{trigger.to_state.attributes.friendly_name}} opened'
      sonos_entity: media_player.kitchen
      volume: 0.3
    service: script.sonos_say
````Preformatted text`

Place to read about it:
https://www.home-assistant.io/docs/configuration/templating/

Thanks.
/Jørn