Select template

hello,
I’m new in HA
I would like to have a list of item, provide by an entity.
I found i need to use template, but when I change the selected value, this not work, alway lost

  - select:
     - name: "Porch Camera Day-Night Mode"
       unique_id: ma_lis
       options: "{{ states.select.mon_domicile.attributes.options }}"
       state: "{{option | default(this.attributes.options[1]) }}"

       select_option:
        - service: select.select_option
          target:
            entity_id: select.porch_camera_day_night_mode
          data: 
            option : "Vacances"

I try with fixed value for exemple

Your template for the state option is self-referential. That isn’t a viable strategy.

I suggest you review this guide:

i think i would like to self-referential
i have a dynamic list of options, and when I select one of them, i would like the select keep this option

You already know how that behaves (the value is lost).

Refer to the examples in the link. They store the selected option in a helper.

yes, i use it and this work thanks !
I thought i’m stupid … but no, you want store … and i dont understood why.
the basic list can, but not the template
i lost many hours for this … :frowning:

  1. Select an option.
  2. Restart Home Assistant.
  3. Does your Template Select report the option you chose before restarting?

You can set it up to self reference for the state which will basically just give it an initial value…but for it to actually function, you also need to enable optimistic mode so that the state is updated by any selection events. As far as I know, the variable option is undefined outside of select_option, so there’s no point in using it in the state template:

- name: "Porch Camera Day-Night Mode"
  unique_id: ma_lis
  options: "{{state_attr('select.mon_domicile', 'options') }}"
  state: |
    {{ this.attributes.options[1]| default('No options available', true) }}
  optimistic: true
  select_option:
    - service: select.select_option
      target:
        entity_id: select.porch_camera_day_night_mode
      data: 
        option : "{{option}}"
  availability: "{{has_value('select.mon_domicile')}}"

IIRC, the state will not survive reload of the Template integration or restart of HA.
If you want to guarantee the state survives restart you should use an additional Helper.

1 Like

In my opinion, this is an important aspect for creating a viable Template Select. Losing its state, after a reload/restart, is likely to be problematic for many applications.

1 Like

Sorry, i forgot to respond
the 2 solutions help me :smiley:
make sometime somehting who dont need to be saved

thantks