Numeric input_select state with human-readable labels?

wasn’t sure how to build the topic to contain my idea, but here’s the explaination:

I use quite a few input_select components to keep “settings” of my hass.io - different people preferences about notifications, specific scenes for rooms, and planned sprinkler integration. I want to keep the selects as readable for every user as possible, so couple of them are not only 1-word or number, but like a small sentences [for example one of the options for notifications: “Get all important, but none at home” etc.]. is there a way to create numeric aliases for the options to use them in conditions or while setting them via scripts/automations? I’m starting to think that words are confusing my automations & want to make it easier at the yaml-side, but keep informative for humans.

in simple words: I’d love to have a possibility to check input_selects in conditions like “if input_selects.week_option > 3” but have that options visible to user as words, not numbers. that way I could also set input_select.week_option to, let’s say, 4, and that would mean that users will see “thursday” as selected setting*.

*disclaimer: week days are only as an example, I know I can number them with now() function :wink: that’s not the case :slight_smile:

What you want is possible, but requires work.

You’ll need to have the following code in all your templates:

Automations where you aren’t using trigger object (trigger.to_state… etc)

{% set option_number = state_attr('input_select.xxx','options').index(states('input_select.xxx')) %}

Automations where you are using the trigger object

{% set option_number = state_attr(trigger.entity_id,'options').index(states(trigger.entity_id)) %}

The output of these templates is an index position of your input_selection. Remember list indexes start at zero.


Example

These are the options in your input_select.xxx

  • Item A
  • Item B
  • Item C
  • Item D

If your input_select.xxx is set to Item A, your output_number from the template contain a value of 0 after executing the template line supplied above.

If your input_select.xxx is set to Item B, your output_number from the template contain a value of 1 after executing the template line supplied above.

If your input_select.xxx is set to Item C, your output_number from the template contain a value of 2 after executing the template line supplied above.

If your input_select.xxx is set to Item D, your output_number from the template contain a value of 3 after executing the template line supplied above.

So, pretty much your selection option order is what drives what number you get from that template line.

4 Likes

looks good, thank you!

that definitely will work for attribute read and will make some of my conditions easier to grasp :slight_smile:
I guess that the opposite - selecting an option via automation/script based on number (“select 1” which would on fact select the “Item B” selection) is not possible?

No it is:

state_attr('input_select.xxx','options')[0]

would be Item A etc

great!

thank you so much!
I’m opening notepad++ and starting to rewrite my automations now :slight_smile:

1 Like

@pejotigrek any chance that you can paste an example of your automation? I’m trying to do the same with my solax inverter states and I’d love to have a way to decode/encode the human readable inverter state with the [0,1,2,3] operation code needed to set the state.

Post the service call that you’ll be using to convert the options, and post what each option should mean.

@luismsousa I guess my automation won’t help you as it was altered maaaaany times after this post :wink: but here’s what you can try in your automation, assuming, that it is triggeredy by input_select’s option change:

- service: <state change service>
  target:
    entity_id: <entity to change the state in>
  data:
    state: {{ state_attr(trigger.entity_id,'options').index(states(trigger.entity_id)) }}

Another option would be to use a Template Select.