Skip to content

Battery charge limit with batmgt

KDE Power Management can set a charge limit, but the setting does not survive a reboot (KDE Bug 450551). The Battery Management Utility bat from tshakalekholoane/bat writes the threshold and keeps it through a systemd unit.

The package below is not in the official repositories or the AUR. You must update it by hand when upstream changes.

Terminal window
pkgname=batmgt
pkgver=1.2
pkgrel=1
pkgdesc="Battery management utility for Linux laptops (renamed to batmgt)"
arch=('x86_64')
url="https://github.com/tshakalekholoane/bat"
license=('MIT')
depends=('glibc' 'systemd')
makedepends=('go' 'make')
provides=('batmgt')
conflicts=('batmgt')
source=("https://github.com/tshakalekholoane/bat/archive/refs/tags/${pkgver}.tar.gz")
sha256sums=('241249b45659489d86cd128be38c7acde50544e3a8c08ab272468724b4e4091a')
build() {
cd "bat-${pkgver}"
# Set up standard Arch Linux Go build flags
export CGO_CPPFLAGS="${CPPFLAGS}"
export CGO_CFLAGS="${CFLAGS}"
export CGO_CXXFLAGS="${CXXFLAGS}"
export CGO_LDFLAGS="${LDFLAGS}"
export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external -mod=readonly -modcacherw"
# Build from source using Make and Go
make build
}
package() {
cd "bat-${pkgver}"
# Install the compiled binary from the bin/ directory to /usr/local/bin as batmgt
install -Dm755 bin/bat "$pkgdir/usr/local/bin/batmgt"
# Install the systemd service, adjusting path, binary name, and target unit
install -Dm644 bat.service "$pkgdir/usr/lib/systemd/system/batmgt.service"
sed -i 's/\/bin\/bat/\/local\/bin\/batmgt/g' "$pkgdir/usr/lib/systemd/system/batmgt.service"
sed -i 's/ bat / batmgt /g' "$pkgdir/usr/lib/systemd/system/batmgt.service"
sed -i 's/{{.Event}}.target/multi-user.target/g' "$pkgdir/usr/lib/systemd/system/batmgt.service"
# Install the man page under the new name
install -Dm644 bat.1 "$pkgdir/usr/share/man/man1/batmgt.1"
# Install the license file
install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
  1. Create a build directory and save the PKGBUILD above as PKGBUILD inside it.

    Terminal window
    mkdir -p ~/batmgt-build && micro ~/batmgt-build/PKGBUILD
  2. Build and install the package. The build downloads the source from GitHub and creates an installable .zst package.

    Terminal window
    cd ~/batmgt-build && makepkg -si
  3. Set the charge limit. Replace 80 with your target percentage.

    Terminal window
    sudo batmgt threshold 80
  4. Persist the limit across reboots.

    Terminal window
    sudo batmgt persist
  5. Check the value written to the battery. The value should match your limit.

    Terminal window
    cat /sys/class/power_supply/BAT0/charge_control_end_threshold

Source: How to persist a charge limit on your laptop: bat