Change color_temp only for "ON" lights in group

Hello. At first sory for my english :slight_smile:
I have a binary switch and group of lights “group.all_light_big_room”.
when I click on switch lights in group “group.all_light_big_room” change their “color_temp”

i have this code in Automation:

- id: '1526499564809'
  alias: Switch_BiR (Right) Cold Light
  trigger:
  - event_data:
      click_type: single
      entity_id: binary_sensor.wall_switch_right_158d0001f40a02
    event_type: click
    platform: event
  condition:
  - below: '230'
    condition: numeric_state
    entity_id: light.big_room_lamp_1_2
    value_template: '{{ state.attributes.color_temp }}'
  action:
  - data:
      color_temp: '333'
      entity_id:
      - group.all_light_big_room
    service: light.turn_on

How can I make this only for allready “ON” lights. Without turning ON all lights in group?

This should work for you. It returns a list of entities to the entity_id. You need to make sure you swap your data section for a data_template. Here’s what you should to to replace your current action in your automation.

  action:
  - data_template:
      color_temp: '333'
      entity_id: >-
        {% set entities = states.group.all_light_big_room.attributes.entity_id %}
        {% for entity in entities if states(entity) == 'on' %}
          {{- entity }}
        {% endfor %}
    service: light.turn_on
2 Likes

You are my hero! Thank you!

1 Like

sorry, but I have one problem with your code when I try it. It dosn’t work.
I have this string in LOG

> 2018-05-18 16:52:46 ERROR (MainThread) [homeassistant.core] Invalid service data for light.turn_on: Entity ID light.big_room_lamp_1_2
> light.big_room_lamp_2_2
> light.yeelight_strip_7811dc691b05
> light.gateway_light_7811dcdedb99 is an invalid entity id for dictionary value @ data['entity_id']. Got 'light.big_room_lamp_1_2\nlight.big_room_lamp_2_2\nlight.yeelight_strip_7811dc691b05\nlight.gateway_light_7811dcdedb99'

I think the problem is in format of data in template:

    {% set entities = states.group.all_light_big_room.attributes.entity_id %}
    {% for entity in entities if states(entity) == 'on' %}
      {{- entity }}
    {% endfor %}

When I try it in “Templates Developer Tools” it look like this:

Unfortunately my knowledge is not enough to understand how to fix it :frowning:

Yeah, that looks normal.

Try this:

action:
  - data_template:
      color_temp: '333'
      entity_id: >-
        {% set entities = states.group.all_lights.attributes.entity_id %}
        {% for entity in entities if states(entity) == 'off' -%}
          {{ entity }}, 
        {%- endfor %}
    service: light.turn_on
1 Like

ok, I make some research. and I know that this code is good

    {% set entities = states.group.all_lights.attributes.entity_id %}
    {% for entity in entities if states(entity) == 'on' -%}
      {{ entity }}, 
    {%- endfor %}

but it add comma after last entity_id, and automation not work…
like this:

do you know how can I delete comma at the and of string

Haven’t tried it (except for on the Template page of the frontend), but maybe:

{{ entity }}{{ ',' if not loop.last }}

BTW, thanks @petro, I think I’m going to use this technique, too! :smile:

2 Likes

thank you, it work fine

- id: '1526499564809'
  alias: Switch_BiR (Right) Cold Light
  trigger:
  - event_data:
      click_type: single
      entity_id: binary_sensor.wall_switch_right_158d0001f40a02
    event_type: click
    platform: event
  condition:
  - above: '230'
    condition: numeric_state
    entity_id: light.big_room_lamp_1_2
    value_template: '{{ state.attributes.color_temp }}'
  action:
  - data_template:
      color_temp: '175'
      entity_id: >
        {% set entities = states.group.all_light_big_room.attributes.entity_id %}
        {% for entity in entities if states(entity) == 'on' -%}
          {{ entity }}{{ ',' if not loop.last }}
        {%- endfor %}
    service: light.turn_on
1 Like