8

I run Ubuntu 16.04 on an Acer Aspire E5-773G notebook. Unfortunately, I can not use my brightness function keys on the login screen, so the first moment when I am able to change my screen backlight brightness is after successfully entering my password and logging in.

By default, Ubuntu boots with the brightness level that has been set at the last shutdown, but this is not very useful as e.g. sometimes I shut down late at night with a brightness level around 5% and start it up again the next day where I would need a brightness level of at least 50% to comfortably see everything. The other way round, if I shut down during the day with maybe 80% brightness and boot it up in the evening, I nearly get blinded by the lights...

How can I configure Ubuntu to boot with a brightness level depending on the current daytime? I would like something like 10% brightness from 22:00 to 06:00, 60% brightness from 10:00 to 16:00 and 30% brightness in between. Smoothing the curve is okay, as long as it stays easily configurable. Alternatively, if you find a way to set it based on local sunset/sunrise times, that would be a big bonus. :)

Modifying brightness according to these rules should only take place during boot or optionally when waking up from suspend. It may not automatically change brightness while I'm working.

9
  • When I first converted from 14.04 to 16.04 I noticed systemd was setting the last brighness which was an eye-doctor's dream because 14.04 always went full blast and you had to manually change it. I imagine something can be rigged where multiple configurations are stored, and then set based on time of day. Besides asking for "local sunset/sunrise times" you can also ask for "cloudy vs sunny" :) Sep 26 '16 at 10:53
  • This would be helpful for doing it based on light intensity in the room (or outside on the beach): ()[askubuntu.com/questions/23593/… Sep 26 '16 at 11:01
  • I've got tape on my webcam as I normally don't use it. Also, setting the initial brightness based on fixed, configurable daytime hours is totally sufficient if I can adapt the values a few times a year. No weather dependent stuff, that's unnecessary.
    – Byte Commander
    Sep 26 '16 at 11:04
  • Yeah I have masking tape on mine too. Big brother knows too much already. Sep 26 '16 at 12:14
  • Have you tried using xbacklight to set the brightness of the screen? After it is installed it would be like xbacklight -set 10 for 10% brightness. If it works it could easily be added to a cronjob or something like that.
    – Terrance
    Sep 29 '16 at 22:03
4
+50

Basic idea:

  1. 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.

  2. 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()
    
  3. 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.

  4. 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.

2
  • 1
    A minor detail perhaps because I don't know python and the ramifications but /opt/.time_brightness_values is missing settings for 11pm to 4am. I know Byte's mother wants him to be sleeping then but he might be breaking bed-time rules. Oct 2 '16 at 18:43
  • @WinEunuuchs2Unix thats just an example. He can configure the file for custom time range and percentage :) Oct 2 '16 at 18:55
1

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:

eyesome main menu.png

Edit Configuration - General tab

When you click the Edit button from the main menu the edit configuration general tab initially appears as shown below.

eyesome configuration general tab.png

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):

eyesome-edit-configuration-monitor-1.png

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.

enter image description here

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:

eyesome nighttime test.gif

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:

eyesome override main window.png

No doubt your first step would be to click the Help using this window button:

eyesome override help button.png

Assume we click the Get button and select Monitor 3, Nighttime setting:

eyesome override monitor 3 nightime.png

Now that we have default values in memory click the Color button:

eyesome override color temperature to gamma.png

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.