Use entity_id as variable in automation

Hello,

I’m currently looking for the best way to update the heating mode of each heaters/zones in my apartment with an input_select. I’ll have 5 heater with 5 modes each, but hard configuring all the cases looks like overdoing it.

The input_select looks like this:

input_select:
heater_living_room:
name: Heater Living Room
options:
- Arret
- Confort
- Eco
- Hors Gel
- Confort -2
initial: Eco
icon: mdi:gauge

upon state changed, the following automation is started. This is where I’ll have 5*5 cases!

alias: Living Room Heater Select Eco
trigger:
platform: state
entity_id: input_select.heater_living_room
to: Eco
action:
- service: shell_command.living_room_2

this then triggers one of the following:
shell_command (only automation for case 2 is configured):

living_room_0: ‘echo 0aaaa > /dev/ttyUSB0’
living_room_1: ‘echo 1aaaa > /dev/ttyUSB0’
living_room_2: ‘echo 2aaaa > /dev/ttyUSB0’
living_room_3: ‘echo 3aaaa > /dev/ttyUSB0’
living_room_5: ‘echo 5aaaa > /dev/ttyUSB0’

(and thats only for one room)

So I was wondering

  • if there is some kind of way to generalize the automation part, like being able to call the service with a variable based on the entity_id like this (names will need to be tweaked obviously):

alias: Living Room Heater Select Eco
trigger:
platform: state
entity_id: input_select.{%VARIABLE}
to: Eco
action:
- service: shell_command.{%VARIABLE}_2

and/or like this

living_room_{%VARIABLE}: ‘echo {%VARIABLE}aaaaa > /dev/ttyUSB0’

(this is obviously a completely wrong syntax)

  • if there is also a way to match options from input_select (can’t use input_slider… or only with specific value like 0, 7, 4, 18, 20), with values to be used as variables for shell_command.

For some additional insight, I’m using an arduino connected in serial on HA and specific home made cards to create the 220V signal to control the heaters. Im passing the mode values to the 5 heaters by writing on /dev/ttyUSB0. Then the arduino pilots the High Voltage card (all is optocoupled) to generate the 220V signals, based on the ‘norme fil pilote’ we do have over here.

thanks a lot! :slight_smile:

So if I understand correctly, you want to fire off different actions, based on what entity_id triggers the automation. Right?

You can create different scripts for your heaters and then like this:

- alias: 'Heater Control'
  trigger:
    - platform: state
      entity_id: 
        - input_select.heater_living_room
        - input_select.heater_another_room
        - input_select.heater_whatever_room
      to: 'Eco'
  action:
    - service: script.turn_on
      data_template:
        entity_id >
            {% if trigger.entity_id == "input_select.heater_living_room" %} script.living_room
            {% elif trigger.entity_id == "input_select.heater_another_room" %} script.another_room
            {% elif trigger.entity_id =="input_select.heater_whatever_room" %} script.whatever_room
            {% endif %}

Not sure how to implement IF/ELSE statement in the shell command service call, that’s why I suggest using scripts.

You can try this but I did not test this or have ever used it before:

- alias: 'Heater Control'
  trigger:
    - platform: state
      entity_id: 
        - input_select.heater_living_room
        - input_select.heater_another_room
        - input_select.heater_whatever_room
      to: 'Eco'
  action:
    service_template: >
        {% if trigger.entity_id == "input_select.heater_living_room" %} shell_command.living_room
        {% elif trigger.entity_id == "input_select.heater_another_room" %} shell_command.another_room
        {% elif trigger.entity_id =="input_select.heater_whatever_room" %} shell_command.whatever_room
        {% endif %}
2 Likes

The second one works like a charm.
It’s not as dense as I would have expected, but it is much much better now!

Thank you very much!

1 Like

Just for reference if someone else is coming by; this is handled in the docs: https://www.home-assistant.io/docs/scripts/service-calls/#use-templates-to-determine-the-attributes

… and the template can be reduced to this one-liner:

    service_template: "shell_command.{{trigger.object_id[20:]}}"