How can I set the entity_id with an input_select?

Hi,

I have been trying to set my switches directly from an input_select …

from input_select.yaml

rowentafan:
  name: RowentaFan
  options:
       - 'switch.fan_rotate'
       - 'switch.fan_timer'
       - 'switch.fan_pulsewave'
       - 'switch.fan_speed'
       - 'switch.fan_power'
  initial: 'switch.fan_speed'
  icon: mdi:airplay  

from automation.yaml

 - alias: Rowenta Fan Commands
   trigger:
      - platform: state
        entity_id: input_select.rowentafan
   action:
      - service: switch.turn_on
        entity_id: "{{ trigger.to_state.state }}"

However I get …
2017-07-18 23:17:22 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: Entity ID {{ trigger.to_state.state }} is an invalid entity id for dictionary value @ data[‘action’][0][‘entity_id’]. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/automation/

Anyone have any ideas how this can be accomplished?

You need a data_template.
Look at the last example on this page ;

1 Like

Hmm I’m not sure that would work here as the entity_id is known but with mine it’s determined by the input_select, I’m not setting an attribute of an entity_id, I’m trying to turn on a switch using an input_select, so I don’t think a template will work in this case.

Try this:

- alias: Rowenta Fan Commands
   trigger:
      - platform: state
        entity_id: input_select.rowentafan
   action:
      - service: switch.turn_on
        data_template:
          entity_id: "{{ trigger.to_state.state }}"
2 Likes

Many Thanks treno, you put me on the right track tho’ I suspect it should be easier :stuck_out_tongue:

working config…

 - alias: Rowenta Fan Commands
   trigger:
      - platform: state
        entity_id: input_select.rowentafan
   action:
      - service: switch.turn_on
        data_template:
          entity_id: >
            {% if   is_state('input_select.rowentafan', 'fan_rotate') %}
              switch.fan_rotate
            {% elif is_state('input_select.rowentafan', 'fan_timer') %}
              switch.fan_timer
            {% elif is_state('input_select.rowentafan', 'fan_pulsewave') %}
              switch.fan_pulsewave
            {% elif is_state('input_select.rowentafan', 'fan_speed') %}
              switch.fan_speed
            {% elif is_state('input_select.rowentafan', 'fan_power') %}
              switch.fan_power
            {% endif %}

This method is OK for 5 commands but when faced with 15 or 20 for led bulbs for instance it’s a bit of a PIA. I’ll give danichispa’s idea a go later to see if that will work, tho’ I did try it without the data_template bit, so maybe that’s all I needed, I’l report back later :slight_smile:

2 Likes

Hi @keithh666,

I did something along these lines. Have a look here: