Template Select Entities

Template Selects create an entity that acts like an Input Select Helper in the front end, but more like an automation behind the scenes. Their options list can be either static or dynamically generated by a template. Often, a helper entity will be required to store the state value for the Template Select. Either Input Text or Trigger-based Template Sensors can be used for this purpose.

Template Selects in place of an automation

This is the most general use type for a Template Select.

Example: Source Selector for Legacy equipment

In the following example, a Select is used to switch sources on an old stereo receiver. The select_option sequence shown below saves the selected value to an Input Text helper and passes it along to a script that handles sending IR commands to the stereo receiver.

template:
  - select:
      - name: "RCA Stereo Source"
        unique_id: rca_stereo_source_select
        state: "{{ states('input_text.rca_receiver_output') }}"
        options: "{{ ['Cable', 'CD','AUX', 'AM/FM', 'DVD'] }}"
        select_option:
          - service: input_text.set_value
            target:
              entity_id: input_text.rca_receiver_output
            data:
              value: "{{ option }}"
          - service: script.rca_receiver_send_command
            data:
              command: "{{ option }}"
              repeats: 2
        availability: "{{ states('input_text.rca_receiver_output') is defined }}"
Example: Entraining Booleans / Only one "on" at a time

In this example the options are automatically populated with the entity IDs of all input_boolean entities that match a specific naming scheme. The state and options are presented in the form of entity IDs. The select_option sequence turns the selected entity ‘on’. Then, all the non-selected booleans are turned ‘off’.

template:
  - select:
      - name: "Partially Entrained Booleans"
        state: >-
          {{ (expand(this.attributes.options
          | select('is_state', 'on')
          | list) | sort(attribute='last_changed', reverse=1)
          | map(attribute='entity_id')|list)[0] or
          (expand(this.attributes.options) | sort(attribute='last_changed', reverse=1)
          | map(attribute='entity_id')|list)[0]}}
        select_option:
          - service: input_boolean.turn_on
            target:
              entity_id: "{{ option }}"
          - variables:
              boolean: |
                {{ this.attributes.options |reject('eq', option)|list}}
          - service: input_boolean.turn_off
            target:
              entity_id: '{{ boolean }}'
        options: >
          {{ states.input_boolean
          | selectattr('object_id', 'match', 'test_bool_')
          | map(attribute='entity_id')
          | list }}

Proxy/Translate an Existing Select or Input Select entity

Sometimes you need to “edit” the option list of a Select entity that was created by another integration.

  • If the options are not labelled in a way that you like.
  • If the option names are not easily understood by people in your household. For example, some HVAC integrations will have numeric options. It’s much easier to work with normal mode names rather than trying to remember that 101 is “off”, 102 is “heat”, and 121 is “cool”…
  • If the list includes options that you want to exclude.

Or, you may live in a bilingual household, and want to have a version in each language, but keep their states entrained.

Example: Translating Options
template:
  - select:
      - name: "Color Picker"
        unique_id: seleccionador_de_colores_translated_to_english
        state: >
          {% set opt = states('select.seleccionador_de_colores') %}
          {% set mapper = {
            'Rojo': 'Red',
            'Azul': 'Blue',
            'Verde': 'Green',
            'Amarillo': 'Yellow',
            'Blanco': 'White'
          }%}
          {{ mapper.get(opt) }}
        options: "{{['Red','Blue','Green','Yellow','White']}}"
        select_option:
          - service: select.select_option
            data:
              option: >
                {% set mapper = {
                  'Red': 'Rojo',
                  'Blue': 'Azul',
                  'Green': 'Verde',
                  'Yellow': 'Amarillo',
                  'White': 'Blanco'
                }%}
                {{ mapper.get(option) }}
            target:
              entity_id: select.seleccionador_de_colores
        availability: "{{ state_attr('select.seleccionador_de_colores', 'options') is defined }}"

Area-based Scene Selector

The following examples show two ways to create a selector for all the scenes assigned to a given Area. The state and options are presented in the form of friendly names for UI usability. In both cases, selecting a scene activates that scene.

Helper-free Version
template:
  - select:
      - name: "Basement Scenes"
        state: >
          {% set area = "Basement" %}
          {{ 
            expand(area_entities(area))
              | selectattr('domain', 'eq', 'scene')
              | sort(attribute='state', reverse=true)
              | map(attribute='name')
              | list
              | first
          }}
        options: >
          {% set area = "Basement" %}
          {{
            expand(area_entities(area))
              | selectattr('domain', 'eq', 'scene')
              | map(attribute='name')
              | list
              | sort
          }}
        select_option:
          - service: scene.turn_on
            target:
              entity_id: >
                {{
                  states.scene
                    | selectattr('name', 'eq', option)
                    | map(attribute='entity_id')
                    | first
                }}

This method was originally posted by @TheFes in another thread

Using a trigger-based Template sensor as the helper entity
template:
  - select:
      - name: "Basement Scenes"
        state: "{{ states('sensor. basement_scenes_helper') }}"
        options: >
          {{ expand(area_entities(area)) | selectattr('domain', 'eq', 'scene')
          | map(attribute='name') | list }}
        select_option:
          - variables:
              entity: >
                {{ states.scene | selectattr('name', 'eq', option) | map(attribute='entity_id') | join }}
          - event: custom_template_select
            event_data:
              target: basement_scenes
              value: "{{ option }}"
          - service: scene.turn_on
            target:
              entity_id: "{{ entity }}"
  - trigger:
      - platform: event
        event_type: custom_template_select
        event_data:
          target: basement_scenes
    sensor:
      - name: Basement Scenes Helper
        unique_id: basement_scenes_helper_0001
        state: "{{ trigger.event.data.value }}"

Merged Options

This example shows how to effectively combine two Select or Input select helpers so you can have a single selector on your dashboard:

Merging Audio Source Selectors
template:
  - select:
      - name: "Merged Audio Selects"
        state: >
          {{expand('input_select.radio_stations','input_select.music_genres')
          | sort(attribute='last_changed', reverse=true)
          | map(attribute='state')
          | list | first}}
        options: >
          {{ state_attr('input_select.music_genres', 'options')
          + state_attr('input_select.radio_stations', 'options') }}
        select_option:
          - service: input_select.select_option
            target:
              entity_id:
                - input_select.music_genres
                - input_select.radio_stations
            data:
              option: "{{ option }}"

Call for Assistance/Submissions:

If you run across a use case or example that you feel is especially helpful or doesn’t fit any of the sections above, please add it to this post along with a brief explanation of what you use it for.

While they are technically configurable, I have been unable to create a functional and reliable trigger-based Template Select. It might not be possible. If you have been able to create one that works reliably, feel free to share it here or send me a private message with the configuration.


Home Assistant Docs - Template Integration - State-based - Select

The Home Assistant Cookbook - Index

10 Likes

Is there a way to have a self contained template select?

Say I have ['opt1', 'opt2', 'opt3']

I select opt3

So the state would be opt3 and the options would be ['opt3', 'opt1','opt2']

Or even keep the options list static?? Not sure.

Something like…

template:
  - select:
      - name: "my_template_select"
        state: >-
          {{ this.attributes.options | list | first }}
        select_option: >
          {{ move the selected option to the first index here }}
        options: >
          {{ this.attributes.options }}

Your question is a bit confusing… one of the major points that Template selects are meant to address is when you can’t say what options you have. The example configuration you have provided, with static options, is just an over-complicated Input Select.

If you look in the Area-based Scene Selector section above, you can see an example of the closest thing to a self-contained option that I know of… though it is not exactly self-contained. There are a couple issues with creating something like what you have described.

Issue #1: Unlike input_select, the select domain doesn’t currently have a set_options action, so there’s no current way to “move the selected option to the first index”. Such an action could have serious negative impacts if used on select entities from integrations other than Template, so I doubt we will ever see it in core. This might be something that Frenck would consider adding to Spook… but AFAIK it doesn’t exist currently.

Issue #2: You would have to at least start with a static list to define options. In the YAML only days that would mean that the value of options would set back to the original values after restart… I’m not familiar enough with how the UI config works under the hood to say how it would work for a select configured through the Template Helper UI.