Help Needed ! (Solved) How to Calculate the average brightness of multiple lights?

Hello World !
I need your help to write a blueprint

blueprint:
    input:
      target_lights:
        name: Target Lights
        description: Select multiple lights
        selector:
          entity:
            domain: light
            multiple: true
variables:
  light_entities: !input target_lights

action:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'brightness_move_up' }}"
        sequence:
          - variables:
              total_brightness: >
                {% set total = 0 %}
                {% for light in light_entities %}
                  {% set total = total + states[light].brightness.state | int(0) %}
                {% endfor %}
                {{ total }}
              average_brightness: >
                {% set count = light_entities | length %}
                {% if count > 0 %}
                  {{ (total_brightness / count) | int }}
                {% else %}
                  0
                {% endif %}

          - action: light.turn_on
            metadata: {}
            target:
              entity_id: !input target_lights
            data:
              brightness: >
                {% set new_brightness = average_brightness + 25 %}
                {% if new_brightness > 255 %}
                  255
                {% else %}
                  {{ new_brightness }}
                {% endif %}

This template is not correct, but it is the idea of what I need.

I need to calculate the average brigthness of a list of lights and give the same brightness + 25 to all lights. But I can calculate it, can someone help me ?

lights_entities: (Are know as those)

  • light.lamp1
  • light.lamp2
  • light.lamp3

Thanks in advance

@beckynet I haven’t done blueprints before, but be careful with what type of information you have in light_entities. If it is a string, then you can’t iterate over it the way you are doing and you would need to split it up by , or space first to make it into a list.

Also, when you do states[light] you should instead do state_attr(light, ‘brightness’).

I hope those help somewhat!
-David

1 Like

Multiple: true makes it a list no matter what…

1 Like

Good to know. I often find I get “light.light1, light.light2” which is a string and then needs to be split to make an iterable list. If it comes out as a list in the variable then that wouldn’t be an issue. (You could check that because if you have only one item it should work if it is a string or a list, I think.)
-David

1 Like

My error was with the variable scope:
A variable set inside a For Loop is not the same as the variable set outside the For Loop

In my original template, the response is always 0, because the ‘total’ set outside the loop and the ‘total’ set inside the loop are not the same

sequence:
  - variables:
      total_brightness: > 
        {% set total = 0) %}
        {% for light in ligth_entities %}
          {% if state_attr(light, 'brightness') is not none %}
            {% set total = total + state_attr(light, 'brightness') %}
          {% endif %}
        {% endfor %}
        {{ total }}

In this correct code, I work with namespace, in this case total is computed

sequence:
  - variables:
      total_brightness: > 
        {% set ns = namespace(total = 0) %}
        {% for light in  ligth_entities %}
          {% if state_attr(light, 'brightness') is not none %}
            {% set ns.total = ns.total + state_attr(light, 'brightness') %}
          {% endif %}
        {% endfor %}
        {{ ns.total }}

The full runing BluePrint code is:

blueprint:
  name: Average Brightness of Multiple Lights
  description: Calculate the average brightness of selected lights
  domain: automation
  input:
    target_lights:
      name: Target Lights
      description: Select multiple lights
      selector:
        entity:
          domain: light
          multiple: true

variables:
  ligth_entities: !input target_lights

trigger:
  - platform: state
    entity_id: !input target_lights
    attribute: brightness

condition: []

action:
  - variables:
      total_brightness: >
        {% set ns = namespace(total = 0) %}
        {% for light in ligth_entities %}
          {% if state_attr(light, 'brightness') is not none %}
            {% set ns.total = ns.total + state_attr(light, 'brightness') %}
          {% endif %}
        {% endfor %}
        {{ ns.total }}
      light_count: >
        {% set ns = namespace(count = 0) %}
        {% for light in ligth_entities %}
          {% if state_attr(light, 'brightness') is not none %}
            {% set ns.count = ns.count + 1 %}
          {% endif %}
        {% endfor %}
        {{ ns.count }}
      average_brightness: >
        {% if light_count > 0 %}
          {{ (total_brightness / light_count) | int }}
        {% else %}
          0
        {% endif %}

  - action: persistent_notification.create
    data:
      message: "The average brightness of the selected lights is {{ average_brightness }}."
      title: "Average Brightness"

Thanks to those who inspired me with their response to understand where my mistake was.