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:
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?