How to change brightness of devices part of group that are on

Hi, I’m fairly new to HA. And have now struggled with a problem for more than a week. Hope somebody can/will help me :sweat_smile:
I’m trying to make an automation triggered on an event (this part is OK) and then increase the brightness of the lights in a group that is turned on - and don’t touch the lights that are off.
So how to do that in yaml/template?

Hi there, welcome to the community.
I hope you have already successfully deployed some other automations. There are multiple methods to achieve what you are looking for in this case. The simplest of which would be to use choose action with each light. The action part of the automation would look like this.

action:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.dining-room
            state: 'on'
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.dining-room
            data:
              brightness_pct: 70
  - choose:
      - conditions:
          - condition: state
            entity_id: light.living-room
            state: 'on'
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.living-room
            data:
              brightness_pct: 70

In this case the automation will check each and every light if it turned on and then increase brightness. If off it will skip to next.

There are also complicated methods which involves templates and all. Hope some one else would give that option.

Hi sheminasalam
Thanks for your solution. I have also thought about this solution. My tries are properly far to complicated because I come from a programmer background and have to get used to the more simple configurations, but with much more redundancy.
What I have right now looks like this:

alias: KITCHEN-Decrease Spots Brightness
sequence:
  - variables:
      light_entities: '{{ entity_ids }}'
      entities_to_regulate: >
        {{ expand(light_entities) | selectattr('state', 'eq', 'on') 
          | selectattr('brightness', '>', 1)  | map(attribute='entity_id') | list }}
      new_brightness: >
        {% if entities_to_regulate | count > 0 %}
          {{ state_attr(entities_to_regulate[0], 'brightness') + 1 }}
        {% endif %}
  - service: light.turn_on
    entity_id: 
        '{{entities_to_regulate}}'
    data:
      brightness: '{{ new_brightness }}'
mode: single

Where I call this script from the action and give it the entity_ids it should be working on.
But something is wrong with it.

Provide an example of the value of the entity_ids variable.

It should look something like this (comma-separated quoted entity_ids):

'light.kitchen', 'light.bedroom', light.bathroom'

Hi Taras
I have tried to change a little:

alias: KITCHEN-Decrease Spots Brightness
sequence:
  - variables:
      light_entities:
        - light.kitchen_inner_spots
        - light.kitchen_outer_spots
      entities_to_regulate: >
        {{ expand(light_entities) | selectattr('state', 'eq', 'on') 
          | map(attribute='entity_id') | list }}
      new_brightness: |
        {% if entities_to_regulate | count > 0 %}
          {{ (state_attr(entities_to_regulate | first, 'brightness') | int) - 1 }}
        {% else %}
        25
        {% endif %}
# For DEBUG
  - service: input_text.set_value
    data:
      value: '{{ entities_to_regulate }}'
    target:
      entity_id: input_text.entities
# For DEBUG
  - service: input_number.set_value
    data:
      value: '{{ new_brightness }}'
    target:
      entity_id: input_number.number
  - service: light.turn_on
    entity_id: 
      '{{ entities_to_regulate }}'  # This is the line that does not work
#        - light.kitchen_inner_spots
#        - light.kitchen_outer_spots
    data:
      brightness: '{{ new_brightness }}'
mode: single

As you can see have I added a few helpers to see what was going on. In the helpers everything looks perfect. And if I uncomment the two last commented lines and comment out the line that is doesn’t like it it can run and work. So I guess my problem is that I do not know how to expand the list '{{ entities_to_regulate }}' so it will be accepted as an entity list.

You overlooked to answer my question.

Your modified script works because light_entities is defined as a list (and expand handles a list correctly).

Your original script uses the data passed to it via the entity_ids variable. Look at what calls the script and the format of the data that it passes to the script via entity_ids. The data must be either a list or a string in the format I showed in my previous post.

In my original script I gave it a coma seperated list as you describe, but this was not the problem. I just put the list into the modified script to make it easier to debug.
The problem comes when I put in
entity_id:
‘{{ entities_to_regulate }}’
Then it complains when I try to save the script.

I’ll ask one more time: Post an example of what is passed to the script.

I found the solution to my problem - Taras lead me to it - thanks

alias: Increase Brightness if Light On
sequence:
  - variables:
      light_entities: '{{ entity_ids }}'
      entities_to_regulate: |
        {{ expand(light_entities) | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list }}
      new_brightness: |
        {% if entities_to_regulate | count > 0 %}
          {{ (state_attr(entities_to_regulate | first, 'brightness') | int) + 1 }}
        {% endif %}
  - condition: template
    value_template: '{{ entities_to_regulate | count > 0 }}'
  - service: light.turn_on
    data_template:
      entity_id: '{{ entities_to_regulate | join('', '') }}'
    data:
      brightness: '{{ new_brightness }}'
mode: single

And I call it from another script like this:

              - service: script.increase_brightness_if_light_on
                data:
                  entity_ids:
                    - light.kitchen_inner_spots
                    - light.kitchen_outer_spots

I hope somebody else can use it. And thankyou for a wonderful and very active forum.

If my suggestion led to the solution then it’s the custom of this forum to mark that post with the Solution tag (not your own post). For more information, refer to guideline 21 in the FAQ.


What I see in your most recent post is that you passed a proper list to the script. This:

entity_ids:
  - light.kitchen_inner_spots
  - light.kitchen_outer_spots

is effectively this:

entity_ids: ['light.kitchen_inner_spots', 'light.kitchen_outer_spots' ]

which the expand function understands how to expand just like it knows how to expand this (comma-separated string):

entity_ids: 'light.kitchen_inner_spots', 'light.kitchen_outer_spots'

That’s why I asked for an example of what you were attempting to pass to the script because anything other than those two formats will fail to be expanded and cause an error.

1 Like