# Turning the lights on according to the light level outside

I have found a very crude way of turning the lights on dependending on the light level that comes from outside. This is done by a light sensor which reports the lumens. I would like my lights to turn on with incrementaly according to the lumens measure. Let’s say at 300 lumens i’d like to turn them on at 10 percent brightness 150lux 50% and so on. the only problem I’m having is that i also have a routine that turns the lights off when i leave the door open and turns them back on when i close my door. Only now the lights turn on to a set brightness percentage and i would like to turn them om as bright as they would to the according lux reading. could anyone help me with this?

Create a template sensor that maps your light level to wanted brightness. Then use this sensor to specify the brightness whenever you call the light turn on service.

Using the two points you supplied, and 100% brightness at 0 lux, gives this equation:

bri_pct = -0.3 * lux +98.33

Which you can use in a template sensor like this:

``````- name: 'Calculated Light Brightness'
unit_of_measurement: "%"
icon: "mdi:brightness-auto"
state: >
{{ ( [0, (-0.3 * states('sensor.your_lux_here')|float(0) + 98.33 )|round(0)|int(0), 100]|sort )[1] }}
``````

This maps 328 lx → 0 % through to 0 lx → 98% brightness. Note that the points you supplied do not quite cover the full 0 to 100% range, but you would not notice 2% difference between 98% and 100%, so close enough.

Output values are limited to 0% to 100% brightness by the `[0, x, 100]|sort[1]` function. It sorts the list in order of increasing value and picks the middle one. So if you had a brightness value of -7:

[0, -7, 100] it would sort to [-7, 0, 100] and pick the middle value, 0. Thus clamping -7 to 0.

How to use the sensor in an action:

``````action:
- service: light.turn_on
target:
entity_id: light.your_light
data:
brightness_pct: "{{ states('sensor.calculated_light_brightness') }}"
``````

You can use a spreadsheet to experiment with different points and see what the equation produces:

6 Likes

Note: now that the compensation sensor has limits there is a much easier way than using a template sensor: