There are similar questions and answers in Ask Ubuntu that point to these solutions:
- f.lux® software to make your life better
- Redshift adjusts the color temperature of your screen according to your surroundings.
- GNOME’s New Night Light Feature Aims to Help You Sleep Better
- Set initial startup background brightness depending on daytime
but they either require manual entry of sunrise and sunset times or they merely adjust the color balance and add more red at night.
I'm looking for a solution that automatically obtains sunrise and sunset times each day and physically adjusts the display brightness. Furthermore I want a transition effect over a period of 60 to 120 minutes so I don't notice the display change.
Re-configuring on cloudy days (dimmer outside) or when the sun happens to set between two three story buildings instead of their roof tops (brighter before sunset) should be a quick and easy process.
Best Answer
Updated Post from 2019 - Eyesome
Eyesome is a bash script running as a deamon and sleeping most of the time 24/7. It automatically adjusts screen brightness (and optionally gamma too) for your laptop display via hardware interface and up to two other monitors using xrandr's software control.
At sunrise (the time is automatically obtained from the internet each day), your screen brightness (and optionally gamma too) is adjusted gradually. The gradual adjustment is defined by you but, 120 minutes works for me. To keep the adjustments unnoticeable set a sleep interval between adjustments. Anywhere between 15 and 60 seconds is probably best and the default is 60.
Eyesome daemon sleeps many hours until sunset transition starts. Inversely to sunrise transition, the sunset transition gradually decreases screen brightness (and optionally gamma too) so it is unnoticeable. 90 minutes before sunset is recommended but you can set any period you like.
During sunset transition gamma may be defined to increase. For example Red gamma may be defined as 1.0 during day and 1.2 during night to reduce eye strain. Blue gamma in turn may be defined as 1.0 during day and .8 during night so it will decrease instead.
To reduce resources, eyesome sleeps the entire period between sunset and sunrise transitions. There are one time exceptions when resuming from suspend or hot plugging external monitors. It depends on where you live and the season of the year but, the average sleep will be 12 hours.
You can download Eyesome from Github
Original Post from 2017
Introduction
This a bash solution with no need to install additional programs. It requires an Internet connection (if you are reading this you have one) to automatically get sunrise and sunset times each day. It provides a user-friendly configuration screen. It starts up automatically with
cron
and requires no user interaction.This answer is divided into multiple parts
cron
cron
Automatically get sunrise and sunset times each day with cron
There are many websites to report sunrise and sunset times for your location. This script uses (https://www.timeanddate.com/) which has been a well-known site for a long time.
With sudo powers edit the file
/usr/local/bin/sun-hours
and paste this:Before saving the script replace the two occurrences of
/canada/edmonton
with your own country and city. Visit the sitewww.timeanddate.com
to get the exact name or number. For example, "Paris, USA" has dozens of names so they'll contain a number such as/worldclock/@5205082
for "Paris, PA USA".Use cron to run sun-hours script each day
With sudo powers edit the file
/etc/cron.daily/sun-hours
and paste this:Mark both files as executable with
sudo chmod a+x ___/sun-hours
where "___" is the directory for each file.Use Conky to monitor sunrise / sunset time changes each day
Conky is a popular tool for monitoring your system. Here are the commands for it to display sunrise, sunset and brightness setting:
This is what it looks like:
Note: this Conky code uses
intel_backlight
which you need to change depending on your display adapter as described in sections below.Configure brightness settings and transition time
You don't want your display to change to full brightness on sunrise and full dimness on sunset. A transition period is needed. Additionally a configuration file is required to record the variables for full brightness and full dim. This is the configuration screen:
The screen is built using standard zenity command and as such existing values are shown in labels with "(value)" tag and you key in new values if needed. Otherwise the existing files are kept when new values are blank.
Replace
intel_backlight
with your display driver if different. To find out your name use:ls /sys/class/backlight/*/brightness
.Using sudo powers create the file
/usr/local/bin/auto-brightness-config
and paste in this code:Mark the file as executable using:
Main program
display-auto-brightness
Using sudo powers create the file
/usr/local/bin/display-auto-brightness
and paste in this code:Mark the file as executable using:
NOTE: Unsuccessfully revised August 26, 2017 to correct bug where program would stop when last brightness level was equal to minimum or maximum brightness and current minute's adjustment value is blank (zero). Successfully fixed December 2, 2017 but not published until February 17, 2018. Ooops!
Start display-auto-brightness automatically every boot with
cron
Cron is used to start the main application every boot. Create the file
/etc/cron.d/display-auto-brightness
with sudo powers and copy in this:Instant brightness adjustment resuming from suspend
You can suspend your computer at 6 am when it before the sun rises and your setting is at 300. You can resume it at 4 pm when the sun is bright and the setting should be 2000 but you have to wait 1 to 59 seconds for
display-auto-brightness
to reset the display. When the display does reset the change is dramatic. To solve this asystemd
script is needed. Create the file/lib/systemd/system-sleep/display-auto-brightness
containing:Mark the file as executable using:
Summary
Display current brightness level is systray
In addition to Conky example presented earlier, indicator-sysmonitor you can display the current brightness in the systray through bash:
In this systray example brightness is set at
418
and it is about 20 minutes before sunset. At sunset the value will be 250 and the daytime full brightness is 1200. On this machine intel_backlight=4882 is possible but, if you are indoors with curtains open the screen is like staring into the sun.The systray indicator is setup using this code:
This Q&A (Can BASH display in systray as application indicator?) describes how to setup indicator-sysmonitor.
Future Enhancements
xrandr
xrandr