ebiscaia
(Eduardo Biscaia de Queiroz)
September 21, 2024, 10:41pm
1
Hi guys,
I wrote a simple automation to get the friendly name of a target. However, I haven’t been lucky with that. Here it is:
alias: test
description: ""
trigger:
- platform: time
at: "08:30:55"
condition: []
action:
- action: switch.toggle
metadata: {}
data: {}
target:
entity_id: switch.tapo_smart_plug
- action: notify.mobile_app_ebiscaia_s_11pro
metadata: {}
data:
message: >-
the device {{ state_attr(entity_id, 'friendly_name') }} was
triggered
mode: single
The automation is not triggered in this case:
Template variable error: ‘entity_id’ is undefined when rendering ‘the device {{ state_attr(entity_id, ‘friendly_name’) }} was triggered’
And other attempts were (with no success and similar error messages):
target.entity_id
switch.tapo_smart_plug
Thanks
armedad
September 22, 2024, 12:04am
2
hi!
i think you have a misunderstanding of variables. in the switch.toggle call, your line that says:
entity_id: switch.tapo_smart_plug
only defines the entity_id for the call to switch.toggle. it does not define it for your whole automation.
in your particular example, there doesn’t seem to be a reason to use that entity_id… so you can just do:
the device {{ state_attr('switch.tapo_smart_plug', 'friendly_name') }} was
but i’m guessing there’s a different use case where you may want it calculated or passed in? if so, show that code and we may be able to help solve that situation.
ebiscaia
(Eduardo Biscaia de Queiroz)
September 22, 2024, 1:44am
3
Hi,
Thanks for your reply. Here is the automation I am trying to get proper notifications (do not consider the message blocks because it is where I need to fix):
alias: Plant light trigger
description: ""
trigger:
- platform: sun
event: sunrise
offset: "-1:00:00"
id: sunrise1
- platform: sun
event: sunrise
offset: "3600"
id: sunset2
- platform: sun
event: sunset
offset: "3600"
id: sunset
condition: []
action:
- choose:
- conditions:
- condition: trigger
id:
- sunrise1
sequence:
- metadata: {}
data: {}
action: switch.turn_on
target:
entity_id: switch.sunshine_for_plants_socket_1
- data:
title: Automation "Plant light trigger" executed
message: The device(s) was(were) turned on
action: notify.mobile_app_ebiscaia_s_11pro
- conditions:
- condition: trigger
id:
- sunset2
sequence:
- metadata: {}
data: {}
action: switch.turn_on
target:
entity_id: switch.tapo_smart_plug
- metadata: {}
data:
message: The device(s) {{ trigger.entity_id }} was(were) turned on
title: Automation triggered
action: notify.mobile_app_ebiscaia_s_11pro
- conditions:
- condition: trigger
id:
- sunset
sequence:
- metadata: {}
data: {}
action: switch.turn_off
target:
entity_id:
- switch.sunshine_for_plants_socket_1
- switch.tapo_smart_plug
- data:
message: >-
The device(s) {{ state_attr('target.entity_id', 'friendly_name')
}} was(were) turned off
action: notify.mobile_app_ebiscaia_s_11pro
enabled: true
mode: single
My idea is to use templates so that I can call the right name(s) without hard-coding the entities so that I could expand it in the future.
armedad
September 22, 2024, 3:25am
4
you could do something like this, but i don’t think it wins you anything.
alias: Plant light trigger
description: ""
trigger:
- platform: sun
event: sunrise
offset: "-1:00:00"
id: sunrise1
- platform: sun
event: sunrise
offset: "3600"
id: sunset2
- platform: sun
event: sunset
offset: "3600"
id: sunset
condition: []
action:
- variables:
use_entity: |
{% if trigger == 'sunrise1' %}
switch.sunshine_for_plants_socket_1
{% elif trigger == 'sunset2' %}
switch.tapo_smart_plug
{% elif trigger == 'sunset' %|
switch.sunshine_for_plants_socket_1, switch.tapo_smart_plug
{% endif %}
- choose:
- conditions:
- condition: trigger
id:
- sunrise1
sequence:
- metadata: {}
data: {}
action: switch.turn_on
target:
entity_id: >
{{ use_entity }}
- data:
title: Automation "Plant light trigger" executed
message: The device(s) was(were) turned on
action: notify.mobile_app_ebiscaia_s_11pro
- conditions:
- condition: trigger
id:
- sunset2
sequence:
- metadata: {}
data: {}
action: switch.turn_on
target:
entity_id:
{{ use_entity }}
- metadata: {}
data:
message: The device(s) {{ trigger.entity_id }} was(were) turned on
title: Automation triggered
action: notify.mobile_app_ebiscaia_s_11pro
- conditions:
- condition: trigger
id:
- sunset
sequence:
- metadata: {}
data: {}
action: switch.turn_off
target:
entity_id:
{{ use_entity }}
- data:
message: >-
The device(s) {{ state_attr('target.entity_id', 'friendly_name')
}} was(were) turned off
action: notify.mobile_app_ebiscaia_s_11pro
enabled: true
mode: single
TheFes
(The Fes)
September 22, 2024, 7:29pm
5
you could also add the entity_id(s) to use in a trigger variable. I also included the action to take, to even simplify it a bit more.
alias: Plant light trigger
description: ""
trigger:
- platform: sun
event: sunrise
offset: "-1:00:00"
id: sunrise1
variables:
action: "on"
to_toggle:
- switch.sunshine_for_plants_socket_1
- platform: sun
event: sunrise
offset: "3600"
id: sunrise2
variables:
action: "on"
to_toggle:
- switch.tapo_smart_plug
- platform: sun
event: sunset
offset: "3600"
id: sunset
variables:
action: "off"
to_toggle:
- switch.sunshine_for_plants_socket_1
- switch.tapo_smart_plug
action:
- action: switch.turn_{{ action }}
target:
entity_id: "{{ to_toggle }}"
- action: notify.mobile_app_ebiscaia_s_11pro
data:
title: Automation "Plant light trigger" executed
message: The {{ to_toggle | map('state_attr', 'friendly_name') | join(' and ') }} {{ 'was' if to_toggle | count == 1 else 'were' }} turned {{ action }}
mode: single
1 Like
armedad
September 22, 2024, 8:41pm
6
ah yes! @TheFes 's answer is better! i didn’t notice that the triggers mapped to the conditions one for one.