Template Switch with Input Select input Not Working - Icon and State Selection

Hi,

I’m hoping to use a template switch to control and indicate my room scenes, i.e. select this scene at between time sunrise and sunset, but can get it to work and looing for some help?

I’m looking to change the icon and state (i.e. on/off) of the switch based on a input_select, but I can’t get:

  1. The state to chnage from off and;
  2. The icon only works for full, but seems to default to unknown for all the other options.
input_select:
  snug_scene:
    options:
      - full
      - relaxed
      - tv
      - night
      - switched_off
switch:
  - platform: template
    switches:
      snug_button:
        value_template: >
          {% if is_states("input.select.snug_scene", "switched_off") %} 
            'off'
          {% else %}
            'on'
          {% endif %}
        icon_template: >
          {% if is_state("input_select.snug_scene", "full") %}
            mdi:weather-sunny
          {%-elif is_state("input.select.snug_scene", "relaxed") %}
            mdi:candle
          {%-elif is_state("input.select.snug_scene", "tv") %}
            mdi:television
          {%-elif is_state("input.select.snug_scene", "night") %}
            mdi:weather-night
          {%-elif is_state("input.select.snug_scene", "switched_off") %}
            mdi:lightbulb-outline
          {% else %}
            mdi:cloud-question
          {% endif %}

I’ve tried the following combinations in the above code, with no success:

        value_template: >
          {% if is_states("input.select.snug_scene", "switched_off") %} 
            'off'
          {% else %}
            'on'
          {% endif %}

        value_template: "{{ not is_states("input.select.snug_scene", "switched_off") }}"
        icon_template: >
        icon_template: ->
        icon_template: >-
          {% elif ...
          {%-elif ...

Any help would be greatfully received.

Cheers

Change this:

        value_template: >
          {% if is_states("input.select.snug_scene", "switched_off") %} 
            'off'
          {% else %}
            'on'
          {% endif %}

To:

        value_template: >
          {{ is_state("input.select.snug_scene", "switched_off") }} 

The value template needs to evaluate to true or false, not the strings “on” or “off”.

Also in your icon template put a space between the dash and elif {%- elif... or just replace the dashes with spaces. They are not needed.

1 Like

Hi Tom,

Thank you for the reply, unfortunately both don’t work.

The state is always off and only “full” works.

See the code below - have I missed someting?

Cheers

switch:
  - platform: template
    switches:
      snug_button:
        value_template: >
          {{ is_state("input.select.snug_scene", "switched_off") }}
        icon_template: >
          {% if is_state("input_select.snug_scene", "full") %}
            mdi:weather-sunny
          {%- elif is_state("input.select.snug_scene", "relaxed") %}
            mdi:candle
          {%- elif is_state("input.select.snug_scene", "tv") %}
            mdi:television
          {%- elif is_state("input.select.snug_scene", "night") %}
            mdi:weather-night
          {%- elif is_state("input.select.snug_scene", "switched_off") %}
            mdi:lightbulb-outline
          {% else %}
            mdi:cloud-question
          {% endif %}
switch:
  - platform: template
    switches:
      snug_button:
        value_template: >
          {{ is_state("input.select.snug_scene", "switched_off") }}
        icon_template: >
          {% if is_state("input_select.snug_scene", "full") %}
            mdi:weather-sunny
          {% elif is_state("input.select.snug_scene", "relaxed") %}
            mdi:candle
          {% elif is_state("input.select.snug_scene", "tv") %}
            mdi:television
          {% elif is_state("input.select.snug_scene", "night") %}
            mdi:weather-night
          {% elif is_state("input.select.snug_scene", "switched_off") %}
            mdi:lightbulb-outline
          {% else %}
            mdi:cloud-question
          {% endif %}

If you look at the docs you will see that the turn on and turn off actions are required. You have not supplied these.

Hi Tom,

Sorry, I didn’t send you the whole code, but please see below.

I use switch.momentary_test for development.

Cheers

switch:
  - platform: momentary
    name: test
    mode: on
    toggle_for: 1
    cancellable: False
  - platform: template
    switches:
      snug_button:
        value_template: >
          {{ is_state("input.select.snug_scene", "switched_off") }}
        icon_template: >
          {% if is_state("input_select.snug_scene", "full") %}
            mdi:weather-sunny
          {%- elif is_state("input.select.snug_scene", "relaxed") %}
            mdi:candle
          {%- elif is_state("input.select.snug_scene", "tv") %}
            mdi:television
          {%- elif is_state("input.select.snug_scene", "night") %}
            mdi:weather-night
          {%- elif is_state("input.select.snug_scene", "switched_off") %}
            mdi:lightbulb-outline
          {% else %}
            mdi:cloud-question
          {% endif %}
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.momentary_test
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.momentary_test

That’s not a template switch it appears to be a custom momentary switch.

Hi Tom,

It is, but the template switch is snug_button.

I thought template switch on/off actions could be linked to any entity?

I’m eventually planning to link the on to a automation which selects the scene based on the sun/time and the off to the same automation to select switched_off.

Cheers

Neil

Just use the input select in the automations directly. Either as triggers or conditions.

Hi Tom, can HA be setup to change icon based on the selected option with the input select’s?