Automated Dimming Threshold

I’ve got a current issue with a light switch and some LED bulbs where when the light is turned down past a certain threshold, the LEDs are off, but the switch is still on. I would like to fix this by using Home Assistant to create an automation where when the lights are below a certain threshold, Home Assistant turns the lights back up to the lowest identified brightness setting where the lights are still visible. I would rather do this than turn the switch off, because the brightness setting appears to be persistent, and when the light is turned back on, it’s still at the lower setting.

This is what I currently have, but it’s not really doing anything. I’m trying to find the right event to latch on to for whenever the brightness is changed.

automation:
################################################################################
  - id: function_lighting_dining_room
    alias: "Function - Lighting - Dining Room"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: light.wink_dining_room
        to: 'on'
    condition:
      - below: '15'
        condition: numeric_state
        entity_id: light.wink_dining_room
    action:
    - data:
        brightness_pct: '20'
        entity_id: light.wink_dining_room
      service: light.turn_on
################################################################################

you gotta call out the attribute for numeric state. Probably easier to just use the trigger in a value tempalte:

automation:
################################################################################
  - id: function_lighting_dining_room
    alias: "Function - Lighting - Dining Room"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: light.wink_dining_room
        to: 'on'
    condition:
      - condition: template
        value_template: "{{ state_attr('light.wink_dining_room','brightness_pct') | int < 15 }}"
    action:
    - data:
        brightness_pct: '20'
        entity_id: light.wink_dining_room
      service: light.turn_on
################################################################################

I don’t think that would work because that will only trigger when the light goes from off to on. (Same issue with the trigger in the OP.) But I think the OP wants it to trigger whenever the light level changes, even if it was already on.

I think a template trigger might do the trick:

  trigger:
    platform: template
    value_template: >
      {{ is_state('light.wink_dining_room', 'on') and
         state_attr('light.wink_dining_room', 'brightness')|float < 255*0.15 }}

This will be evaluated whenever either the state or any of the attributes of light.wink_dining_room change, and will be true (and cause the automation to fire) if the light is on and the brightness is below 15%. (Note that brightness goes from 0 to 255, hence the need to multiply it by 15%, or 0.15.)

I missed the to:‘on’ bit. If he removes it, it should update when attributes update as well as when the state changes.

EDIT: might have to build in safety for off state changes

Derp. You’re absolutely right, triggering on to: 'on' will only happen when it turns on. Removed that line, it’ll run on any state change.

Adding in a safety check for whether or not the light is on is also a good idea. Here’s what I’ve got right now:

automation:
################################################################################
  - id: function_lighting_dining_room
    alias: "Function - Lighting - Dining Room"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: light.wink_dining_room
    condition:
      condition: 'and'
      conditions:
      - condition: state
        entity_id: light.wink_dining_room
        state: 'on'
      - condition: numeric_state
        entity_id: light.wink_dining_room
        below: '15'
    action:
    - data:
        brightness_pct: '20'
        entity_id: light.wink_dining_room
      service: light.turn_on
################################################################################

I’ll test it out a bit when I get a chance, but that should work.

FWIW, my suggestion above replaces the trigger and conditions in your new automation. Either way is fine; it’s more of a style thing.

Although, there still are two bugs in your last automation…

First, the brightness of a light is a value between 0 and 255. If you test against 15, then you’re actually testing against 5.9% (i.e., 15/255), not 15%. You should be using a value of 38.25 (which is 15% of 255.)

Second, the numeric_state condition is trying to check the state of light.wink_dining_room, but the state will be ‘on’ or ‘off’, not the brightness level – that is in an attribute. You at least need to change that condition to:

      - condition: numeric_state
        entity_id: light.wink_dining_room
        value_template: "{{ state.attributes.brightness }}"
        below: 38.25

This is what I get for trying to write configurations when I’m away from home and can’t immediately test it. Here’s what I’ve currently got. I’ll check when I get home to see if this is working right. Thanks for your help.

automation:
################################################################################
  - id: function_lighting_dining_room
    alias: "Function - Lighting - Dining Room"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: light.wink_dining_room
    condition:
      condition: 'and'
      conditions:
      - condition: state
        entity_id: light.wink_dining_room
        state: 'on'
      - condition: numeric_state
        entity_id: light.wink_dining_room
        value_template: "{{ state.attributes.brightness }}"
        below: 38.25
    action:
    - data:
        brightness_pct: '20'
        entity_id: light.wink_dining_room
      service: light.turn_on
################################################################################