How to auto fill input_select?

I’m making a timer like this, I want it can let any entity auto turn off after countdown.
But now i have an issue. I can’t make input select auto fill.
I try to searching and I found this Fill Input select
But I don’t know how to use it. The python script is setup, but I don’t know how to let the input select use it.

Someone can help me? Thanks so much.

Did you remember to add an input select section to your configuration.yaml file?
Something like this:

input_select:
    activity_name:
        name: Friendly Name
        options:
            - PowerOff
            - Option1
            - Option2
            - Option3
            - etc....
         initial: PowerOff
         icon: mdi:remote

Where activity_name, Friendly Name and the icon is your choice as well as a valid list of activities.

Yes, I set input select ready.But i don’t really know how dose that python script work. Did I need add some variable in the input select options to let that python script fill it? Or I need to add all entity friendly name to options manual?

Hi! Sorry, I bookmarked your post when you first asked for help with this (in the other topic) but haven’t gotten around to going back through all the posts I bookmarked yet! :slight_smile:

Anyway, the idea is to create an automation that will call the script when HA starts so that it can populate the list of options in the input_select. Then you’ll need to customize the script for your particular situation.

Do you really want all entities to be put in the list, or just entities of certain types (such as switches, lights, etc.)?

The automation would be like this:

- alias: Populate input_select.entities options
  trigger:
    platform: homeassistant
    event: start
  action:
    service: python_script.update_entities

You need to add this to configuration.yaml to enable use of python_scripts:

python_script:

Then you create a directory in your config directory (i.e., the directory that contains configuration.yaml) named python_scripts. Then you put a file in there named update_entities.py, with this content (assuming you do want all entities):

entities = hass.states.entity_ids()
service_data = {'entity_id': 'input_select.entities', 'options': sorted(entities)}
hass.services.call('input_select', 'set_options', service_data)

If you want some subset of entities, just let me know what you want. Also, this populates the input_select options list with entity_id’s. If you’d rather use friendly_name’s, just let me know that, too.

1 Like

atpnb, your reply give me so much more helps! Thanks you so much. You are a genius.

Now I’m trying to finish my general timer and I think I have a last question. I can’t use variable in automation conditions, like this.

    condition:
      - condition: state
        entity_id: ">
          {{ (states|selectattr('attributes.friendly_name','eq',
                states('input_select.general_timer_entities'))|list)
             [0].entity_id }}"

Or this

    condition:
      - condition: state
        entity_id: >
          "{{ (states|selectattr('attributes.friendly_name','eq',
                states('input_select.general_timer_entities'))|list)
             [0].entity_id }}"

You can see I trying to put the double quotation marks on different place, but error report still appear.

Invalid config for [automation]: Entity ID > {{ (states|selectattr('attributes.friendly_name','eq', states('input_select.general_timer_entities'))|list) [0].entity_id }} is an invalid entity id for dictionary value @ data['condition'][0]['entity_id']. Got None
extra keys not allowed @ data['condition'][0]['for']. Got None
extra keys not allowed @ data['condition'][0]['state']. Got None
not a valid value for dictionary value @ data['condition'][0]['condition']. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/automation/

It’s that mean the automation conditions can’t use variable so complex like this?

To use a template in a condition you need to use a template condition, not a state condition.

Sorry for my dumb, I don’t relly know the variable.

My original condition is this.

    condition:
      - condition: state
        entity_id: light.ke_ting_she_deng
        state: 'off'
        for:
          seconds: 10

I don’t know how to let value_template have the same function. ( stay off for 10s )

And I try to use a template sensor for temporary storage entity_id, but state condition still can’t use it

sensor:
  - platform: template
    sensors:
      input_select_entity_id:
        friendly_name: entityid
        value_template: >
          {{ (states|selectattr('attributes.friendly_name','eq',
                states('input_select.general_timer_entities'))|list)
             [0].entity_id }}
             
automation: 
    condition:
      - condition: state
        entity_id: "{{ states.sensor.input_select_entity_id.state }}"
        state: 'off'
        for:
          seconds: 10

Sorry again for the delayed response!

Did you get this working yet?

FWIW, I think this might work for you:

condition:
  condition: template
  value_template: >
    {% set state = (states|selectattr('attributes.friendly_name','eq',
                   states('input_select.general_timer_entities'))|list)[0] %}
    {{ state.state == 'off' and
       as_timestamp(now()) - as_timestamp(state.last_changed) > 10 }}

Hello, I am a beginner and I thank you for your explanations. I would like to advance in the understanding of your script, wanting to refine the list (for example get the friendly_name).
Can you tell me where I can find information on the variables you are transmitting to the py script ? where do we find this information?
service_data ?
hass.services.call(‘input_select’ ?