Try:
{{ state_attr('group.all_lights', 'entity_id') }}
Try:
{{ state_attr('group.all_lights', 'entity_id') }}
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') }} ]
Close, but:
options: "{{ state_attr('group.all_lights', 'entity_id') }}"
EDIT: Sorry doing this on my phone. Actually that might work the way you had it.
Sorry, still doesn’t work…
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.
Thanks pnbruckner, you are great!! But the thing is (i’m just starting with HA) that doesn’t work…
I put the script exactly as you described, and updated my automation this way:
- alias: Llenar lista para timers genericos
initial_state: 'on'
trigger:
platform: homeassistant
event: start
action:
- delay: 00:30
- service: python_script.update_timers_genericos
the input select is declared this way:
timer_generico7:
name: Timer generico 7 orden
options:
- Elegir
icon: mdi:clock
I put one option because if I don’t, I recive an error on check config…
Buy When I open the input select, I just have this initial option…
I tried @pnbruckner’s solution and it works great for me.
If you did this, the service should be:
python_script.update_timer_generico7
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.
the service named pyton_script is not listed in my services page. Should I eneble this in some way?
NOW IT WORKS!!! I had to enable python scripts from the configuration.yaml
THANKS A LOT!!!
Oops, yeah, sorry, forgot about that. Glad it’s working!
pnbruckner, can you tell me how the python script would be to show the friendly name instead of the entity id?
I was wondering if you’d want that.
group_entities = hass.states.get('group.all_lights').attributes['entity_id']
all_lights = []
for e in group_entities:
all_lights.append(hass.states.get(e).attributes['friendly_name'])
service_data = {'entity_id': 'input_select.timer_generico7',
'options': all_lights}
hass.services.call('input_select', 'set_options', service_data)
Note that this script has basically no error checking, so you might want to “beef it up” a bit.
Sorry, I’m a rookie. I continue seeing the entity id…
Sorry, I made a mistake…
IT WORKS!!!
you’re a genious!!, Thanks a lot!!!
Can I turn on a light using its friendly name intead of its entity_id?
Not directly. But you can do something like this:
- service: light.turn_on
data_template:
entity_id: >
{{ (states.light|selectattr('attributes.friendly_name','eq',
states('input_select.timer_generico7'))|list)
[0].entity_id }}
Thanks, I’ll Try as soon as I can