pabou
March 14, 2026, 11:33am
1
I would like to turn a switch on/off with the target entity NOT being hardcoded in the action, but “templated”
iow, something like:
action: switch.turn_off
metadata: {}
data: {}
target:
entity_id: {{states(‘some entity’)}}
Is this at all possible ?
What I am doing is redirect solar surplus into electric heaters, with some “ranking”, ie use first such heater, and if there is still surplus use this other heater. The actual heaters to be used, and in which sequence are configured in a dashboard, and stored in helpers
Thanks for your help !!
tom_l
March 14, 2026, 11:35am
2
pabou:
Is this at all possible
Yes. Though you would not use this:
As that will return the state value of “some entity”, not the entity id. Or it would if you had quoted it correctly.
Unless “some entity” contains an entity_id string as it’s state value.
Here’s an example template:
- action: switch.turn_off
target:
entity_id: >
{% if states('sensor.excess_energy')|float(0) > 420 %}
switch.heater_1
{% elif states('sensor.excess_energy')|float(0) > 42 %}
switch.heater_2
{% else %}
switch.heater_3
{% endif %}
pabou
March 14, 2026, 11:56am
3
thanks tom_l. you saved my day . yes the sensor I am using contains the entity id of the actual switch , as one of its attributes
The yaml below seems to be acceptable to HA.
target:
entity_id: |
{{state_attr('sensor.solar2heater_primary', 'relay')}}
the sensor ‘sensor.solar2heater_primary’ has attributes:
relay: switch.salon_switch_0
pilote: true
friendly_name: solar2heater primary
1 Like