Ubuntu – How to get a notification using Notify-OSD when the charger is inserted/removed

batterynotificationnotify-osd

How can I get an on-screen notification using notify-osd when I plug/unplug the charger?

Best Answer

enter image description here enter image description here

dbus

Some people reported my earlier udev solution sent the notification too many times when the power cable was plugged in. I couldn't reproduce that but I wrote this python script to utilize dbus instead of udev. Save it as a .py file somewhere on your hard drive. Mark the file executable by running:

sudo chmod +x yourFile.py  

and add it to your startup applications as described here. This script requires the package acpi be installed.

#!/usr/bin/python

import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject
import subprocess


dbus_loop = DBusGMainLoop()
bus = dbus.SystemBus(mainloop=dbus_loop)

onMessage="Power plugged in!"
offMessage="Power unplugged!"
onImage="/usr/share/icons/gnome/32x32/devices/ac-adapter.png"
offImage="/usr/share/icons/gnome/32x32/status/battery-full.png"

def callback():
    state = subprocess.check_output(["acpi", "-a"]).split(':')[1].strip()
    if state == "on-line":
        subprocess.call(["notify-send", "-i", onImage, onMessage])
    elif state == "off-line":
        subprocess.call(["notify-send", "-i", offImage, offMessage])

bus.add_signal_receiver(callback, 'Changed', 'org.freedesktop.UPower.Device', 'org.freedesktop.UPower', '/org/freedesktop/UPower/devices/line_power_AC')

loop = gobject.MainLoop()
loop.run()

udev

With a little experimentation (and a little help) I was able to utilize a udev rule to accomplish this. Some people have reported that it sometimes sends the notification more than once but I have not had any problems. YMMV.

Create a script with the following contents:

#!/bin/bash

# Set this to your username
USER="some_user"

if [ "$POWER" == "on" ]
  then
  DISPLAY=:0 /bin/su $USER -c '/usr/bin/notify-send -i /usr/share/icons/gnome/32x32/devices/ac-adapter.png "Power cable plugged in."'
elif [ "$POWER" == "off" ]
  then
  DISPLAY=:0 /bin/su $USER -c '/usr/bin/notify-send -i /usr/share/icons/gnome/32x32/status/battery-full.png "Power cable unplugged."'
fi

replacing some_user with your username. Mark the file executable by running:

sudo chmod +x /path/to/script.sh  

replacing /path/to/script.sh with the path to where you saved the script.

Next create a file in /etc/udev/rules.d named 10-power.rules with the contents:

SUBSYSTEM=="power_supply", ACTION=="change", ENV{POWER_SUPPLY_ONLINE}=="0", OPTIONS+="last_rule", RUN+="/path/to/script.sh" ENV{POWER}="off"
SUBSYSTEM=="power_supply", ACTION=="change", ENV{POWER_SUPPLY_ONLINE}=="1", OPTIONS+="last_rule", RUN+="/path/to/script.sh" ENV{POWER}="on"

again replacing /path/to/script.sh with the path to the script you created earlier.

Now reload the udev rules by running:

sudo udevadm control --reload-rules                              

Unplug the power cable. You should get a notification.