Light minimum brightness

It’s easy to create an automation for each light so when it changes state, check the brightness value and if below certain value, set it to minimum.

But is there a way to do this automatically with any lights using just 1 automation with help of templates?

This template will give us the list of lights currently on and their settings. Can add count to the end for a count.

    {% set domain = 'light' %}
    {% set state = 'on' %}
    {{ states[domain] | selectattr('state','eq', state) | list  }}

This is the result for 2 lights that supports brightness (ones don’t won’t have brightness in its attributes):

[<template TemplateState(<state light.living_room_tv_light_bulb=on; supported_color_modes=['brightness'], color_mode=brightness, brightness=1, is_deconz_group=False, friendly_name=Living Room TV Light Bulb, supported_features=41 @ 2021-04-14T15:17:54.410311+01:00>)>, <template TemplateState(<state light.study_light=on; supported_color_modes=['brightness'], color_mode=brightness, brightness=2, is_deconz_group=False, friendly_name=Study Light, supported_features=41 @ 2021-04-14T15:18:01.420260+01:00>)>]

Question is, how do I then extract the entity ID and brightness values from that, using template codes?

With those 2 bits of information, I can then do a test to see whether the value is below certain number and finally run light.turn_on service to set the value as desired.

So, plan is: (psudo code)

Trigger: 
- lights_on_count != saved lights_on_count 
Action: 
- updated saved lights_on_count 
- extract all brightness and entity ID out of the fields 
- condition continue if any brightness value is less than my desired minimum 
- set the lights with brightness lower than minimum to minimum using service call

Any help or different way to achieve this is much appreciated.
Thanks!

You can use adaptive lightning.

1 Like

Try this:

action:
  - variables:
      threshold: 100
  - service: light.turn_on
    target:
      entity_id: >
        {% set lights =  states.light
          | selectattr('state', 'eq', 'on')
          | selectattr('attributes.brightness', 'lt', threshold)
          | map(attribute='entity_id') | join(', ') %}
        {{ lights if lights|length > 0 else 'none' }}
     data:
       brightness: "{{ threshold }}"
1 Like

Brilliant. Thank you! That’s exactly what I’m looking for.

This is the automation currently, in case anyone else wants to use it:
(Need to create input_number.lights_min_brightness_count helper. )

- alias: Lights - Minimum Brightness 
  mode: restart
  trigger: 
  - platform: time_pattern
    minutes: /5
  - platform: template
    value_template: >-
      {% set count = states['light'] | selectattr('state','eq', 'on') | list | count %} 
      {{ count != (states('input_number.lights_min_brightness_count') | int) }}
  condition: []
  action:
  - service: input_number.set_value
    target:
      entity_id: input_number.lights_min_brightness_count
    data:
      value: "{{ states['light'] | selectattr('state','eq', 'on') | list | count }}"
  - delay: 00:00:05
  - variables:
      threshold: 50
  - service: light.turn_on
    target:
      entity_id: >
        {% set lights =  states.light
          | selectattr('state', 'eq', 'on')
          | selectattr('attributes.brightness', 'lt', threshold)
          | map(attribute='entity_id') | join(', ') %}
        {{ lights if lights|length > 0 else 'none' }}
    data:
      brightness: "{{ threshold }}"
  - service: input_number.set_value
    target:
      entity_id: input_number.lights_min_brightness_count
    data:
      value: "{{ states['light'] | selectattr('state','eq', 'on') | list | count }}"

However, there are a few tiny bugs with my approach…

As you can see, I’ve had to put in a delay in there. Otherwise any transitioning light will be set the minimum brightness.

Also had to be very careful with the input_number and setting it as many times as possible to ensure it keeps in-sync with the count. Because template trigger only triggers on false->true transition. Hence the timer trigger as well.

Would be great if there’s a way to blacklist lights from being set this way?

Found another bug while using lights last night: when a single light is turned on, in a grouped lights (light: platform: group) that is less than threshold. The whole group will turn on and set to min brightness.

That action template picks up both grouped lights and individual lights. In my case:
light.all_bedroom_lights, light.bedroom_2_light_bulb

Any idea how to get around this?
Perhaps ignore lights with entity_id as one if its attribute? How to detect existence of attribute within templates?

That is to be expected because your original requirement stated “any lights”.

You will have to enhance the template to exclude certain lights. You can use the reject filter to do that.

Alternatively, instead of using states.light in the template, create a group containing all the lights you want to control and then replace states.light with expand('group.my_lights').

1 Like

Thank you, again!

Indeed my original requirement stated any lights, forgetting light groups. A good example of end-user not having enough understanding of the system to provide sensible requirements.

The reason I thought I wanted to do this for all lights was because I wanted the automation to work no matter how I change the light setup.


For anyone who might stumble across this, wanting to do the same thing. Here is my completely updated automation. Now only need to create a group to whitelist the lights.

- alias: Lights - Minimum Brightness 
  id: '999999999001'
  mode: single
  trigger: 
  - platform: template
    value_template: >
      {% set threshold = 50 %}
      {{ expand('group.light_min_brightness')
        | selectattr('state', 'eq', 'on')
        | selectattr('attributes.brightness', 'lt', threshold)
        | map(attribute='entity_id') | list | count }}
  condition: []
  action:
  - variables:
      threshold: 50
  - service: light.turn_on
    target:
      entity_id: >
        {% set lights = expand('group.light_min_brightness')
          | selectattr('state', 'eq', 'on')
          | selectattr('attributes.brightness', 'lt', threshold)
          | map(attribute='entity_id') | join(', ') %}
        {{ lights if lights|length > 0 else 'none' }}
    data:
      brightness: "{{ threshold }}"

I ditched my initial idea with the input_number by applying KISS (keep it simple stupid). The delay and assignment makes everything very unpredictable. The gradual transition problem no longer appears when keeping everything as simple as possible. Now it just utilises the fact template triggers on any transition from zero to non-zero value.

If you wish, I think you can take advantage of the new trigger_variables introduced in version 2021.3.

The threshold variable can be defined once and then referenced by the trigger and the action.

- alias: Lights - Minimum Brightness 
  id: '999999999001'
  mode: single
  trigger_variables:
    threshold: 50
  trigger: 
  - platform: template
    value_template: >
      {{ expand('group.light_min_brightness')
        | selectattr('state', 'eq', 'on')
        | selectattr('attributes.brightness', 'lt', threshold)
        | map(attribute='entity_id') | list | count }}
  condition: []
  action:
  - service: light.turn_on
    target:
      entity_id: >
        {% set lights = expand('group.light_min_brightness')
          | selectattr('state', 'eq', 'on')
          | selectattr('attributes.brightness', 'lt', threshold)
          | map(attribute='entity_id') | join(', ') %}
        {{ lights if lights|length > 0 else 'none' }}
    data:
      brightness: "{{ threshold }}"
1 Like

That’s much nicer, setting the variable at a single place near the top. I did look through the docs and must have missed it on quick search.


Is there a way to trigger off UI (Lovelace) light.turn_on events?

I’m still having problem with physical button triggered automation with transitions getting overridden by this automation. Even for 1s transition with a wait of 3 seconds in this automation. I think the problem is a few of the Osram zigbee bulbs are slow to report back its brightness updates. Ikea bulbs have no problem even without any delay.

The real reason I want min brightness is for all of the lights’ UI toggles. A lot of the times they just turn on at brightness of 1.


Edit (19 April):
Silly me, with button triggered automations, I just needed to disable this automation and then re-enable it after transition period.