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.