How to show the battery level of your Bluetooth mouse in the shell | Hund

Hund

How to show the battery level of your Bluetooth mouse in the shell

Wednesday, July 21, 2021

UPower is an abstraction for enumerating power devices, listening to device events and querying history and statistics.

It comes with a command-line client called upower, which makes it possible to check the status of your device via the shell. I have a wireless mouse called Logitech G305 and this is the output I get with upower --dump:

$ upower --dump
Device: /org/freedesktop/UPower/devices/mouse_hidpp_battery_0
  native-path:          hidpp_battery_0
  model:                G305 Lightspeed Wireless Gaming Mouse
  serial:               4074-61-97-4e-4d
  power supply:         no
  updated:              Wed 21 Jul 2021 12:07:44 PM CEST (4 seconds ago)
  has history:          yes
  has statistics:       yes
  mouse
    present:             yes
    rechargeable:        yes
    state:               fully-charged
    warning-level:       none
    battery-level:       full
    percentage:          100% (should be ignored)
    icon-name:          'battery-full-charged-symbolic'
  History (charge):
    1626862064	100.000	fully-charged

Daemon:
  daemon-version:  0.99.11
  on-battery:      no
  lid-is-closed:   no
  lid-is-present:  no
  critical-action: PowerOff

If I have multiple devices and would like to only show information about my mouse, I can then use the flag -i, --show-info like this:

$ upower -i /org/freedesktop/UPower/devices/mouse_hidpp_battery_0

And if I just want to print the battery percentage:

$ upower -i /org/freedesktop/UPower/devices/mouse_hidpp_battery_0 | grep "percentage" | awk '{print $2}'
100%