Get entities as input_select & use entity from input_select

Ok, i want to be able to choose a light from a dynamically generated list of lights present, then have an automation use that entity I’d within itself. I think I can tackle the second part once I get there, but I’m trying to get the first half working.

I’m guessing I’ll need an input_select, and one automation to populate it, then one to use it, the first one might be as simple as using an automation triggers with a data_template that cycles through all of the available entities.

Does anyone have any thoughts?

Input selects can currently not be filled dynamically. They get their values at startup. That’s probably a show stopper for w/e you planned to do right there.

~Cheers

There’s a set_options call for it, so I think it can. I just need to figure out how to use a template for it.

You are absolutly right. My bad. Weird I remember looking that up and it was not possible…huh…

Well it should be doable then. What do you have so far? What exactly are you struggling with?

It seems you just need to create a list of strings and give that to the set_options service.

~Cheers

automation:
  alias: populate_list
  trigger:
    platform: homeassistant
    event: start
  action:
    - delay: 1:00
    - service: input_slider.set_options
    data_template:
      - entity_id: input_select.light_for_alarm
      - options: [ "{% for state in states.light -%} {{ state.entity_id }}, {% endfor %}"]

I’ve never used a data_template in an automation, and i’m struggling with the formatting.

I’m trying to create a package with a light-based alarm clock.

That will include the comma inside the quotes. You can test these things in the Templates screen

I was under the impression that data_templates on one line needed to be enclosed in single or double quotes, per https://home-assistant.io/docs/automation/templating/ “You must surround single-line templates with double quotes (”) or single quotes (’)."

if i take out the quotes, I get the following error:
found character ‘%’ that cannot start any token
in “/config/packages/alarm_clock.yaml”, line 61, column 21

I don’t think they were meaning to get rid of the quotes, I think they were pointing out that the formatting was off and that you needed to consider checking where both the quotes and commas were placed inside of your template.

Try something on this order. (Note - this is completely untested, but it follows the docs on how to set up the input_select utilizing the set_options)
"{% for state in states.light -%} "{{ state.entity_id }}", {% endfor %}"

I think this will get you closer to a working template to use in your automation.

Ok, i’m close, I have:
automation:
alias: populate_list
trigger:
platform: homeassistant
event: start
action:
- delay: 0:30
- service: input_select.set_options
data_template:
entity_id: input_select.light_for_alarm
options: ‘{% for state in states.light -%}"{{ state.entity_id }}"{% if loop.last -%} {% else %}, {% endif -%}{% endfor %}’

which gives me:
“light.dining_room_level”, “light.entryway_level”, “light.ge_plug_dimmer_level”, “light.hall_level”, “light.kitchen_level”, “light.lamp_bulb_1_level”, “light.master_bathroom_level”, “light.master_bedroom_level”, “light.master_closet_level”

all in one option (and in the template editor:
“light.dining_room_level”, “light.entryway_level”, “light.ge_plug_dimmer_level”, “light.hall_level”, “light.kitchen_level”, “light.lamp_bulb_1_level”, “light.master_bathroom_level”, “light.master_bedroom_level”, “light.master_closet_level”

But i can’t figure out how to put those on separate choices…

you could try the following for options:

options: >
{%- for state in states.light %}
  - {{ state.entity_id }}
{%- endfor %}

sadly that just puts it all in the same element in a different format:
- light.breakfast_nook_level - light.dining_room_level - light.entryway_level - light.ge_plug_dimmer_level - light.hall_level - light.kitchen_level - light.lamp_bulb_1_level - light.master_bathroom_level - light.master_bedroom_level - light.master_closet_level

hmm… make sure it is options: > and not options: >-

Adding - at the end of > removes any trailing spaces, which makes it into one line.

I copied and pasted your code exactly, it looks right in the template editor, but not in input_select.

if i past in th output of the template: [{% for state in states.light -%}"{{ state.entity_id }}"{% if loop.last -%} {% else %}, {% endif -%}{% endfor %}]
which is:
([“light.breakfast_nook_level”, “light.dining_room_level”, “light.entryway_level”, “light.ge_plug_dimmer_level”, “light.hall_level”, “light.kitchen_level”, “light.lamp_bulb_1_level”, “light.master_bathroom_level”, “light.master_bedroom_level”, “light.master_closet_level”])

and got the correct info showing up in the input_select, but i still can’t make it work as a data_template.

@Danielhiversen can you shed any light on what I’m doing wrong here?

Still haven’t gotten anywhere on this, I’m guessing I need to file a bug report.

Any luck? … I’m having the same problem? Could it be to do with the format of the envelope of the data