Getting a push message on your phone or watch when your laptop battery is running low

When your laptop is sitting on a table and you're doing some other stuff in the meanwhile, it might quietly be running out of battery. In that case, you likely want to be nudged to find it a power outlet before it has to shut down or hibernate.

Wouldn't it be nice to get a push message on your phone (or even smartwatch, because hey, it's 2015)? You can :)

On Linux, there's udev, which takes care of a lot of the subsystems of your machines. Add a rule by creating a file called 90-lowbattery.rules in /etc/udev/rules.d/ and add the following line:

SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="20", RUN+="/root/bin/pingphone.sh battery_20percent"

You can customise the exact RUN command of course. The 'capacity' part is the percentage of battery left; in this case 20%.

If you don't know PushBullet yet, now is the time to introduce you to this excellent push platform. It's really quite nifty, cross-platform and has plenty of uses in day-to-day life. The pingphone.sh script looks like this:

#!/bin/bash
echo "`date +%Y%m%d_%H%M` $1" >> /root/bin/pingphone.log
. /root/.virtualenvs/pushbullet/bin/activate
python /root/bin/pyPushBullet/pushbullet_cmd.py yourpushbulletkey1234567890abcde note 123456 "laptop battery low" "$1"

Replace the yourpushbulletkey1234567890abcde part with your actual PushBullet API key and the 123456 with the deviceID you want the note to be pushed to. (Actually it's of the format udeCmddJpl and you can get the relevant one by doing a python /root/bin/pyPushBullet/pushbullet_cmd.py yourpushbulletkey1234567890abcde getdevices). The $1 is the message battery_20percent of the RUN command, which you can of course let say anything you like.

As you see, it uses a Python script to push a note through PushBullet. Clone pyPushBullet into a dir (like /root/bin) and correct the above command line to have it point there. As pyPushBullet needs a few support libraries, the cleanest way to install these is by creating a virtualenv and pip install them in there:

mkvirtualenv pushbullet
pip install websocket-client
pip install requests
pip install python-magic 

This creates a pushbullet directory in /root/.virtualenvs/ and installs the three dependencies in there that pyPushBullet needs to do its thing. This environment gets activated by sourcing the activate script on the third line in the pingphone.sh script, as seen above.

Now test the script by running:

/root/bin/pingphone.sh test

This should land a push message on your device.

All kinds of other things can be fired by these udev triggers. If you'd like your machine to automatically suspend for example (if it is not already configured to do so), you can have it look like this: RUN+="/usr/bin/systemctl suspend"

Comments

2 Posted at by

There is an even simpler way. Instead of using a Python-library, you can just make a POST to the Rest API using Curl.

curl --header 'Authorization: Bearer <your_access_token_here>' -X POST https://api.pushbullet.com/v2/pushes --header 'Content-Type: application/json' --data-binary '{"type": "note", "title": "Laptop battery low", "body": "The battery level of your laptop is now lower than 20%."}'

Check out the API Docs here:
https://docs.pushbullet.com/#pushes

  • Michiel Scholten
  • Home
  • 🕔

Add comment

You need to provide a valid e-mail address to comment here, but it will not be displayed on this website. HTML will be escaped, so you won't be able to add links. Post the URL instead. Line breaks will be converted to breaks.

Name

E-mail address


Mail me when someone else comments too
Your address won't be shown in your comment

WWW


Leave empty if you don't want to provide a url

Are you a spammer (yes/no)?

Be sure to save your comment after you've previewed it!