I’m trying to do something with my lights. I’ve several lights in my HA, and I would like to change the color of all those lights at the same time (using any trigger) but only for lights that are already on.
Can some help me on how to do it in HA ? Or point at me some documentation or forum thread that could help me to acheive this ? I’m a newbie in automations…
What you probably want to do is create an automation or script that calls the light.turn_on command with the specified colors, and use a data_template to create a list of the entity_id’s of the lights that are currently on. This would be an if statement (if the light is on) inside a for loop for all entities in the light domain.
Under developer tools on the front end, under templating, there is an example that iterates through sensors in a similar way. The templating page will be crucial to figuring this out.
I can provide more examples once I’m on my pc, but this should get you started!
I think @Dolores is more close to that I want to do because I can’t create a group dynamically. Lights that will be on or off at a specific time will not be the same during a day.
@Dolores can you post some examples when you have time, please ?
I took a quick stab at this this morning. This is as close as I got, but is not working yet. I’ll take a look after work today, but here’s my stab at the data_template in case anyone else can easily fix it:
{% for state in states.light %} {% if is_state(state.state, 'on') %} {{ state.entity_id }}, {% endif %}{% endfor %}
@Hydreliox Okay, I couldn’t help myself. This should do the trick:
entity_id:{% for state in states.light %}{% if state.state == 'on' %} {{state.entity_id}},{% endif %}{% endfor %}
Test it yourself by putting it into the Templates section under developer tools on the front end.
In your script or automation, call the light.turn_on with a data_template, and pass that statement above as the entity_id . The first example on the documentation templating doc page is similar, except it’s the “message” that has a template there, instead of entity_id. You won’t need the “>” as I wrote this as a one-line statement.
P.S. This template leaves a hanging comma at the end of statement. I don’t think that will be a problem, but it can be fixed with a slightly more complicated statement if it is.
- action:
- data_template:
entity_id:{% for state in states.light %}{% if state.state == 'on' %} {{state.entity_id}},{% endif %}{% endfor %}
profile: '{{states.input_select.house_mode.state}}'
service: light.turn_on
alias: Changement de Mode
condition: []
id: '1511461079824'
trigger:
- entity_id: input_select.house_mode
platform: state
And this is the error :
2017-11-23 22:38:24 ERROR (Thread-2) [homeassistant.util.yaml] mapping values are not allowed here
in "/home/julien/.homeassistant/automations.yaml", line 164, column 14
2017-11-23 22:38:24 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/julien/.homeassistant/configuration.yaml: mapping values are not allowed here
in "/home/julien/.homeassistant/automations.yaml", line 164, column 14