Input_select - select_option by index number

Hello!
Is there an easy way to select an option using an index number in an input_select?

Let’s say I have this code and I want to select “Adam” in an automation using the index number “2”.

input_select:
  who_cooks:
    name: Who cooks today
    options:
      - Eric
      - Anna
      - Adam
      - Steve

If given this:

input_select:
  example:
    name: Example
    options:
    - 'first'
    - 'second'
    - 'third'

it appears in Developer Tools > States like this:

Its options attribute contains a list of its options so I can reference any of them by index number:

What you cannot do is set the input_select’s current state value using an index number.

2 Likes

Thank you @123 Taras!

I want to use this for “bidirectional communication” between a modbus sensor and an input_select and it seems to work perfectly.
Here is my complete code:

input_select:
  heru_regulation_mode:
    name: Regulation mode
    options:
      - 'supply'
      - 'extract'
      - 'room'
      - 'extract s/w'
      - 'room s/w'
    icon: mdi:tune
modbus:
  - name: heru
    type: tcp
    host: 192.168.0.115
    port: 10502
    sensors:
      - name: TPHeruRegulationMode
      # 0 = Supply, 1 = Extract, 2 = Room, 3 = Extract S/W, 4 = Room S/W
        slave: 1
        address: 11
        input_type: holding
        count: 1
        scale: 1
        offset: 0
        precision: 0
        data_type: int
        scan_interval: 15
automation:
  - alias: 'Heru regulation mode'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_select.heru_regulation_mode
    condition: []
    action:
      - service: modbus.write_register
        data:
          hub: heru
          unit: 1
          address: 11
          value: "{{ state_attr(trigger.entity_id,'options').index(states(trigger.entity_id)) }}"
      - service: homeassistant.update_entity
        data:
          entity_id: sensor.tpheruregulationmode

  - alias: 'Heru regulation mode, Update input_select'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.tpheruregulationmode
    condition: []
    action:
      service: input_select.select_option
      target:
        entity_id: input_select.heru_regulation_mode
      data:
        option: "{{ state_attr('input_select.heru_regulation_mode','options') [states(trigger.entity_id) | int] }}"

2 Likes

I think I’m trying to do something similar in esphome but with no luck…
Esphome config file:

select:
  - platform: "tuya"
    tuya_id: "house"
    name: "ESPH House Power Select"
    enum_datapoint: 4
    options:
      0: P1-High
      1: P2-Med High
      2: P3-Med Low
      3: P4-Low

And in an automation I’m trying to set the Power Level as follows, but it doesnt seem to work.

service: select.select_option
data:
  option: 1
target:
  entity_id: select.esph_house_power_select

Any advice?
EDIT: I had to use the valu, not the index. Duh!