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 ?
@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â).
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
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.