Adaptive display brightness control based on ambient brightness using the Home Assistant REST API

After years of not further thinking about it, I recently became fed up with the fact that run-of-the-mill office monitors do not have a way of automatically adjusting their brightness based on their surroundings, like many laptop monitors can.

But if you happen to already have your own local Home Assistant instance, there is a pretty neat and budget friendly way to accomplish just that.

If you don’t want to rely on Home Assistant, you can also hook up a sensor directly to your computer and use software to read its values. Go here to learn more.

Prerequisites

There are a few things you need. You need…

  • a Home Assistant instance accessible to you with sufficient permissions, which is also necessary for…
  • a Home Assistant REST access token
  • a light sensor hooked up to your Home Assistant, which supplies the ambient brightness values. I am using a zigbee compatible light sensor from xiaomi which does the job pretty well.
  • a computer having the following packages installed
    • python (+ pip, just in case you are missing python dependencies)
    • ddcutil (Windows implementation of ddcutil: Github; MacOS implementation is also available: Github) on Arch: $ pacman -Syu ddcutil

Execution

Once you have all prerequisites checked, download a small python script that I wrote: Github
It is necessary that you alter a few parameters to fit your environment before you execute it.

Please pay special attention to the parameters defined between line 16 and 34. Make sure that all these parameters are according to your environment and your liking:

[...]
# set your private home assistant rest token
token = "SECRETTOKEN"
# set the sensor name that you want to query
sensor = "sensor.balcony_light_illuminance_lux"
# set the relevant json field name
field = "illuminance_lux"
# set host ip and port
host = "192.168.0.1:8123"
# set illuminance value at which the monitor is set to maximum brightness
illuminance_max = 10000
# set illuminance value at which the monitor is set to minimum brightness
illuminance_min = 500
# set maximum monitor brightness
brightness_max = 100
# set minimum monitor brightness
brightness_min = 20
# set query interval in seconds
interval = 60
# get the id of the corresponding ddc brightness parameter, typically it is 10
ddcattr = "10"
# verify TLS certificate? Set to false because of reasons
verifyTLS = False
[...]

Do understand that the content of the JSON that is returned by Home Assistant depends heavily on the sensor you use and therefore your JSON might be structured differently. The script helps you by printing out the complete JSON before failing. Make sure that you change line 53 according to your needs.

This is how my JSON output looks like. Yours will look different:

{'entity_id': 'sensor.balkon_lichtsensor_illuminance_lux', 'state': '0',\
'attributes': {'state_class': 'measurement', 'battery': 100, 'illuminance': 0, \
'illuminance_lux': 0, 'linkquality': 81, 'voltage': 3200, 'unit_of_measurement': \
'lx', 'device_class': 'illuminance', 'friendly_name': \
'Balkon_Lichtsensor_illuminance_lux'}, 'last_changed': \
'2022-02-11T16:52:33.547092+00:00', 'last_updated': \
'2022-02-11T21:48:55.763008+00:00', 'context': {'id': \
'18c19154b14243c52b68d8b597205187', 'parent_id': None, 'user_id': None}}

Once you are done with the details, run the python script. It will now periodically check the ambiance brightness, calculate the target display brightness and set it by creating a smooth transition using ddcutil.

Dont forget to make the script autostart on boot and have fun!

More ideas

If you have an old webcam laying around and don’t want to spend money on a separate light sensor, it might be worth to check out clight. Clight is a Linux daemon that makes use of a webcam to calculate the average brightness and sets the display brightness accordingly. But since I wanted to make use of ambience brightness values in my smart home setup using Home Assistant anyway, I went with a separate sensor. And you can even share the sensor data with your friends!

To all the Mac users: If you’re looking for a way to have this functionality but don’t have access to Home Assistant, you can easily assemble your own sensor and hook it up to your computer. Having a sensor directly attached to your PC allows you to use paid, but user-friendly solutions like Lunar or MonitorControl.

If you’re suffering from rapidly changing light conditions inside your room and want to keep the display brightness somewhat stable, why not setting up your sensor outside? That’s exactly what I do. This helps compensate for changing light and weather conditions throughout the day which was my reason to realize this small project.

Leave a Reply

Your email address will not be published. Required fields are marked *