ashscott
(Ash)
February 16, 2022, 7:26pm
1
I trying to get the name of the entity that triggers the below automation into the message bod, without success.
All works fine if I just put plain text in the message.
What am I missing here?
- alias: Notify pump state is clear
trigger:
- platform: state
entity_id: sensor.balance_pump_state
to: Clear
- platform: state
entity_id: sensor.main_pump_state
to: Clear
- platform: state
entity_id: sensor.waterfall_pump_state
to: Clear
action:
- service: notify.mobile_app_ash_iphone
data:
message: "{{ state_attr(trigger.event.data.entity_id, 'friendly_name') }}"
title: Koi pond
- service: notify.lounge_tv
data:
message: "{{ state_attr(trigger.event.data.entity_id, 'friendly_name') }}"
title: Koi pond
mode: single
You’re making it a more complicated than it is…
- alias: Notify pump state is clear
trigger:
- platform: state
entity_id: sensor.balance_pump_state
to: Clear
- platform: state
entity_id: sensor.main_pump_state
to: Clear
- platform: state
entity_id: sensor.waterfall_pump_state
to: Clear
action:
- service: notify.mobile_app_ash_iphone
data:
message: "{{ trigger.name }}"
title: Koi pond
- service: notify.lounge_tv
data:
message: "{{ trigger.name }}"
title: Koi pond
mode: single
trigger.name
will return the “friendly name” if one has been assigned and otherwise return the entity_id.
ghassan
(Ghassan Christensen)
February 16, 2022, 9:04pm
4
or use:
message: "{{ state_attr(trigger.entity_id, 'friendly_name') }}"
if you want the friendly name
5 Likes
WeBeRiO
(David MM)
July 15, 2022, 12:24pm
5
@ghassan A lot of thanks, workme perfectly
Cergon
April 11, 2023, 11:30am
6
The above suggestions are not working here. The notifications are being triggered from an event being fired and I have tried the following code:
service: notify.all
data:
message: >-
{{ state_attr(trigger.entity_id, 'friendly_name') }} detected {{ trigger.event.data.name
}} with confidence {{ trigger.event.data.confidence }}
Which results in no notification being sent and an error in the log.
Template variable error: ‘dict object’ has no attribute ‘entity_id’ when rendering ‘{{ state_attr(trigger.entity_id, ‘friendly_name’) }} detected {{ trigger.event.data.name }} with confidence {{ trigger.event.data.confidence }}’
service: notify.all
data:
message: >-
{{ trigger.name }} detected {{ trigger.event.data.name
}} with confidence {{ trigger.event.data.confidence }}
Which results in the notification being displayed only from the word ‘detected’ onwards.
Any ideas?
ghassan
(Ghassan Christensen)
May 4, 2023, 4:54pm
7
Hi @Cergon
The original code works if the trigger is entity state change, then you can operate on the related entity and fetch its friendly name etc.
In your case it is an event.
Could you please add more details on what kind of event it is, and what kind of information you would like to extract and include in your message from it or some how related entity