Get battery status to update more often or on AC power/wake

An annoying problem I have is that upower will take a few minutes or more to update my battery status (This problem is similar to this post but I provide my laptop's details). That means if I wake my laptop from hibernation it shows the wrong battery percentage for a few minutes, and when I plug in my charger the battery icon does not indicate so (though the hardware charge light turns on). Other than those cases I don't mind the update time.

Curiously, when I unplug my charger, the icon updates immediately. I've come up with a temporary solution to update on wake that may or may not work but it seems to be there should be a good fix. Running Ubuntu 16.04 on Acer Aspire E5-575G-53VG.

acpi always shows the correct battery percentage and status. sudo service upower restart forces the battery status of upower to update.

Output of upower --monitor-detail when plugging in charger:

Monitoring activity from the power daemon. Press Ctrl+C to cancel.
[00:55:46.411]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:55:46 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.9157 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         6.6933 W
    voltage:             12.562 V
    time to empty:       7.6 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (rate):
    1485928546  6.693   discharging
    1485928537  10.856  discharging
    1485928517  6.083   discharging
    1485928504  10.423  discharging
    1485928485  6.149   discharging
    1485928466  21.190  discharging
    1485928463  6.371   discharging
    1485928449  7.071   discharging

[00:55:46.414]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:55:46 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.9157 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         6.6933 W
    voltage:             12.562 V
    time to empty:       7.6 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (rate):
    1485928546  6.693   discharging
    1485928537  10.856  discharging
    1485928517  6.083   discharging
    1485928504  10.423  discharging
    1485928485  6.149   discharging
    1485928466  21.190  discharging
    1485928463  6.371   discharging
    1485928449  7.071   discharging

[00:55:46.417]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:55:46 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.9157 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         6.6933 W
    voltage:             12.562 V
    time to empty:       7.6 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (rate):
    1485928546  6.693   discharging
    1485928537  10.856  discharging
    1485928517  6.083   discharging
    1485928504  10.423  discharging
    1485928485  6.149   discharging
    1485928466  21.190  discharging
    1485928463  6.371   discharging
    1485928449  7.071   discharging

[00:55:46.419]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:55:46 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.9157 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         6.6933 W
    voltage:             12.562 V
    time to empty:       7.6 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (rate):
    1485928546  6.693   discharging
    1485928537  10.856  discharging
    1485928517  6.083   discharging
    1485928504  10.423  discharging
    1485928485  6.149   discharging
    1485928466  21.190  discharging
    1485928463  6.371   discharging
    1485928449  7.071   discharging

[00:55:46.422]  device changed:     /org/freedesktop/UPower/devices/line_power_ACAD
  native-path:          ACAD
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:55:46 AM EST (0 seconds ago)
  has history:          no
  has statistics:       no
  line-power
    warning-level:       none
    online:              yes
    icon-name:          'ac-adapter-symbolic'

[00:55:46.422]  device changed:     /org/freedesktop/UPower/devices/line_power_ACAD
  native-path:          ACAD
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:55:46 AM EST (0 seconds ago)
  has history:          no
  has statistics:       no
  line-power
    warning-level:       none
    online:              yes
    icon-name:          'ac-adapter-symbolic'

[00:55:46.422]  daemon changed:
  daemon-version:  0.99.4
  on-battery:      no
  lid-is-closed:   no
  lid-is-present:  yes
  critical-action: HybridSleep

Output when I unplug charger:

Monitoring activity from the power daemon. Press Ctrl+C to cancel.
[00:58:17.752]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.765]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.773]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.781]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.789]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.797]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.803]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.809]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.811]  device changed:     /org/freedesktop/UPower/devices/line_power_ACAD
  native-path:          ACAD
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          no
  has statistics:       no
  line-power
    warning-level:       none
    online:              no
    icon-name:          'ac-adapter-symbolic'

[00:58:17.811]  device changed:     /org/freedesktop/UPower/devices/line_power_ACAD
  native-path:          ACAD
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          no
  has statistics:       no
  line-power
    warning-level:       none
    online:              no
    icon-name:          'ac-adapter-symbolic'

[00:58:17.811]  daemon changed:
  daemon-version:  0.99.4
  on-battery:      yes
  lid-is-closed:   no
  lid-is-present:  yes
  critical-action: HybridSleep

What I noticed is that when the charger is plugged in, the battery state is still discharging. However, /org/freedesktop/UPower/devices/line_power_ACAD reports online correctly. Any help with this problem is appreciated. I do not recall having this issue on KDE.


While working on one of my previous projects, I discovered that the power indicator on Unity indeed has apparently event-driven delay for updating, and it stems form UPower daemon, which is what the indicator seems to use to obtain power information. This gets in the way of custom scripts that try to monitor battery usage via the UPower daemon with certain amount of precision. However, the UPower service does have calls for updating/refreshing that information. The script below takes advantage of that dbus call. The script is to be copied and saved as a file, preferably in ~/bin folder.

Usage is very simple: ./update_battery.py <SECONDS> , where <SECONDS> is how often you want to refresh the information. Obviously, you have to keep in mind that very frequent refreshing will lead to higher resource usage, so I would recommend something more or less sensible, for instance 3 - 10 seconds.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import dbus
import time
import sys

def run_dbus_method(bus_type, obj, path, interface, method, arg):
    if bus_type == "session":
        bus = dbus.SessionBus()
    elif bus_type == "system":
        bus = dbus.SystemBus()
    else:
        return None

    proxy = bus.get_object(obj, path)
    dbus_method = proxy.get_dbus_method(method, interface)

    return dbus_method(arg) if arg else dbus_method()

def find_battery_path():
    call = [ 'system', 'org.freedesktop.UPower', 
             '/org/freedesktop/UPower', 'org.freedesktop.UPower',
             'EnumerateDevices', None ]
    devices = run_dbus_method(*call)
    for i in devices:
        if 'BAT' in i: return str(i)

def main():
    bat_path = find_battery_path()
    call = [ 'system', 'org.freedesktop.UPower',
             bat_path, 'org.freedesktop.UPower.Device',
             'Refresh', None ]

    timeout = int(sys.argv[1])
    while True:
        run_dbus_method(*call)
        time.sleep(timeout)

if __name__ == '__main__': main()

Sergiy's answer worked, but it was causing some micro-stutter performance issues for me. I modified it to be more efficient.

first off, I modified update-battery.py to remove the sleep loop:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import dbus


def run_dbus_method(bus_type, obj, path, interface, method, arg):
    if bus_type == "session":
        bus = dbus.SessionBus()
    elif bus_type == "system":
        bus = dbus.SystemBus()
    else:
        return None

    proxy = bus.get_object(obj, path)
    dbus_method = proxy.get_dbus_method(method, interface)

    return dbus_method(arg) if arg else dbus_method()


def find_battery_path():
    call = ['system', 'org.freedesktop.UPower',
            '/org/freedesktop/UPower', 'org.freedesktop.UPower',
            'EnumerateDevices', None]
    devices = run_dbus_method(*call)
    for i in devices:
        if 'BAT' in i:
            return str(i)
    return None


def main():
    bat_path = find_battery_path()
    call = ['system', 'org.freedesktop.UPower',
            bat_path, 'org.freedesktop.UPower.Device',
            'Refresh', None]

    run_dbus_method(*call)


if __name__ == '__main__':
    main()

then I made a separate bash script that calls update-battery.py every 5 seconds, but only calls it when necessary. I called this keep-battery-updated and added it as a startup script.

#!/bin/bash
#
# @version      1.0
# @script       keep-battery-updated
# @description  keeps the battery status updated
#
##

while sleep 5; do
    badbattery=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep state: | cut -d ':' -f 2 | xargs)
    case $badbattery in
        discharging)
            badbattery="Discharging"
            ;;
        charging)
            badbattery="Charging"
            ;;
        fully-charged)
            badbattery="Full"
            ;;
    esac
    goodbattery=$(acpi -V | grep Battery | sed -n 1p | cut -d ':' -f 2 | cut -d ',' -f 1 | xargs)

    if [ $badbattery != $goodbattery ]; then
        python3 /usr/local/bin/update-battery.py
    fi
done

Now the battery is updated every 5 seconds, with no noticeable performance issues so far