Template help needed (adjust brightness of multiple entities)

First…
What do I want to do;
I want to use 2 regular switches to be a dimmer, for the “light”.entity’s that have a state “on” AND are member of “group.livingroom” or “group.kitchen”.

I came up with a entity template:
{% for state in states.light %}{% if (state.state == ‘on’) %} {{state.entity_id}},{% endif %}{% endfor %}
This wil fill all the entity’s that are currently on, but how to target this both groups?..

than I need to fill the automation with the value’s…

  • id: ‘0000000000111’
    alias: Kitchen switch right double
    trigger:
    platform: event
    event_type: click
    event_data:
    entity_id: binary_sensor.wall_switch_999
    click_type: double
    action:
    service: light.turn_on
    data_template:
    entity_id:
    {% for state in states.light %}{% if (state.state == ‘on’) %} {{state.entity_id}},{% endif %}{% endfor %}
    brightness: ???
    initial_state: ‘on’

I’m stuck… I think I need to do something with a for loop in the brightness… But i cant figure out where to go further…
Any advice?

data_template:
  entity_id: >
    {% set g1 = state_attr('group.livingroom', 'entity_id') %}
    {% set g2 = state_attr('group.kitchen', 'entity_id') %}
    {% set g = g1 + g2 %}
    {{ states.light|selectattr('entity_id','in',g)
                   |selectattr('state','eq','on')
                   |map(attribute='entity_id')|join(',') }}

EDIT: The only problem with this is if none of the lights are on, then the list will be empty, which will cause an error. The easiest way to solve that is to add a dummy light entity_id to the list (because the light.turn_on service doesn’t require the entities to actually exist.) So:

data_template:
  entity_id: >
    {% set g1 = state_attr('group.livingroom', 'entity_id') %}
    {% set g2 = state_attr('group.kitchen', 'entity_id') %}
    {% set g = g1 + g2 %}
    {{ (states.light|selectattr('entity_id','in',g)
                    |selectattr('state','eq','on')
                    |map(attribute='entity_id')|list+['light.dummy'])|join(',') }}
1 Like

!!! This works like a charm!!! Could not resist testing it before rushing to work :wink:

It didn’t think of the option I could put the entitiy’s in a variable :-S

I google’ed some more, and I see suggestions to increase or decrease the brightness by doing following:

brightness: >
{% set n = states.light.living.attributes.brightness + 10}
{% if n > 254}
254
{% else %}
{{ n }}
{% endif %}

I’m now at work, so testing some more is not an option for now :frowning:
But this will just adjust the brightness for just 1 fixed entity…
As of your brilliant idea, I’m just going to ask… any idea how to set the brightness for every entity in the array +10 to a max of 254?

As far as I know, you can’t do it in one service call, assuming every light might be at a different level.

The easiest way to do that would probably be to create a python_script that you would call from your automation.