Automation using light brightness as a condition?

Hi, I’m pretty sure this is possible and probably fairly easy using templates but I couldn’t find an example and I’m not sure how to do it myself so I figured I’d ask for some help. What I’d like to do is have an automation that will monitor my lights status between a set period of time (Night) and if the light is on, automatically dim it down to the brightness I choose. The only thing I need to figure out is how to set a condition for the automation such that the light needs to be on (easy) and that the light is brighter than say 50%.

Here’s a condition that checks whether one of my lights is below 10 (scale = 0-255)

condition: numeric_state
      entity_id: light.kitchen_bar_level_8_0
      value_template: '{% if states.light.kitchen_bar_level_8_0.state == "on"  %}{{ states.light.kitchen_bar_level_8_0.attributes.brightness }}{% else %}0{% endif %}'
      below: 10
2 Likes

Perfect, this is exactly what I was looking for. Thanks.

You might want to check out my automation. I do this exact thing but have a template to check all lights.

https://github.com/CCOSTAN/Home-AssistantConfig/blob/master/automation/detect_and_adjust_lights.yaml

1 Like

This is great! So what exactly do these templates do? Do I just replace light, light.tv, etc with all of my lights? Will this let them all operate separately? Sorry for all the questions, I don’t know much about code.

The statements with the != are the lights that you DON’T want affected (for me , it’s the accent lighting that is already properly lit). You can either delete them or repurpose them for devices you don’t want touched. By Default, every light that comes on is evaluated and adjusted as necessary for the time frame. it works great!

Ok, So I should be able to swap out the time condition for the sun condition if I wanted to have my lights dim after sunset right? I can’t seem to get this working, I don’t get any errors or anything, It just doesn’t dim my lights when I turn them on. I’ll include my automation below, I’d appreciate if you could take a look at it.

alias: 'Autoadjust Living Room Lights After Sunset'
trigger:
  - platform: event
event_type: state_changed
condition:
  - condition: state
    entity_id: device_tracker.phone
    state: 'home'
  - condition: sun
    after: 'sunset'
  - condition: sun
    before: 'sunrise'
  - condition: template
    value_template: "{{ trigger.event.data.entity_id.split('.')[0] == 'light' }}"
  - condition: template
    value_template: "{{ trigger.event.data.new_state.state == 'on' }}"
  - condition: template
    value_template: "{{ trigger.event.data.old_state.state == 'off' }}"
action:
  - service: light.turn_on
    data_template:
      entity_id: "{{ trigger.event.data.entity_id }}"
      brightness: 130

I’m just trying to have it work for every light as of right now, that way I can just understand the basics of how to do this.

Counting on an event to happen when you turn on a light is problematic. Many zwave switches don’t instantaneously report their on/off state to the zwave controller. So you’re dependent in that case on the polling interval, which can be slow.

Does it matter if it’s instantaneous? I don’t mind there being a delay due to that if that is the issue. I’m using a wink hub so there definitely is delay involved.

It shouldn’t be a prob, if you’re OK with the delay.

The above code strikes me as overkill if you’re just trying to establish a dim setting for a couple lights.

If this is your first foray into automation, I’d recommend starting basic so that you understand the concepts.

Also - you’ve got a lot of conditions there that can halt the automation. Troubleshoot by checking the states you car about by looking a the states dev tool.

I’ve done a few automations and I get the jist of it I think. My main confusion with this one is using the template stuff. I don’t know anything about that. I really just want to have the lights dim between sunset and sunrise automatically if say I turn on my lights from another service such as my amazon echo. Is there a simpler way of doing this that you know of?

On my system, there can be up to a minute delay on the auto dim but it does work pretty flawlessly. I might steal your idea and change the 8pm to be sunset. Nice idea.

For anyone curious, I was able to figure out my own way of doing it, I’ll include my automation below in case anyone was curious as to how I set mine up.

alias: 'Autoadjust Bedroom Lights After Sunset'
trigger:
  - platform: numeric_state
    entity_id: light.bedroom_light_1
    value_template: '{{ state.attributes.brightness }}'
    above: 140
condition:
  condition: state
  entity_id: sun.sun
  state: below_horizon
action:
  - service: script.bedroom_lights_fade_down_to_50_percent
4 Likes

When a HUE light is turned off and then on using a physical switch it will then default to full brightness. Not something I like during the night.
So I use this automation to “quickly” (as soon as HUE controller picks it up again) change it to another brightness level:

- alias: 'Light Physically turned off and on'
  trigger:
  - platform: numeric_state
    entity_id: light.hue_white_lamp_4
    value_template: '{{ state.attributes.brightness }}'
    above: 253
  action:
    - service: light.turn_on
      data: 
        entity_id: light.hue_white_lamp_4
        color_temp: 353
        brightness: 1

Curious, could I do this with Color Temperature? I have some Osram Lights connected to my Hue, which come on and default to a warm white, we like them a bit cooler, so I’d like them to change to the cooler temp if they come on (via physical switch).

Could I use this same automation, and just swap in my light names, and my color_temp value?

Yes, just swap it in for your light names and color_temp value however first look at the exact values right after you switch them on physically. You’ll need to trigger with that initial value otherwise they’ll change in other cases as well.

Thanks, it works unfortunately one of the bulbs I want to run it on doesn’t reconnect with the hue very quickly. need to look into that.