Battery Percentage Report for Arch Linux

Certainly! Let’s integrate the notification for the critical battery level between 0% and 3% into the script:

Updated Battery Monitoring Script with Critical Battery Level Notification

#!/bin/bash

# Function to check battery status and send notifications
check_battery() {
    # Get battery information
    battery=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0)

    # Extract battery percentage (rounded to nearest integer)
    percentage=$(echo "$battery" | grep -E "percentage" | awk '{print int($2 + 0.5)}' | tr -d '%')

    # Extract battery state (charging, discharging, fully-charged)
    state=$(echo "$battery" | grep -E "state" | awk '{print $2}')

    # Check battery state and send appropriate notifications
    if [ "$state" == "discharging" ]; then
        if (( percentage >= 80 )); then
            notify-send -u low "Battery Level" "Battery level is at $percentage%"
        elif (( percentage >= 50 )); then
            notify-send -u normal "Battery Level" "Battery level is at $percentage%"
        elif (( percentage >= 20 )); then
            notify-send -u critical "Low Battery Warning" "Battery level is critically low ($percentage%)"
        elif (( percentage >= 10 )); then
            notify-send -u critical "Low Battery Warning" "Battery level is critically low ($percentage%)"
        elif (( percentage >= 3 )); then
            notify-send -u critical "Battery Level" "Battery level is at $percentage%"
        elif (( percentage >= 0 )); then
            notify-send -u critical "Critical Battery Level" "Battery level is critically low ($percentage%)"
        fi
    elif [ "$state" == "charging" ]; then
        if (( percentage >= 80 )); then
            notify-send -u critical "Battery Level" "Battery is charging and is at $percentage%"
        fi
    elif [ "$state" == "fully-charged" ]; then
        notify-send -u low "Battery Level" "Battery is fully charged ($percentage%)"
    fi
}

# Call the function to check battery status
check_battery

Explanation:

  • Additional Condition: Added a new condition for battery percentage between 0% and 3% to display a critical notification labeled “Critical Battery Level”.
  • Notification Message: Customized the message to indicate a critical battery level when the percentage falls within the specified range.
  • Integration with Existing Logic: This addition fits seamlessly into the existing battery monitoring script structure, ensuring comprehensive notification coverage based on battery status and percentage.

Running the Script

  1. Save the updated script to a file, e.g., battery_monitor.sh.

  2. Make the script executable:

    chmod +x battery_monitor.sh
    
  3. Execute the script:

    ./battery_monitor.sh
    

Notes:

  • Customization: Adjust notification messages and thresholds as needed to suit your preferences and specific requirements.
  • Integration: Ensure the script is integrated with your desktop environment’s notification system (mako in your case) to display notifications correctly.
  • Monitoring: Keep the script running in the background to continuously monitor battery status and provide timely notifications.

This approach enhances your battery monitoring script to include specific notifications for critical battery levels between 0% and 3%, ensuring you are alerted appropriately based on your battery’s charge state.