Dynamic Global Default Brightness

Let me start by saying thank you to anyone willing to help me out. I’m finally figuring some of this out and almost ready to start paying my dues by helping some others.

So, I’ve read a BUNCH of posts and tried a couple different things, but I’m not finding what I want and I’m afraid I’m over-thinking it.

What I’m looking for is to set a default brightness, globally, for all lights, based on time or sun. In other words, when we turn on a light, I want the brightness to be adjusted so we don’t blind ourselves when turning on lights at night. Equally important is that I don’t want to change lights that are already on.

I’m not asking anyone to write the code for me, just perhaps point me in the right direction. If you’ve done this or something similar, I’d love to see how you did it.

Requirements:

  1. If a light is already on, leave it alone.
  2. if a light is turned on (manually via switch, through app, or via alexa) set brightness based on time of day or sun/sunset.
  3. Apply rule to all lights, without turning on all lights when a single light is selected.

Thanks in advance, hoping this is do-able and not overly complex.

When you turn on a light manually via its switch, the light is under the switch’s direct control. The best outcome in this situation is that Home Assistant can detect the switch is on (via an automation) and then set the light to the appropriate level (based on whatever criteria you specified). However, in practice, this means:

  • manually turn on light switch
  • light is under switch’s direct control and is set to full brightness (see note 1)
  • automation detects switch is on and reduces brightness (based on your criteria)

Clearly, that’s not a great ‘user experience’ so you’ll have to temper your expectations when it comes to manually operating a light switch.

Here’s the workaround I use: a motion detector does the work of turning on the light switch automatically after dark.

I have a motion detector and a light-level sensor in the bathroom. Motion triggers an automation. If it’s dark (light level is below a threshold I determined empirically), the light is turned on. Depending on what time of the evening, the light’s level is set accordingly. For example, if it’s before midnight the brightness is set to ~40%. If it’s after midnight, it’s set to 10%. Of course, if I turn on the light switch manually, it overrides the automation.

NOTE
(1) Some switch’s have a ‘last-level’ option. When you turn on the light, its brightness is set to its previous brightness level (which may have been less than 100%).


EDIT

Some light switch’s can be programmed so that their switch does not control the local load. In other words, operating the rocker button doesn’t directly control the light that’s connected to the switch. What happens instead is that operating the switch simply sends out events (‘upper-rocker tapped once’ or ‘bottom-rocker tapped twice’). Effectively, the rocker button behaves like a remote-control.

An automation would listen for these events and then, based on time of day, send a command back to the light switch to instruct it to set the local load (the connected light) to a specific brightness level. I should point out that this level of sophistication depends on the switch’s lighting technology.

Okay - that all makes sense. Until we’re ready to start swapping out physical light switches, we can focus on the “smart” controls.

How about the idea of changing default values for all lights, throughout the day/night? I have a couple motion sensors already, but I’m really trying to find a solution that’s a little more on-the-fly.

I’ve tried the Circadian Lighting option, which is close. However it doesn’t play nice with scenes or lights already on since it continually updates.

Thanks again.

What kind of lighting technology are you currently using? The ability to set global rules may depend on what you have.

Good question - I should have shared that. Most of our lights are hue color. Some bulbs and some retrofit cans.

This may get you started. It doesn’t handle #1, but you could insert the on lights into a group that then gets ignored by the script.

Please post back improvements; this works but always looking for better/cleaner!

##########################################################
## Update brightness when light turned on
##########################################################
- alias: Adjust brightness as lights are turned on
  initial_state: True
  mode: queued

  trigger:
    - platform: event
      event_type: state_changed
      
  condition:
    - condition: template
      value_template: "{{ trigger.event.data.new_state.domain == 'light' }}"
    
    - condition: template
      value_template: "{{ trigger.event.data.new_state.state == 'on' }}"
      
    - condition: template
      value_template: "{{ trigger.event.data.old_state.state == 'off' }}"
   
    # Don't change these lights.
    #   - condition: template
    #     value_template: >-
    #       {{ trigger.event.data.entity_id != 'light.outdoor_front_switch' }}
   
  action:
    - service: script.update_brightness
      data_template:
        entity_id: "{{ trigger.event.data.entity_id }}"

#    - service: logbook.log
#      data_template:
#        name: "Auto Dim Intercept: {{ trigger.event.data.entity_id }}"
#        message: "{{ trigger.event.data.old_state.state }} -> {{ trigger.event.data.new_state.state }}"

##########################################################
## Update Light Brightness
##########################################################
- alias: Update Light Brightness
  initial_state: True

  trigger:
    - platform: time_pattern
      minutes: "/10"
      seconds: 05

  condition:
    - condition: template
      value_template: "{{ states.light | selectattr('state','==','on') | map(attribute='entity_id') | list | count >= 1 }}"
 
  action:
    - service: script.update_brightness
      data_template:
        entity_id: "{{ states.light | selectattr('state','==','on') | map(attribute='entity_id') | join(',') }}"

And the script:

##########################################################
## Update Brightness
##########################################################
update_brightness:
  alias: Update Brightness
  mode: queued

  sequence:
#    - service: logbook.log
 #     data_template:
  #      name: "Light Update"
   #     message: "{{ entity_id }}"

    - service: light.turn_on
      data_template:
        entity_id: '{{ entity_id }}'
        transition: 15
        brightness_pct: >-
          {%- if is_state('alarm_control_panel.ha_alarm','triggered') -%}
            100
          {%- else -%}
            {%- set hour = ( now().hour|int + now().minute|int / 60 )|float|round(1) -%}
            {%- if hour >=  6.0 and hour < 8.0 -%}
              {%- set adj_brightness = ( hour * 20.0 ) - 80 -%}
              {{ adj_brightness|int }}
            {%- elif hour >=  8.0 and hour < 19.0 -%}
              80            
            {%- elif hour >= 19.0 and hour < 23.0 -%}
              {%- set adj_brightness = 270.0 - ( hour * 10.0 ) -%}
              {{ adj_brightness|int }}
            {%- else -%}
              40 
            {%- endif -%}           
          {%- endif -%}
2 Likes

So when you described turning these lights on manually, how are you doing that now? Using a Philips Hue Dimmer Switch? The Philips Hue app? What is your interpretation of manually turning on a Hue bulb?

Doesn’t this do the ‘not great user experience’ scenario I described earlier? The moment it detects a light has been turned on it sends a new brightness level to override the initial one. Depending on the lighting technology employed, you may get an initial momentary flash (brightness = 100%) before the overriding level (perhaps 35%) takes precedence.

FWIW, I’ve witnessed this behavior with color (using Philips Hue Color bulbs). They’re used to illuminate a pool and are programmed to change colors. The moment they are turned on, they use their previous color, and then the automation sets them to a random color. So the moment they are turned on there is a momentary flash of the previous color before the overriding color takes effect.

I don’t get the initial flare at all with a manual turn on. Most modern switches are soft turn on and I am pretty sure the first automation fires quickly and sets a new set point well before reaching 100% (or the last setting if it’s a switch that remembers).

Depending on your lights it may work differently, but very smooth here.

I also use that script when an automation turns on a light so that its target is correct.

Note: that if our home alarm is triggered all lights are turned on and go to 100% as long as in the triggered state.

Unless programmed to ‘snap’ on. However, I’ll grant you that if they slow-ramp, then the odds are far better of not noticing any initial flash. Nevertheless, those color pool lights I mentioned are slow-ramp but the color flash is still noticeable (but only if you happen to be there at the precise moment they turn on, which is unlikely in the case of pool lights).

Thanks for sharing John! I’ll try to give this a shot this weekend and post back what I find.

With regards to “manually” turning the lights on, I mean with a physical wall switch. My wife and kids are not always on board for my “projects” so I’m having trouble getting them to leave the switches on. I’ll probably have to replace a few with “smart” switches, but I’m trying to see what I can get done before dropping any more cash.

In my early days I dealt with that with switch lock covers. They were a couple bucks for a 2 pack and clear in color. They go over the switch to prevent someone from switching it. If you really want to switch it there is a place you can reach under from the side to do so. They worked to “reprogram” my family to ask Alexa to turn a light on/off versus hit the switch.

Over time as HA became more of an obsession I invested in smart switches.

Or just leave the cover plates off and tell them if they try to flip the switch they will get shocked. They may believe you :wink:

OK, just checking to make sure we’re talking about the same thing when “manually turning on a switch”.

For lights controlled by wall switches, I’ve replaced the standard switches with “smart” switches. I chose to standardize on UPB but zwave, ZigBee, and Wi-Fi are more prevalent communication protocols. A typical “smart” dimmer switch controls standard incandescent and LED lights but only brightness, not color.

In one room we have sconces equipped with Hue Color bulbs, so the standard wall switch controlling them was simply taped into the on position. However, this prevents local control (i.e. you’re physically in the room but without a means to control the lights). I removed the wall switch, hard-wired the electrical connections (permanently on) then closed the box with a custom-made flush-mounted metal plate. Then I magnetically attached a Philips Hue Dimmer Switch to the plate.

The end-result is what appears to be a fairly traditional wall switch providing the convenience of local, manual control of the sconces, but with no possibility of anyone accidentally cutting power to the sconces (short of turning off the breaker).

For table and floor lamps I standardized on Philips Hue bulbs. Once again, to provide the convenience of local control, I wall-mounted a Hue Dimmer Switch in the room (minus any re-wiring of course because these are table/floor lamps). With easy access to the Dimmer Switch, there’s less temptation to manually control the table lamp via its physical switch.