Data_template That will include All lights entity that are on

Hi,
Just think it might be useful for someone .
data_template That will include All light entity that are on.
light.dummy is needed to prevent template error.
the script is called from slider automation event.

set_dim:
  alias: Set Lights brightness
  sequence:
  - service: light.turn_on
    data_template:
      entity_id: >
         light.dummy {% for state in states.light -%}
         {%- if state.state_with_unit =="on" %}
         , {{state.entity_id}}
         {% endif -%}
         {%- endfor -%}
      brightness_pct: '{{ states.input_number.dimlight.state }}'

Cheers

Wow, nice idea using a dummy light entity_id so the list is never empty, which works (at least in the current implementation) because the entity_id’s provided to the light.turn_on service don’t have to actually exist.

FWIW, you can do this using some filters instead of the for loop and if statement:

set_dim:
  alias: Set Lights brightness
  sequence:
    service: light.turn_on
    data_template:
      entity_id: >
        {{ (states.light|selectattr('state','eq','on')|map(attribute='entity_id')|list+
            ['light.dummy'])|join(',') }}
      brightness_pct: "{{ states('input_number.dimlight') }}"

EDIT: Oops, forgot the brightness_pct option.

3 Likes

thank you :slight_smile:
some complex template you have there, and sure it is more elegant

{{ (states.light|selectattr('state','eq','on')|map(attribute='entity_id')|list+['light.dummy'])|join(',') }}

never knew those function (join, map etc)
where do i find Docs on that ? i really wish to learn more on that issue

The easiest place is to go to the Templates page in the frontend. There are two links at the top of the page.

1 Like

Great, thank you :ok_hand: