Skip to content

Instantly share code, notes, and snippets.

@bakman2
Last active November 15, 2024 06:36
Show Gist options
  • Save bakman2/0105e140fd0940c06bdb89273c4e145e to your computer and use it in GitHub Desktop.
Save bakman2/0105e140fd0940c06bdb89273c4e145e to your computer and use it in GitHub Desktop.
Home assistant adaptive light brightness based on lux sensor

Home assistant adaptive light brightness based on lux sensor

Use template sensor

- sensor:
    - name: "Adaptive brightness"
      unique_id: adaptive_brightness_id_1
      state: >-
        {% set input_value = (states('sensor.lux_sensor_illuminance_lux')|int) %}
        {% set min_input_value = 400 %}
        {% set max_input_value = 10 %}
        {% set output_min = 0 %}
        {% set output_max = 60 %}
        {% set mapped_value = (input_value - min_input_value) / (max_input_value - min_input_value) * (output_max - output_min)  %}
        {% if mapped_value < 10 %}
          {{ 0 }}
        {% else %}
          {{ mapped_value|int }}
        {% endif %}
      unit_of_measurement: '%'

This uses a lux sensor.

In this example it maps the lux sensor value from 400lx to 10lx > 0 to 60%.

Adapt the mapped values to your liking.

example: a lux sensor value of 300lx will set the value to 15% and a value of 20lx will output 58%

The output number is exposed as a state value that can be used for the brighness_pct for lights. Set a long transition time as to compensate for the lux sensor update time. The lights will dim/brighten automagically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment