Dim lights as lumens increases

Okay,
So right now I’ve got my lights coming on when the lumens are below 280 and they turn off when the lumens are above 280.
But I really could extend that you to 350lux.

Ideally the lights wouldn’t be very bright at 350lux , maybe 20%, and then as the lux decreases the brightness increases up to 100% when the lux is at 200. And then obviously the reverse as well.

OK lets call the luminance sensor sensor.lum and the light entity light.desk.

Ypur trigger is going to be a change of state of sensor.lum

Your condition is going to be what time of day you want this to operate (if needed)

Your action is going to be a template that calculates the brightness according to this formula: brightness = -3.36 * sensor.lum + 1196 (assuming brightness 0-255), and then calls the service to set the brightness of light.desk to that figure.

It is my bedtime, so I haven’t allowed for the

  1. the edge cases where sensor.lum is not within the range 280-350; or

  2. Actually writing the template,

But I hope that helps :slight_smile:

Just for my understanding, your light sensor isn’t actually measuring the light in your kitchen?

No, it is. I just want my lights in the kitchen to dynamically adjust depending on how much light the sensor is reading.

Brilliant.
Thank for this. I’ll try and put something together.

@nickrout Are you able to let me know how you calculated this formula? It seems in one of the latest updates my lumens sensor has changed and the values are now completely different.
It now looks like the lumens range I need is now 0-500.

Here’s what I have done so far (haven’t finished the brightness template yet).

- id: dynamically_adjust_kitchen_lights
  alias: dynamically adjust kitchen lights
  trigger:
    platform: state
    entity_id: sensor.kitchen_sensor_illuminance_lux
  condition:
    condition: time
    after: "06:00:00"
    before: "18:30:00"
  action:
    service: light.turn_on
    data_template:
      entity_id: light.kitchen_pendant_lights
      brightness: >
      -3.36 * state_attr(trigger.entity_id, 'illuminance_lux') + 1196

Obviously the brightness template isn’t finished but I can;t finish it until I can figure out how that calculation should be now given the new lumens range.

Any help would be greatly appreciated!

Thank you!

Actually I realise in reviewing this to explain it that I made an error earlier!

First of all visualise your data, It is a straight line as we only have two points.

ambient light = 280, brightness = 255
ambient light = 350, brightness = 51 (ie 20% of 255)

Plot it on a piece of paper.

Imgur

Every straight line can be defined by the formula

y = (slope * x) + constant

so in this case

brightness = (slope * lux) + constant

Now the slope of the line is:

(51-255)/(350-280)
= -204/70
= -2.91 (I got -3.36 last time, don’t worry how, I just did, sorry bout dat)

To work out constant, take one of those points we know about and work it out. We’ll use the left hand point, lux = 280, brightness = 255

We know

brightness = (slope * lux) + constant
255 = (-2.91 * 280) + constant
255 = -814.8 + constant
so constant = 1069.8 (once again different to last time because of the wrong answer for slope)

Try it out for the other point, just to check:

lux = 350, brightness = 51

brightness = (-2.91 * lux) + 1069.8
51 should equal (-2.91*550) + 1069.8) (in fact it is .3 out because of rounding errors, but that is acceptable.

So in short

brightness = (-2.91 * lux) + 1069.8

Or just use this page :slight_smile:

https://www.mathportal.org/calculators/analytic-geometry/two-point-form-calculator.php

On nice.
Okay, so here’s my plot after updating the values with new lux values.
https://www.mathportal.org/calculators/analytic-geometry/two-point-form-calculator.php?val1=50&val2=255&val3=500&val4=0&rb1=gen

Basically i’m still using the brightness values of 0 to 255, but the lux values have changed to be 50 to 500 (at 500 I basically want the lights off).

What i’m struggling with is how to template this equation. I’m still new to templating so it’s daunting for me.

I’ll have a play.

1 Like

I came up with this from the above slope.
{{ (-0.41 * states('sensor.kitchen_sensor_illuminance_lux')|int) +255 | round }}

Not sure if the math was right though.

But on the graph you posted above it should be

{{ (-0.57 * states('sensor.kitchen_sensor_illuminance_lux')|int) +283 | round }}

17/30 = .56666666 round to .57
850/3 = 283.3333 round to 283

hmm so how would I stop it going over 255 for the brightness. Because it’s trying to set 261 as the brightness right now when the lumens are at 39.

You only defined the problem for lumens between 50 and 500. If you want to allow for other use cases then yes, you’ll need to do something like:

if lumens < 50 brightness = 255
else (your formula above)

ah of course.
Perfect!
Thank you very much for helping me get to this.

It’s quite fun to see my light dynamically adjust though out the day. it seems my lumens sensor checks in every minute or less so it’s near real time!

for completeness,
Here is the final automation for anyone else looking at this.

- id: dynamically_adjust_kitchen_lights
  alias: dynamically adjust kitchen lights
  trigger:
    platform: state
    entity_id: sensor.kitchen_sensor_illuminance_lux
  condition:
    condition: time
    after: "06:00:00"
    before: "18:30:00"
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.kitchen_pendant_lights
        brightness: >-
          {%- set kitchenbrightness = (-0.57 * states('sensor.kitchen_sensor_illuminance_lux') | float) + 283 %}
          {% if states('sensor.kitchen_sensor_illuminance_lux') < '50' %}
            255
          {% else %}
            {{ kitchenbrightness | round }}
          {% endif %}
1 Like

Not sure if you aware but perception of brightness is logarithmic not linear, therefore, linear translation of the brightness measured by sensor to brightness of a lamp might not give desired result.

Indeed, do you have a better solution?

The solution should be to map brightness meassured by the sensor in logarithmic scale, e.g.: 500 lx and more: 0% lamp brightness; 375: 1.6% brightness; 255: 6.25% brightness; 155: 25% brightness; 0 lx: 100% brightness. It is easier btw to use brightness_pct. The numbers given are some approximations.

I was trying to get away from having to create “steps” of brightness and have it seem more fluid.
I was also using brightness over brightness_pct as brightness_pct is being deprecated.

If I can see an example of how to do this logarithmic rather than the way I have it now that’d be great.