Input select labem / value pairs?

I have been looking around this forum and cannot find an elegant way to create a “Select” which has input/value pairs.

From programming I am used to separating how you show things (the label) from the actual value of a select. That way if you change the label, you don’t have to change underlying code. This does not seem to be possible in HA which, for me, is a weird design choice.

Does anyone have a working example of how they solved that issue? Or are you just using the labels and (hopefully) never changing them?

On the latest or greater 2021.9+, create a select template.

template:
- select:
  - name: xyz
    state: >
      {% set value = states('some.entity') %} # this will get the value that the label represents
      {% set mapper = {
         'value1': 'label1',
         'value2': 'label2',
          ... etc...
         } %}
      {{ mapper[value] if value in mapper else None }}
    select_option:
    - condition: template
      value_template: >
          {% set mapper = {
             'label1': 'value1',
             'label2': 'value2',
              ... etc...
              } %}
         {{ option in mapper }}
    - service: some.service
      target:
        entity_id: some.entity
      data:
        some_field: >
          {% set mapper = {
             'label1': 'value1',
             'label2': 'value2',
              ... etc...
              } %}
          {{ mapper[option] }}

if you provide more info, I can help you with a working version… not this mangled mess that has ‘fake’ information.

Your example demonstrates the usefulness of the new Template Select and also the need for “Template Variables” so that mapper can be defined just once.


EDIT

Just allowing for constants, or even ‘limited templates’, would be a big help. I’d settle for constants.

yep, not sure how the variables would work with triggers and multiple templates in the services though. I think that might be the roadblock. :man_shrugging:

EDIT: nevermind, overthinking it. The tempaltes in the services wouldn’t get resolved until the service was called.

actually… we can use variables for at least half of it.

template:
- select:
  - name: xyz
    state: >
      {% set value = states('some.entity') %} # this will get the value that the label represents
      {% set mapper = {
         'value1': 'label1',
         'value2': 'label2',
          ... etc...
         } %}
      {{ mapper[value] if value in mapper else None }}
    select_option:
    - variables:
        mapper:
          label1: value1
          label2: value2
          ...etc...
        valid: >
          {{ option in mapper }}
        value: >
          {{ mapper[option] if valid else '' }}
    - condition: template
      value_template: "{{ valid }}"
    - service: some.service
      target:
        entity_id: some.entity
      data:
        some_field: "{{ value }}"