Trying to get the lowest brightness of a group of light entites

So I am new to this, I have been working at it for days. I have experience coding mostly C and C#, be many other languages primary C based, and I have tried all different types of techniques with this.

I basically just want to select the light entity in a group of entities that has the lowest brightness. Then I can use entity to stop a dimmer loop that is working for the entire group. In other words it won’t stop until the entity with the lowest brightness is at 100%.

I thought the easiest way would be just to loop through the objects and find the one that was the lowest brightness. Seemed simple enough.

                {% set ns = namespace(entities=[]) %}
                {% set min_brightness = 255 %}
                {% for s in expand(all_entities) if s.state != 'off' %}
                  {% set brightness = state_attr(s.entity_id, 'brightness') | default(0) %}
                  {% if brightness < min_brightness %}
                    {% set min_brightness = brightness %}
                  {% endif %}
                  {% set ns.entities = ns.entities + [s.entity_id ~ ' - ' ~ brightness ~ ' (min: ' ~ min_brightness ~ ')' ] %}
                {% endfor %}
                {{ ns.entities | join(', ') }}

But here are my results

light.living_room_front_overhead - 56 (min: 56),
light.living_room_back_overhead - 94 (min: 94)

As you can see it looks like the min_brightness variable reset’s at every pass of the loop. So frustrating! At this point I am just trying to get notification messages so I can see if I am on the right track.

So then I tried tempting, but I will admit I am lost at this point. This was my attempt, but I got no notification messages at all. Part of the issue is the syntax, I have been using chatgpt to help me with that as well as reading here, but it’s confusing what will work with home assistant and so forth. Anyway here is my code that doesn’t work.

              all_entities: "{{ targets }}"
              brightness_values: "{{ all_entities| map(attribute='attributes.brightness') | list }}"
              min_brightness: "{{ brightness_values | min }}"
              min_entity: "{{ all_entities| selectattr('attributes.brightness', 'equalto', min_brightness) | first }}"

It’s 3am and I have been busting my ass on this for 3 days. I had had some success coding blueprints before I got to this little challenge.

Please someone point me in the right direction.

I assume that, in your example, the variable all_entities is a list containing light entity_ids.

I tested the following on my system and it reported the entity_id of the dimmest light listed in all_entities (excludes lights that are off, of course).

{% set lights = expand(all_entities) | selectattr('state', 'eq', 'on')
  | map(attribute='entity_id') | list %}
{% set levels = lights | map('state_attr', 'brightness') | list %}
{{ (zip(lights, levels) | sort(attribute=1) | first | default([none, none]))[0] }}

If there are no lights that are on, the template report a null object (none). This is configurable in the default filter.


NOTE

If you want to report the entity_id and its brightness level, simply change the ending of the last line to this:

{{ (zip(lights, levels) | sort(attribute=1) | first | default([none, none])) | join(', ') }}
1 Like

Yes

all_entities: "{{ targets }}"

the way it works is you select the “targets” when you initiate the blueprint,

and yes {{ all_entities| join(', ') }} would create a comma separated list of those entities.

I will give your code a try and report back. Thank you for this.

It worked!! Finally I can move on. So I was on the right track but it’s like I said syntax idiosyncrasies.

So I have been working with this code

{% set lights = expand(all_entities) | selectattr('state', 'eq', 'on')
  | map(attribute='entity_id') | list %}
{% set levels = lights | map('state_attr', 'brightness') | list %}
{{ (zip(lights, levels) | sort(attribute=1) | first | default([none, none]))[0] }}

Is there a way to get it to select all the entities but just substitute the those with brightness no brightness levels because they are off to 0? I tried something like this but again it doesn’t work


{% set lights = expand(all_entities) | map(attribute='entity_id') | list %}
{% set levels = lights | map('state_attr', 'brightness') | map('default', 0) | list %}
{{ (zip(lights, levels) | sort(attribute=1) | first | default([none, none]))[0] }}

The point is if one is off, and the other is set on 30 the one that is off should really work like it’s brightness is set to 0

Thanks again.

Glad to hear it worked.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which indicates to others that the topic has been solved . This helps other users find answers to similar questions.

When a light is off it has no integer brightness value; the value is not 0 it’s null.

Why do you want to convert null to zero?

NOTE

I tried your template and it reported the entity_id of a light that was off. Can you explain what you mean when you said it “doesn’t work”? What result were you expecting? EDIT: My testing procedure was incomplete. I now see the problem.

This version worked for me:

{% set lights = expand(all_entities) | map(attribute='entity_id') | list %}
{% set levels = lights | map('state_attr', 'brightness') | map('int', 0) | list %}
{{ (zip(lights, levels) | sort(attribute=1) | first | default([none, none]))[0] }}

Thank you,

This worked exactly as I needed it to.

I would like to note here, what this does is takes a list of entities and select the one with the lowest brightness.

Changing the | first | in the command below to | last | will give you the one with the highest.

{{ (zip(lights, levels) | sort(attribute=1) | first | default([none, none]))[0] }}

I suspect this technique could be use in other ways and maybe it’s presumptuous of me to assume that this isn’t used pretty typically.

However I was unable to find an example of this specifically. So I thought I would document this here in case someone else is struggling. Truth is I am probably just feeling the rush of getting this to work after 4 days.

Once again thanks so much to Taras for their help here.