Hi, I’m trying to make an automation, so when HA starts, it fills an option select with all the entities that belongs to a group. I’m trying with the lights_all group, but it cloud be anyone. The idea is that when I include an entity in this group, it automatically appears (after reboot) in all the input selects that I want .
This is my automation. How should I ise this?
- alias: Llenar lista para timers genericos
trigger:
platform: homeassistant
event: start
action:
- delay: 00:30
- service: input_select.set_options
entity_id: input_select.timer_generico7
data_template:
options: [ {{ state_attr('group.all_lights', 'entity_id') }} ]
Yeah, sorry. I’m back at a computer and I’ve been trying, but I think this is one of those, “sorry, you just can’t do this with Jinja” things. At least not directly like this. The problem is, at least as I understand it, a Jinja template will output just one string, no matter what you try to do.
You might be able to do this with a Python Script, though.
Ok, below is a script that should do what you want. It can be enhanced to take variables like the name of the group and the name of the input_select, but for now it’s hard coded for the scenario described above.
Copy the following code into a file in your <config>/python_scripts directory. (Create the directory if necessary.) The file name should have a .py extension. The service you will need to call is python_script.xxx where xxx is the name of the script. So, e.g., if you put this code in <config>/python_scripts/update_timer_generico7.py, then the service will be named python_script.update_timer_generico7.
Here is the code:
group_entities = hass.states.get('group.all_lights').attributes['entity_id']
all_lights = []
for e in group_entities:
all_lights.append(e)
service_data = {'entity_id': 'input_select.timer_generico7',
'options': all_lights}
hass.services.call('input_select', 'set_options', service_data)
The options will be the entity_id’s of all the lights that exist. If you’d rather the options be the friendly_name’s of the lights, let me know.
Per @VDRainer, make sure the name of the service and the name of the script match.
Also, you either need to restart HA, or at least, run the python_script.reload service via the Services page, after you create the python_scripts/xxx.py file.
It should work - I even tested it with an input_select I created just for the test before I provided the solution.