Set initial startup background brightness depending on daytime
Solution 1:
Basic idea:
-
Save configuration file
/opt/.time_brightness_values
:{ "5,7": 0.25, "8,16": 0.50, "17,22": 0.75 }
Pay attention to quoted values and commas. First column with quoted values represents hour range in 24 hour format, the second column is percentage. For example, if you booted from 5 to 7 o'clock your brightness would be set to 25 percent. If you booted from 8 to 16 o'clock, it would be 50 percent.
-
Save the script below as
/opt/set_timed_brightness.py
#!/usr/bin/env python3 from __future__ import print_function from collections import OrderedDict import json import time import sys import os def read_config(): dir = '/opt' filename = '.time_brightness_values' conf_file = os.path.join(dir,filename) brightness = None with open(conf_file) as f: try: data = json.load(f) data = OrderedDict(sorted(data.items())) except Exception as e: print(e) sys.exit(1) else: keys = [ key.split(',') for key,value in data.items()] keys.sort() hour = time.localtime().tm_hour for key in keys: if int(key[0]) <= hour and int(key[1]) >= hour: brightness = data[','.join(key)] return brightness def set_percentage(pcent): dir = os.listdir('/sys/class/backlight') dev = os.path.join('/sys/class/backlight',dir[0]) max = None with open(os.path.join(dev,'max_brightness')) as f: max = f.readline().strip() new = int(float(max)*pcent) new = str(new) with open(os.path.join(dev,'brightness'),'w') as f: f.write(new) def main(): percentage = read_config() if percentage: set_percentage(percentage) if __name__ == '__main__': main()
Set
greeter-setup-script=/opt/set_timed_brightness.py
in the/etc/lightdm/lightdm.conf
file. The greeter setup script runs as root and once your system gets up to the login screen, the script will run and set the brightness you need.-
As far as waking up from suspend goes, create
/etc/pm/sleep.d/set_timed_brightness.sh
:#!/bin/bash case "${1}" in resume|thaw) python3 /opt/set_timed_brightness.py ;; esac
Remember: all the standard rules apply, the scripts must be made executable with chmod +x
, naming and calls to each file have to be consistent.
Solution 2:
eyesome
Eyesome is a bash script running as a daemon 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
software control.
At sunrise (the time is automatically obtained from the internet each day), your screen brightness and gamma (aka color temperature, hue or tint) is adjusted gradually. Before sunset gradual adjustments are made for nighttime settings. In between these periods they eyesome daemon sleeps many hours.
What distinguishes eyesome from Redshift, Night Light and similar products is the ability to simultaneously control three monitors with unique settings. Also notable is eyesome controls brightness (both hardware and software) in addition to color temperature.
Eyesome Setup - Main Menu
When you run sudo eyesome-cfg.sh
this main menu appears:
Edit Configuration - General tab
When you click the Edit button from the main menu the edit configuration general tab initially appears as shown below.
If the "brightness update interval" too short can cause too small hardware brightness adjustments that some laptops will ignore. If your screen isn't dimming before sunset, increase the interval to 60 seconds or more. You can watch the current brightness level change on the Monitor Tab. If for example you see the level changing by less than 21 each update interval, it's too short for a Dell Alienware 17R3 laptop. Yet a brightness change of 1 works fine on a Dell Inspiron 17" 7720 SE laptop.
Your "country/city name" should automatically appear. If necessary you can override it.
The check box for "Watch external monitor plugging / power switching" also applies for random times where some application invokes the color management dbus daemon that resets xrandr
to 100% brightness and gamma. Unless there is a good reason not to, you should check this box.
Edit Configuration - Monitor 1 tab
Clicking on Monitor 1 tab reveals this panel in my configuration (yours may be different):
Don't be daunted by these settings because most are automatically obtained by eyesome. You will need to set the daytime and night brightness/levels though.
If the gamma values for Red, Green and Blue are intimidating, you can fine tune them later with the Override button from the Main Menu. There you have access to a slider control for color temperature where 3500 is typical nighttime setting and 6500 is typical daytime setting. Typical daytime setting is 1.0 for Red, Green and Blue channels which is every systems default.
Edit Configuration - Monitor 3 Tab
Clicking on Monitor 3 Tab reveals this panel in my configuration (yours may be different):
Monitor 2 Tab is not shown because it is a new TV with adaptive brightness and Smart OS. It requires no overrides by Eyesome.
When eyesome is installed configuration is automatically created for the most part. If later on you attach a different monitor you may have to enter the xrandr
monitor name.
Eyesome Setup - 5 second test
From the main menu you can test your daytime and nighttime brightness and gamma settings for 5 seconds by clicking the Daytime and Nighttime buttons respectively. You can change the duration of the test from 1 second up to 20 seconds from the Edit Configuration - General Tab.
Here's what the 5 second Nighttime test looks like:
In real life you see the screen dim dramatically if you run the test in the daytime. Screen recorders used to make above .gif animation cannot record the brightness or color temperature (gamma) change though.
Eyesome Override
When you click the Override button from the Main Menu this window appears:
No doubt your first step would be to click the Help using this window button:
Assume we click the Get button and select Monitor 3, Nighttime setting:
Now that we have default values in memory click the Color button:
Grab the slider and move it left to for warmer/redish color setting at night. Move the slider right for colder/bluish color setting at day. Click the Convert button to calculate Red, Green, Blue gamma channels or Quit button if you changed your mind.
Unusual event handling
Assume that you suspend your laptop in the morning before heading to work and the screen is at full dim. You come home after work when the sun is high in the sky and open your laptop. Now the screen is so dim it's hard to read.
To address this scenario a systemd control file is provided:
-
/etc/systemd/system-sleep/systemd-wake-eyesome
control file is called whenever the system suspends or resumes. - The control file calls the bash script
/usr/local/bin/wake-eyesome.sh
to set brightness according to time and then sleep until next transition period.
You are watching a movie on your external TV at night and close your laptop lid for better viewing. Ubuntu / Lightdm / xrandr takes a few seconds and then resets your external TV to full full brightness. OUCH to your eyes.
To address this scenario an acpi event control file is provided:
-
/etc/acpi/event/lid-event-eyesome
control file is called whenever the laptop lid is opened or closed. - The control file calls the bash script
/etc/acpi/acpi-lid-eyesome.sh
to handle the lid opening and closing. - In turn the eyesome bash script calls
/usr/local/bin/wake-eyesome.sh
to reset brightness for nighttive viewing and then sleeps until sunset transition.
Summary
This answer is almost two years to the day after the question was posted. There was an early version of this answer deleted in February 2017 which I've just undeleted and revised.
Eyesome can be downloaded from: https://github.com/WinEunuuchs2Unix/eyesome
Eyesome was released in September 2018. Then it revised in June 2020 with color temperature features to ease burden of entering red, green and blue gamma channels. Even more changes are planned to enter sunrise and sunset times manually.