How to use an input_select value to select one switch

Hi all,

I need you help for one of my automation. I would like to use the value of an “input_select” in order to select one specific switch. To do that I created one “input_select” (name: heating_mode_week) with the following list: week, week with lunch, work at home, away.
For each choice, I would like to associate one specific switch (which is a scheduler switch).

I tried the code below, but it didn’t work.

Could you please help me to solve this ?

Thank you.
Smilorel

- alias: « week_selection »
trigger:
platform: state
entity_id: input_select.heating_mode_week
action:
service: switch.turn_on
target: >
{% if states.input_select.heating_mode_week == « week » %}
entity_id: switch.heating_week
{% elseif states.input_select.heating_mode_week == « Week with lunch » %}
entity_id: switch.heating_week_lunch
{% elseif states.input_select.heating_mode_week == « Work in home » %}
entity_id: switch.heating_week_lunch_workinhome
{% elseif states.input_select.heating_mode_week == « Away » %}
entity_id: switch.heating_away
{% else %}
entity_id: switch.heating_week
{% endif %}

I think the following is similar to what you are trying to achieve, in this case I have an input_select that has a number of lighting scenes that I can cycle up/down with a Zigbee remote control (there is another automation for the remote that uses input_select: next/previous to do this).

alias: Kitchen Light Scenes
description: Turn On Kitchen Scene Based on Input Select
trigger:
  - entity_id: input_select.kitchen_scenes
    platform: state
condition: []
action:
  - data_template:
      entity_id: scene.{{trigger.to_state.state}}
    service: scene.turn_on
initial_state: "on"
mode: queued
max: 5

In the action you could try changing the entity_id to switch.{{trigger.to_state.state}} and the service to switch.turn_on to see if that works for you.

In this case it won’t work because the input_select’s value doesn’t match the desired switch’s object_id. For example, ‘Week with lunch’ is not the desired switch’s ‘heating_week_lunch_workinhome’.

You can take advantage of what Jonah1970 has suggested if you rename your switches to match the values in your input_select.

Input Select options:

Week
Week with lunch
Work in home
Away

Switches:

switch.heating_week
switch.heating_week_with_lunch
switch.heating_work_in_home
switch.heating_away

Then your automation can simply be this:

- alias: Week selection
  trigger:
    - platform: state
      entity_id: input_select.heating_mode_week
  condition: []
  action:
    - service: switch.turn_on
      target:
        entity_id: 'switch.heating_{{ trigger.to_state.state | slugify }}'

Many thanks @123 and @Jonah1970 .
I will try this solution that could satisfy me.

Nevertheless, if someone knows a solution in order to use a “if structure”, It could be interesting.

smilorel

- alias: Week selection
  trigger:
    - platform: state
      entity_id: input_select.heating_mode_week
  condition: []
  action:
    - service: switch.turn_on
      target:
        entity_id: >
          {% if trigger.to_state.state == 'week' %}
             switch.heating_week
          {% elif trigger.to_state.state == 'Week with lunch' %}
             switch.heating_week_lunch
          {% elif trigger.to_state.state == 'Work in home' %}
             switch.heating_week_lunch_workinhome
          {% elif trigger.to_state.state == 'Away' %}
             switch.heating_away
          {% else %}
             switch.heating_week
          {% endif %}

If you compare this to the previous suggestion, you can see it’s much longer because it has to explicitly specify each one of input_select’s values.

Thank you @123 .
Everything is working fine and I learnt a lot with your answer.

I also posted your answer on the french home assistant community : Gestion de 4 input_boolean avec un seul choix possible sur ces 4 - #7 par aurelb87-smilorel - Entraide Home Assistant - Home Assistant Communauté Francophone

Here’s another way to do the same thing.

Instead of using a long chain of if-elif statements, it uses a dictionary. The variable values is a dictionary containing your input_select’s options and their corresponding text strings. The template uses the dictionary to get the matching text string.

- alias: Week selection
  trigger:
    - platform: state
      entity_id: input_select.heating_mode_week
  condition: []
  action:
    - service: switch.turn_on
      target:
        entity_id: >
          {% set values = {'week': 'week', 'Week with lunch': 'week_lunch',
            'Work in home': 'week_lunch_workinhome',  'Away': 'away'} %}
          switch.heating_{{ values.get(trigger.to_state.state, 'week') }}
1 Like

Ok thank you. Very interesting all the way to reach the solution.

Nevertheless, I have also to manage a switch.turn_off action: it works fine if I have only one switch but if I have more, it doesn’t work :

- service: switch.turn_off
      target:
        entity_id: >
          {% if trigger.to_state.state == 'week' %}
            switch.heating_week_with_lunch
            switch.heating_week_work_in_home
            switch.heating_away

Your original requirement was to turn off a single switch (for each selected option). Is this a new requirement to turn off multiple switches?

New requirement no… But, yes, when I posted my question I was only focus to find a solution to turn_on one switch from an input_select. But, as I have to selection only one switch, I have to turn_off the others…

I found one solution that it works fine for me: we have to add a turn_off service before the turn_on service.

- service: switch.turn_off
      target:
        entity_id:
            switch.heating_week
            switch.heating_week_with_lunch
            switch.heating_week_work_in_home
            switch.heating_away