I’m trying to figure out how to get a template to produce a list that can be consumed as a list by a service call. Specific case right now is to enumerate a list of scenes, and send that list to an input_select as a list of available options
Currently, this is what I have:
- service: input_select.set_options
data_template:
entity_id: "input_select.hasp_plate01_page1button4scene"
options: "[{% for item in states.scene %}'{{ item.entity_id }}'{% if not loop.last %}, {% endif %}{% endfor %}]"
That template renders as ["scene.daylight", "scene.lights_off", "scene.lights_on", "scene.night"] which, if pasted into the service call, works like expected. However, I’m pretty sure it’s just rendering as a long string and I think I need it to somehow be returning a list.
I don’t remember all details and not sure if it is applicable in your case but some used from_json and to_json to pass a lists (or dict?) between automations.
FWIW, even if there were a way to write that template, your example contains a syntax error that would prevent the end-result from being interpreted as a list.
options: "[{% for item in states.scene %}'{{ item.entity_id }}'{% if not loop.last %}, {% endif %}{% endfor %}]"
You’ve wrapped the whole thing in double-quotes, thereby guaranteeing it will be interpreted as a string.
It should look like this:
options: ["{% for item in states.scene %}'{{ item.entity_id }}'{% if not loop.last %}, {% endif %}{% endfor %}"]
To be clear, that still won’t work (because of the reasons already given) but at least that’s the proper syntax.
For example, this works (but doesn’t meet your requirement of being dynamically generated).
@luma I think you need to look at your task from a different angle. Don’t know all the details, but a python script can easily fill in an input_select with whatever you want, just decide on when and with what.