Skip to content

Instantly share code, notes, and snippets.

@zouppen
Last active September 6, 2024 12:43
Show Gist options
  • Save zouppen/755855a1044fd45a8260387f176be0eb to your computer and use it in GitHub Desktop.
Save zouppen/755855a1044fd45a8260387f176be0eb to your computer and use it in GitHub Desktop.
Limit laptop battery charging at home, full charge elsewhere

Limit laptop battery charging at home

Lithium batteries wear more quickly when fully charged but of course you need the capacity on the move. Here's a solution: When at home, charge to 85% and 100% when not connected to your home network (wired or wireless).

Pre-requirements: Linux, NetworkManager

Installation

Place the following file under /etc/NetworkManager/dispatcher.d. Edit the network name list on line 7 to match your needs. Examples are Foonet and Barnet. See possible network names by running:

nmcli -t -f name connection show

Also, double-check the sysfs path to your battery. In most cases it's /sys/class/power_supply/BAT0/.

Finally, make sure file is executable and owned by root (chmod +x and chown root:root).

#!/bin/sh -eu
case "${2-}" in
up|down)
# Avoid double bookkeeping which connections are up or down,
# so just check it here.
if nmcli --terse --fields name connection show --active | grep -qE '^Foonet$|^Barnet$'; then
# Home, avoid wearing of the battery
charge=85
else
# Away, maximum charge
charge=100
fi
echo "$charge" > /sys/class/power_supply/BAT0/charge_control_end_threshold
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment