Template Select Entities

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

Template Selects in place of an automation

This Template Select is used to switch sources on an old stereo receiver. The select_option sequence 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 }}"

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.

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 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 turns it on.

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 }}"

Partially Entrained Booleans

The following auto-populates a selector with all Input booleans 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 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 }}

Merged Options

If you have two Input select helpers that already have a number of automations based on them, but you want to combine them so there’s only one selector on your dashboard:

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:

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 please add it to this post along with a brief explanation of what you use it for or send me a private message with the configuration.


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

The Home Assistant Cookbook - Index

9 Likes