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.

  • 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" :) – WinEunuuchs2Unix 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/… – WinEunuuchs2Unix 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. – WinEunuuchs2Unix 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.

  • 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. – WinEunuuchs2Unix Oct 2 '16 at 18:43
  • @WinEunuuchs2Unix thats just an example. He can configure the file for custom time range and percentage :) – Sergiy Kolodyazhnyy Oct 2 '16 at 18:55
1

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.

After sunrise transition is complete, eyesome daemon sleeps many hours until sunrise transition starts. I'm using 90 minutes before sunrise but you can set any period you like.

Inversely to sunrise transition, the sunset transition gradually decreases screen brightness (and optionally gamma too) so it is unnoticeable.

Note that during nighttime 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. Depending on where you live and the season of the year, the average sleep will be 12 hours.

Eyesome Setup - Main Menu

To configure eyesome, a main menu is provided:

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

Your country/city name should automatically appear. If necessary you can override it.

Edit Configuration - Monitor 1 tab

Clicking on Monitor 1 tab above will reveal this panel in my configuration (yours may be different):

eyesome-edit-configuration-monitor-1.png

Don't be daunted by these settings they are for the most part automatically obtained by eyesome. You will need to set the daytime and night brightness/levels though.

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

If after eyesome is installed you attach a different monitor to your system 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 5 seconds up to 20 seconds from the Edit Configuration - General Tab.

Here's what the 5 second Nighttime test looks like:

eyesome nighttime test.gif

Unusual event handling

Assume you suspend your laptop when it's morning before 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. The screen is so dim you can't read it.

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 reset brightness to full and then sleep until sunset transition.

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.

    Suspend/Lid close/Test brightness/power off/hotplug

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

This program was just released in September 2018 so please let me know if you find any problems or have suggestions for improvement.

The documentation phase is just starting so don't hesitate to ask any questions. Your questions may even result in documentation improvements.

  • Part of it is WIP answer as I'll add cron hourly bash script later that sets brightness based on daylight hours. The other part is to answer your question on how daylight hours can be found in the link posted. You could integrate that link into Serg's answer. But maybe when it's all working I should post a new Q&A is your suggestion??? (Serg had concerns in chat room 1/2 hour ago) – WinEunuuchs2Unix Feb 25 '17 at 19:28

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.