Input Select - Set Options using templates

I am having a problem with the input_select.set_option service.

I have a number of announcements that my system can call upon and I want to have an input_select populated at HA Start with those announcement names.

I create a list of announcements to use in the input_select.set_options like this:

      - service: input_select.set_options
        data_template:
          entity_id: >
            input_select.announcements
          options: >
            {% for script in states.script if 'script.announcement_' in script.entity_id %}
              {%- if loop.first %}
                {{ '["- Select Announcement -", '}}
              {%- endif %}
              {%- set entity = script.entity_id | replace('script.announcement_', '') %}
              {%- set entity = entity | replace('_', ' ') | title -%}
              "{{- entity -}}
              {% if not loop.last -%}
                {{ '", ' }}
              {%- else -%}
                {{ '"]' }}
              {% endif %}
            {%- endfor %} 

Which to save you working it out creates something in this format:

["- Select Announcement -", "Announcement name 1", "Announcement name 2", ... "Announcement name n"]

This fails because the resultant string is more than 255 characters. (The second last line of the error output looks a bit odd to me… f"Invalid state )

Error doing job: Exception in callback InputSelect.async_set_options(options=['{% for scrip... endfor %} \n'])()
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/src/homeassistant/homeassistant/components/input_select/__init__.py", line 281, in async_set_options
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 297, in async_write_ha_state
    self._async_write_ha_state()  # type: ignore
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 405, in _async_write_ha_state
    self.entity_id, state, attr, self.force_update, self._context
  File "/usr/src/homeassistant/homeassistant/core.py", line 977, in async_set
    state = State(entity_id, new_state, attributes, last_changed, None, context)
  File "/usr/src/homeassistant/homeassistant/core.py", line 721, in __init__
    f"Invalid state encountered for entity id: {entity_id}. "
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: input_select.announcements. State max length is 255 characters.

However,

If I use the exact same template as above in the Dev Template Tool to create the same string and use it in the Dev Tools Services it works fine. So the 255 limit seems to be selective!

As an illustration of it working with a string longer than 255 chars:

image

I have also tried publishing it to MQTT and using the trigger.payload but that doesn’t work either.

On the one hand I can’t believe no one has tried this before so it must be possible and I must be doing something wrong, but on the other hand I think I have found a bug somewhere…

Any ideas?

I don’t understand why this should be expected to work at all. The options parameter expects a list but a template always outputs a string (even when it looks like a list).

Well that might explain it…

Does that mean what I want to do is not possible?

It’s possible to use templates to define each individual item in the list.

options:
  - {{ template1 }}
  - {{ template2 }}
  - {{ template3 }}

However that won’t help you because it requires that you know how many items are needed in advance.

I am surprised this doesn’t seem possible.
I would have thought the main reason to use set_option would be to populate the list at startup

If you review the example shown in the documentation for input_select.set_options, the list is hard-coded and not generated by a template (for the reason I explained above).

# Example configuration.yaml entry
automation:
  - alias: example automation
    trigger:
      platform: event
      event_type: MY_CUSTOM_EVENT
    action:
      - service: input_select.set_options
        data:
          entity_id: input_select.who_cooks
          options: ["Item A", "Item B", "Item C"]

It does accept a list. The issue is a result of any template can only be a string. And as this single string will be used as a state of your input_select, we’re arriving to the 255 characters limit on states.
Fill in your input_select individually.

@klogg

Search the community forum for “set_options” and you’ll get many posts all reporting the same limitation. Here’s one:

It describes a workaround using a python_script. Basically, you create a python_script that sets the input_select’s options and then call that python_script when needed.

Thanks.
I’ll see if I can adapt it…