Logo

dev-resources.site

for different kinds of informations.

Code. Battery notifier.

Published at
12/12/2024
Categories
bash
polybar
notifications
unixporn
Author
axkira
Author
6 person written this
axkira
open
Code. Battery notifier.

Out of frustration from some-battery-monitor-util sometimes missing the moment to notify me about low charge, this simple script was put together.

It checks the current status and level of the BAT0 device and does one of the following:

  • notify about low battery level (15%)
  • notify about critical battery level (5%)
  • notify about near-zero battery level (3%), wait a couple of seconds and hibernate if battery status haven't changed to Charging

Requisites:

  • some notification daemon (I like dunst)
  • systemd for hibernation
  • something, that calls that script periodically (I call it with polybar every 60 seconds)

The script itself (battery-notifier.sh):

#!/usr/bin/env bash

LOW_LEVEL=15
CRITICAL_LEVEL=5
HIBERNATE_LEVEL=3

LEVEL=$(cat /sys/class/power_supply/BAT0/capacity)
STATUS=$(cat /sys/class/power_supply/BAT0/status)

hibernate() {
  notify-send -a "Battery" "Hibernating!" -u critical

  # Give some time to plug the power in
  sleep 5

  # Hibernate if still discharging
  STATUS=$(cat /sys/class/power_supply/BAT0/status)
  if [[ "$STATUS" = "Discharging" ]]; then
    systemctl hibernate
  else
    notify-send -a "Battery" "Power was plugged in. Hibernation is cancelled!"
  fi
}

notify_critical() {
  notify-send -a "Battery" "Battery is critically low!" -u critical
}

notify_low() {
  notify-send -a "Battery" "Battery is low!"
}


if [[ "$STATUS" = "Discharging" ]]; then
  if [[ "$LEVEL" -le "$HIBERNATE_LEVEL" ]]; then
    hibernate
  elif [[ "$LEVEL" -le "$CRITICAL_LEVEL" ]]; then
    notify_critical
  elif [[ "$LEVEL" -le "$LOW_LEVEL" ]]; then
    notify_low
  fi
fi
Enter fullscreen mode Exit fullscreen mode

The polybar module config:

[module/battery_notifier]
type = custom/script
exec = ~/.config/polybar/battery-notifier.sh
interval = 60
Enter fullscreen mode Exit fullscreen mode

* Notice, that script is put to ~/.config/polybar/.

bash Article's
30 articles in total
Favicon
Understanding Linux Shells: Interactive, Non-Interactive, and RC Files
Favicon
Fixing Linux Backup Sync Issues for exFAT Compatibility
Favicon
Ergonomic Pyhon Text Piping Solution for Linux Shell with pypyp and uv
Favicon
Changing the Login Shell with Security and Interactivity
Favicon
Final Bash Script Series Mastering Remote Server Management and Web App Deployment
Favicon
Writting a GH extension with AI
Favicon
Test GPIO pins on BeagleBone Black by toggling high to low
Favicon
NYP Infosec December CTF 2024 writeup!
Favicon
Building a Smart Heater Controller with Python, Docker, and Bluetooth #3
Favicon
The Complete Guide to Bash Commands
Favicon
How to Get Started with Bash Scripting for Automation
Favicon
Building a Basic Testing Framework in Bash 🐚
Favicon
Funny how, as I was writing this, I started wondering—could we make this pipeline even smarter? That’s when SonarQube came to mind. But can it even work with Bash scripts? I’d love to hear your thoughts!
Favicon
Domina Bash con ejemplos prácticos de Git
Favicon
Explaining Null Device or Black Hole Register in Vim
Favicon
A Media Server on Steroids - Walkthrough
Favicon
From FZF file preview to a browser for cht.sh to discovering the ideal solution
Favicon
Code. Battery notifier.
Favicon
🌟Simplifying User Account Management with Bash Scripting Day3🌟
Favicon
Become a Bash Scripting Pro in 10 Minutes: A Quick Guide for Beginners
Favicon
Automatically Monitor and Manage RAM Usage for Next.js Development
Favicon
Bash Script - Task Management
Favicon
Bash Your Tools
Favicon
Bash Script Series: Automating Log Analysis with Bash Script or Shell Script
Favicon
The Most Interesting Mostly Tech Reads of 2024
Favicon
🚀 Automating Process Monitoring & Restarting with Bash Scripting Day4! 🔧
Favicon
🚀 RazzShell v1.0.1 is Here: Plugin Support, Enhanced Job Management, and More! 🌟
Favicon
Step-by-Step Guide: Assigning a Namecheap Domain to DigitalOcean Hosting with Nginx
Favicon
Simplify GitHub Workflow Management with My-GitHub-Manager
Favicon
How to play a bunch of lemmings

Featured ones: