Trigger - Substitute value not working

Hi All

Can someone please tell me why this won’t work?

  - trigger:
      platform: state
      entity_id: sensor.hifi_input_function_raw
    action:  
    - service: switch.turn_off
      entity_id:
        - switch.denon_setfunc_dstv
        - switch.denon_setfunc_tuner
        - switch.denon_setfunc_spotify
    - service: switch.turn_on
      data:    
        entity_id: >
          switch.denon_setfunc_{{ sensor.hifi_input_function.state }}

But this works:

  - trigger:
      platform: state
      entity_id: sensor.hifi_input_function_raw
    action:  
    - service: switch.turn_off
      entity_id:
        - switch.denon_setfunc_dstv
        - switch.denon_setfunc_tuner
        - switch.denon_setfunc_spotify
    - service: switch.turn_on
      data:    
        entity_id: >
          {% if states.sensor.hifi_input_function.state == 'tuner' %}
            switch.denon_setfunc_tuner
          {% elif states.sensor.hifi_input_function.state == 'dstv' %}
            switch.denon_setfunc_dstv
          {% elif states.sensor.hifi_input_function.state == 'spotify' %}
            switch.denon_setfunc_spotify
          {% endif %}

Thank you

Change to this:

entity_id: >
  switch.denon_setfunc_{{ states('sensor.hifi_input_function') }}

Thank you Burningstone

It works.

What’s the difference between:

entity_id: >
  switch.denon_setfunc_{{ sensor.hifi_input_function.state }}

and

entity_id: >
  switch.denon_setfunc_{{ states('sensor.hifi_input_function') }}

At the risk of sounding flippant, the first one isn’t a valid template and the second one is.

3 Likes

You are missing states.xxx at the beginning of your template, but it’s better to use the states(xxx) notation as stated in the above link.

1 Like

To get the state value of an entity like sensor.whatever you can use this:

states('sensor.whatever')

or this:

states.sensor.whatever.state

but not this:

sensor.whatever

For more information see: Templating - States


Ninja’d by Burningstone …

1 Like