Help me make a great dynamic light automation script

This is my first attempt at making a proper advanced automation in HA, even though I’ve had the system for a couple of years. I’ve got programming experience, but I’m not used to the ins and outs of HA, and I’m sure there are plenty of pitfalls. So I’m turning to the forum for help, hopefully you can help me realize this idea.

I live in Norway and we have very changeable light conditions. Not only is no day the same with regards to how long the sun is up, but the light during the day varies a lot throughout the year as well. It’s a lot of rain and dark days too, and so I want to make my apartment just the right amount of bright and cozy in any day, by pure automation.

I have a Sensative Strip via Zwave with a Lux sensor outside, and a plethora of different Zigbee lights inside. I’ll be focusing on the living room for now. The behaviour I would like is as follows:

  1. After [morning], the most recent value of the lux sensor will be read, and an approperiate set-point will be targeted. The light will slowly fade to this during the course of 30 minutes.
  2. Between 9 and 18, the light target will be set high
  3. Between 18 and [bedtime], the target will be set to dimmed
  4. Light will dim to 0 for 45 minutes after [bedtime]

If there is too little light outside, the artificial light will boost the inside level. When it’s bright enough, the artificial light will fade to 0%. It will start to compensate for the fading light at dusk also.

My sensor is a power-saving one and will only report every 1 or 2 hours, except for if it notices a doubling of half of the lux value.


This one shows my idea of how to add the light on a bright day

It looks like I should make three automations, one for morning, one for evening, and one for the interval in between. I’ll focus on the daytime one.

I was thinking to do like this:

 automation:
  - alias: "Autolights"
   trigger:
      platform: event
      event_type: state_changed 
 	 entity_id: sensor.sensative_strips_comfort_luminance
   condition:
 	condition: or
 	conditions:
		- condition: numeric_state
		entity_id: sensor.sensative_strips_comfort_luminance
**			if (val < 10lux) { brightness = 30%} # If it's pretty dark, set this as a baseline**
 **	                else if (val < 100lux) {brightness = 50%} #If it's brighter, set new set point**
 **	                else if (val < 1000lux {brightness = 85%}**
 **	                else if (val < 3000lux) {brightness = 50%} #Start to fade them down again here**
 **	                 else (brightness == 0%) #If it's really bright, shut off**

Now I’ve tried to bold out the part where I’m not sure how to best write it. And of course I could do more setpoints than this. I’ve got some questions though;

  1. Will the lights turn on if I just dim them with a brightness >0?
  2. How can I make this happen as smooth as possible? I’d like to fade into the new state over the course of 30 minutes.
  3. Does my code look right?

Many thanks for your time!

You are trying to set the brightness value in the condition block. Try doing this in the action block instead.

I use a sensor to calculate what the brightness of my lights should be so I only have to calculate it once and can use it in many automations:

- platform: template
  sensors:
    calculated_light_brightness:
      friendly_name: 'Calculated Light Brightness'
      value_template: >
        {% if states('sensor.outside_light_level')|is_number %}
          {{ ( [0, (-0.1785 * states('sensor.outside_light_level')|float + 298.35)|int]|max , 255 )|min }}
        {% else %}
          {{ states('sensor.calculated_light_brightness') }}
        {% endif %}
      unit_of_measurement: "Int"

The value is limited to 0 to 255 by the min and max filters and the main equation I use is:

calculated_light_brightness = -0.1785 * outside_light_level + 298.35

I came up with this equation by observing the outside light level and what I liked the inside brightness set to over a number of weeks. I plotted the values in Excel and generated the equation by fitting a trend line to the graph.

This equation is specific to my light sensor, my perceived brightness inside and my requirements. You will probably have to adjust it if you use it.

3 Likes