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