prasanthrangan / hyprdots

// Aesthetic, dynamic and minimal dots for Arch hyprland
GNU General Public License v3.0
7.45k stars 865 forks source link

"Unknown Power Supply status" #411

Closed opticblu closed 1 year ago

opticblu commented 1 year ago

"notify-send"

"Please raise an issue to the github repo"

I keep seeing this notification every few minutes, presumably because I use a desktop PC. Only since the latest update.

Is anyone else seeing this? Thanks

cupenk commented 1 year ago

yes please fix it.

opticblu commented 1 year ago

yes please fix it.

for now just cd to ~/.config/hypr/scripts and chmod -x batterynotify.sh

that should stop the notification spam until a proper fix

cupenk commented 1 year ago

yes please fix it.

for now just cd to ~/.config/hypr/scripts and chmod -x batterynotify.sh

that should stop the notification spam until a proper fix

ok already, how do I activate it again?

opticblu commented 1 year ago

yes please fix it.

for now just cd to ~/.config/hypr/scripts and chmod -x batterynotify.sh that should stop the notification spam until a proper fix

ok already, how do I activate it again?

chmod +x batterynotify.sh

kRHYME7 commented 1 year ago

Sorry, This is my PR, I may missed something,

Can you please try to run ~/.config/hypr/scripts/batterynotify.sh again on the terminal?

If you're using a PC the script should exit If you're using a laptop the notification should kick in.

But maybe there are some errors so your input would be very helpful.

kRHYME7 commented 1 year ago

Hello @cupenk are you using a laptop?

cupenk commented 1 year ago

Hello @cupenk are you using a laptop?

Yes I'm using laptop.

kRHYME7 commented 1 year ago

@opticblu Hi! By any chance is your desktop having a UPS or a UPS enabled? Can we have the output for your

ls /sys/class/power_supply/*/type  &&
  cat /sys/class/power_supply/*/type | grep Battery
kRHYME7 commented 1 year ago

"notify-send"

"Please raise an issue to the github repo"

@cupenk By any chance do you get this issue? while charging? or not charging?

also can we have the output for cat /sys/class/power_supply/BAT1/status

cupenk commented 1 year ago

"notify-send" "Please raise an issue to the github repo"

@cupenk By any chance do you get this issue? while charging? or not charging?

also can we have the output for cat /sys/class/power_supply/BAT1/status

while charging, and I do this chmod +x batterynotify.sh, then there are no notifications anymore, how do I activate it again?

kRHYME7 commented 1 year ago

Can we have the output for cat /sys/class/power_supply/BAT*/status ?

you can also try to merge My PR #414
or just edit your config manually by modifying your ~/.config/hypr/scripts/batterynotify.sh and change what is inside with this and run it on the terminal, You should get some error message.

Please try to plug and unplug your charger to see changes.

Your help will be much appreciated.


#!/bin/bash 

# User Variables
battery_critical_threshold=10     #? Set  Battery  Critical Limit to suspend
battery_low_threshold=20    #? Set Battery Low Limit
unplug_charger_threshold=80   #? Set Max Battery Status Warning
countdown=300      #? Countdown timer ; if set to less than 60 defaults to 60 seconds
action="suspend" #? will be appended to systemctl $action

is_laptop() { # Check if the system is a laptop
    if grep -q "Battery" /sys/class/power_supply/BAT*/type; then
        return 0  # It's a laptop
    else
        exit 0  # It's not a laptop
    fi
}
send_notification() { # Send notification
    notify-send -t 5000 $1 -u $2 "$3" "$4" # Call the notify-send command with the provided arguments \$1 is the flags \$2 is the urgency \$3 is the title \$4 is the message
}
check_battery() {  # Check battery status
    echo $(cat "$1/status") $(< "$1/capacity")    # Read and echo the battery status and capacity
}
handle_action () {
count=$(( $countdown > 60 ? $countdown : 60 )) # reset count
nohup systemctl $action
}

# Handle the power supply status
handle_power_supply() {
for battery in /sys/class/power_supply/BAT*; do
        read -r battery_status battery_percentage <<< $(check_battery $battery)
case $battery_status in         # Handle the power supply status
                "Discharging")
                    if [[ "$prev_status" == "Charging" ]]; then
                        prev_status=$battery_status
                        urgency=$([[ $battery_percentage -le "$battery_low_threshold" ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification  "-r 10" "$urgency" "Charger Plug OUT" "Battery is at $battery_percentage%."
                    fi
                    # Check if battery is below critical threshold
                    if [[ "$battery_percentage" -le "$battery_critical_threshold" ]]; then
                        count=$(( $countdown > 60 ? $countdown : 60 ))
                        while [ $count -gt 0 ] && [[ "$(check_battery $battery)" != "Charging"* ]]; do
                            send_notification "-r 10" "CRITICAL" "Battery Critically Low" "$battery_percentage% is critically low. Device will $action in $((count/60)):$((count%60)) ."
                            count=$((count-1))
                            sleep 1
                        done
                        if [ $count -eq 0 ]; then
                             handle_action
                        fi
                    elif [[ "$battery_percentage" -le "$battery_low_threshold" ]] && (( (last_notified_percentage - battery_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Low" "Battery is at $battery_percentage%. Connect the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
                "Charging")                     
                    if [[ "$prev_status" == "Discharging" ]]; then
                        prev_status=$battery_status
                        count=$(( $countdown > 60 ? $countdown : 60 )) # reset count
                        urgency=$([[ "$battery_percentage" -ge $unplug_charger_threshold ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification "-r 10" "$urgency" "Charger Plug In" "Battery is at $battery_percentage%."
                    fi
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
                "Full")
                    send_notification "-r 10" "CRITICAL" "Battery Full" "Please unplug your Charger"
                    ;;
                "Not Charging")
                    send_notification "-r 10" "CRITICAL" "Device Not Charging!" "Please Check your Charger or Device Temperature"
                    ;;                                       
                    *)
                   if [[ ! -f "/tmp/hyprdots.batterynotify.fallback.status.$battery_status-$$" ]]; then
                    echo "Status: '==>> "$battery_status" <<==' Script on Fallback mode,Unknown power supply status.Please copy this line and raise an issue to the Github Repo.Also run 'ls /tmp/hyprdots.batterynotify' to see the list of lock files.*"
                    touch "/tmp/hyprdots.batterynotify.fallback.status.$battery_status-$$"
                    fi               
                    #send_notification "-r 10" "CRITICAL" "Unknown power supply status." "Please raise an issue to the Github Repo(You will only see this once after boot)"
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    elif [[ "$battery_percentage" -le "$battery_critical_threshold" ]]; then
                        count=$(( $countdown > 60 ? $countdown : 60 ))
                        while [ $count -gt 0 ] && [[ "$(check_battery $battery)" != "Charging"* ]]; do
                            send_notification "-r 10" "CRITICAL" "Battery Critically Low" "$battery_percentage% is critically low. Device will $action in $((count/60)):$((count%60)) ."
                            count=$((count-1))
                            sleep 1
                        done
                        [ $count -eq 0 ] && handle_action
                    elif [[ "$battery_percentage" -le "$battery_low_threshold" ]] && (( (last_notified_percentage - battery_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Low" "Battery is at $battery_percentage%. Connect the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
            esac
        done
}
main() { # Main function
    if is_laptop; then
        handle_power_supply # initiate the function
        last_notified_percentage=$battery_percentage
        prev_status=$battery_status
        dbus-monitor --system "type='signal',interface='org.freedesktop.DBus.Properties',path='$(upower -e | grep battery)'" 2> /dev/null | while read -r battery_status_change; do handle_power_supply ; done
    fi
}
main
prasanthrangan commented 1 year ago

Hey, sorry i cant test this... please let me know if its fixed in 4c416e9.

cupenk commented 1 year ago

Can we have the output for cat /sys/class/power_supply/BAT*/status ?

you can also try to merge My PR #414 or just edit your config manually by modifying your ~/.config/hypr/scripts/batterynotify.sh and change what is inside with this and run it on the terminal, You should get some error message.

Please try to plug and unplug your charger to see changes.

Your help will be much appreciated.


#!/bin/bash 

# User Variables
battery_critical_threshold=10     #? Set  Battery  Critical Limit to suspend
battery_low_threshold=20    #? Set Battery Low Limit
unplug_charger_threshold=80   #? Set Max Battery Status Warning
countdown=300      #? Countdown timer ; if set to less than 60 defaults to 60 seconds
action="suspend" #? will be appended to systemctl $action

is_laptop() { # Check if the system is a laptop
    if grep -q "Battery" /sys/class/power_supply/BAT*/type; then
        return 0  # It's a laptop
    else
        exit 0  # It's not a laptop
    fi
}
send_notification() { # Send notification
    notify-send -t 5000 $1 -u $2 "$3" "$4" # Call the notify-send command with the provided arguments \$1 is the flags \$2 is the urgency \$3 is the title \$4 is the message
}
check_battery() {  # Check battery status
    echo $(cat "$1/status") $(< "$1/capacity")    # Read and echo the battery status and capacity
}
handle_action () {
count=$(( $countdown > 60 ? $countdown : 60 )) # reset count
nohup systemctl $action
}

# Handle the power supply status
handle_power_supply() {
for battery in /sys/class/power_supply/BAT*; do
        read -r battery_status battery_percentage <<< $(check_battery $battery)
case $battery_status in         # Handle the power supply status
                "Discharging")
                    if [[ "$prev_status" == "Charging" ]]; then
                        prev_status=$battery_status
                        urgency=$([[ $battery_percentage -le "$battery_low_threshold" ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification  "-r 10" "$urgency" "Charger Plug OUT" "Battery is at $battery_percentage%."
                    fi
                    # Check if battery is below critical threshold
                    if [[ "$battery_percentage" -le "$battery_critical_threshold" ]]; then
                        count=$(( $countdown > 60 ? $countdown : 60 ))
                        while [ $count -gt 0 ] && [[ "$(check_battery $battery)" != "Charging"* ]]; do
                            send_notification "-r 10" "CRITICAL" "Battery Critically Low" "$battery_percentage% is critically low. Device will $action in $((count/60)):$((count%60)) ."
                            count=$((count-1))
                            sleep 1
                        done
                        if [ $count -eq 0 ]; then
                             handle_action
                        fi
                    elif [[ "$battery_percentage" -le "$battery_low_threshold" ]] && (( (last_notified_percentage - battery_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Low" "Battery is at $battery_percentage%. Connect the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
                "Charging")                     
                    if [[ "$prev_status" == "Discharging" ]]; then
                        prev_status=$battery_status
                        count=$(( $countdown > 60 ? $countdown : 60 )) # reset count
                        urgency=$([[ "$battery_percentage" -ge $unplug_charger_threshold ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification "-r 10" "$urgency" "Charger Plug In" "Battery is at $battery_percentage%."
                    fi
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
                "Full")
                    send_notification "-r 10" "CRITICAL" "Battery Full" "Please unplug your Charger"
                    ;;
                "Not Charging")
                    send_notification "-r 10" "CRITICAL" "Device Not Charging!" "Please Check your Charger or Device Temperature"
                    ;;                                       
                    *)
                   if [[ ! -f "/tmp/hyprdots.batterynotify.fallback.status.$battery_status-$$" ]]; then
                    echo "Status: '==>> "$battery_status" <<==' Script on Fallback mode,Unknown power supply status.Please copy this line and raise an issue to the Github Repo.Also run 'ls /tmp/hyprdots.batterynotify' to see the list of lock files.*"
                    touch "/tmp/hyprdots.batterynotify.fallback.status.$battery_status-$$"
                    fi               
                    #send_notification "-r 10" "CRITICAL" "Unknown power supply status." "Please raise an issue to the Github Repo(You will only see this once after boot)"
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    elif [[ "$battery_percentage" -le "$battery_critical_threshold" ]]; then
                        count=$(( $countdown > 60 ? $countdown : 60 ))
                        while [ $count -gt 0 ] && [[ "$(check_battery $battery)" != "Charging"* ]]; do
                            send_notification "-r 10" "CRITICAL" "Battery Critically Low" "$battery_percentage% is critically low. Device will $action in $((count/60)):$((count%60)) ."
                            count=$((count-1))
                            sleep 1
                        done
                        [ $count -eq 0 ] && handle_action
                    elif [[ "$battery_percentage" -le "$battery_low_threshold" ]] && (( (last_notified_percentage - battery_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Low" "Battery is at $battery_percentage%. Connect the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
            esac
        done
}
main() { # Main function
    if is_laptop; then
        handle_power_supply # initiate the function
        last_notified_percentage=$battery_percentage
        prev_status=$battery_status
        dbus-monitor --system "type='signal',interface='org.freedesktop.DBus.Properties',path='$(upower -e | grep battery)'" 2> /dev/null | while read -r battery_status_change; do handle_power_supply ; done
    fi
}
main

image

image I didn't get a charging notification.

kRHYME7 commented 1 year ago

Thanks @cupenk !
My bad! I really forgot to tell you that run ~/.config/hypr/scripts/batterynotify.sh on the terminal to see what error messages we get. Also this changes were merge on the latest commit so you may update your configs first then reboot.

image

But as I can see your battery notifies as if it is not charging so maybe we can make changes so that you can still get notifications.

cupenk commented 1 year ago

Thanks @cupenk ! My bad! I really forgot to tell you that run ~/.config/hypr/scripts/batterynotify.sh on the terminal to see what error messages we get. Also this changes were merge on the latest commit so you may update your configs first then reboot.

image

But as I can see your battery notifies as if it is not charging so maybe we can make changes so that you can still get notifications.

image

command not found

kRHYME7 commented 1 year ago

hi I remembered you disabled the permission to execute the script

Please run

 chmod +x  ~/.config/hypr/scripts/batterynotify.sh 
 ~/.config/hypr/scripts/batterynotify.sh 

Also, let us prevent the habit of running some unknown scripts using sudo unless told so and reviewed.

kRHYME7 commented 1 year ago

@cupenk Also this is the latest one can you please try this? This is to cater some device that reports Not Charging if plug in

#!/bin/bash

# User Variables
battery_critical_threshold=10     #? Set  Battery  Critical Limit to suspend
battery_low_threshold=20    #? Set Battery Low Limit
unplug_charger_threshold=80   #? Set Max Battery Status Warning
countdown=300      #? Countdown timer ; if set to less than 60 defaults to 60 seconds
action="suspend" #? will be appended to systemctl $action

is_laptop() { # Check if the system is a laptop
    if grep -q "Battery" /sys/class/power_supply/BAT*/type; then
        return 0  # It's a laptop
    else
        exit 0  # It's not a laptop
    fi
}
send_notification() { # Send notification
    notify-send -t 5000 $1 -u $2 "$3" "$4" # Call the notify-send command with the provided arguments \$1 is the flags \$2 is the urgency \$3 is the title \$4 is the message
}
check_battery() {  # Check battery status
    echo $(cat "$1/status") $(< "$1/capacity")    # Read and echo the battery status and capacity
}
handle_action () {
count=$(( countdown > 60 ? countdown : 60 )) # reset count
nohup systemctl $action
}

# Handle the power supply status
handle_power_supply() {
for battery in /sys/class/power_supply/BAT*; do
        read -r battery_status battery_percentage <<< "$(check_battery "$battery")"
case $battery_status in         # Handle the power supply status
                "Discharging")
                    if [[ "$prev_status" == "Charging" ]]; then
                        prev_status=$battery_status
                        urgency=$([[ $battery_percentage -le "$battery_low_threshold" ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification  "-r 10" "$urgency" "Charger Plug OUT" "Battery is at $battery_percentage%."
                    fi
                    # Check if battery is below critical threshold
                    if [[ "$battery_percentage" -le "$battery_critical_threshold" ]]; then
                        count=$(( countdown > 60 ? countdown : 60 )) # reset count
                        while [ $count -gt 0 ] && [[ "$(check_battery "$battery")" != "Charging"* ]]; do
                            send_notification "-r 10" "CRITICAL" "Battery Critically Low" "$battery_percentage% is critically low. Device will $action in $((count/60)):$((count%60)) ."
                            count=$((count-1))
                            sleep 1
                        done
                        if [ $count -eq 0 ]; then
                             handle_action
                        fi
                    elif [[ "$battery_percentage" -le "$battery_low_threshold" ]] && (( (last_notified_percentage - battery_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Low" "Battery is at $battery_percentage%. Connect the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
                "Charging")                     
                    if [[ "$prev_status" == "Discharging" ]]; then
                        prev_status=$battery_status
                        count=$(( countdown > 60 ? countdown : 60 )) # reset count
                        urgency=$([[ "$battery_percentage" -ge $unplug_charger_threshold ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification "-r 10" "$urgency" "Charger Plug In" "Battery is at $battery_percentage%."
                    fi
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
                "Full")
                    send_notification "-r 10" "CRITICAL" "Battery Full" "Please unplug your Charger"                    
                    ;;
                "Not Charging")
                    if [[ ! -f "/tmp/hyprdots.batterynotify.status.$battery_status-$$" ]]; then
                    touch "/tmp/hyprdots.batterynotify.status.$battery_status-$$"                    
                    echo "Status: '==>> "$battery_status" <<==' Device Reports Not Charging!,This may be device Specific errors.Please copy this line and raise an issue to the Github Repo.Also run 'ls /tmp/hyprdots.batterynotify' to see the list of lock files.*"
                    send_notification "-r 9876" "CRITICAL" "Device Reports Not Charging!" "Please Check your Charger or Device Temperature"
                    else
                    if  [[ "$prev_status" == "Discharging" ]]; then
                        prev_status=$battery_status
                        count=$(( countdown > 60 ? countdown : 60 )) # reset count
                        urgency=$([[ "$battery_percentage" -ge $unplug_charger_threshold ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification "-r 10" "$urgency" "Charger Plug In" "Battery is at $battery_percentage%."
                    fi   
                    fi                                    
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    fi                    
                    ;;                                       
                    *)
                   if [[ ! -f "/tmp/hyprdots.batterynotify.status.fallback.$battery_status-$$" ]]; then
                    echo "Status: '==>> "$battery_status" <<==' Script on Fallback mode,Unknown power supply status.Please copy this line and raise an issue to the Github Repo.Also run 'ls /tmp/hyprdots.batterynotify' to see the list of lock files.*"
                    touch "/tmp/hyprdots.batterynotify.status.fallback.$battery_status-$$"
                    fi               
                    #send_notification "-r 10" "CRITICAL" "Unknown power supply status." "Please raise an issue to the Github Repo(You will only see this once after boot)"
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    elif [[ "$battery_percentage" -le "$battery_critical_threshold" ]]; then
                        count=$(( $countdown > 60 ? $countdown : 60 ))
                        while [ $count -gt 0 ] && [[ "$(check_battery $battery)" != "Charging"* ]]; do
                            send_notification "-r 10" "CRITICAL" "Battery Critically Low" "$battery_percentage% is critically low. Device will $action in $((count/60)):$((count%60)) ."
                            count=$((count-1))
                            sleep 1
                        done
                        [ $count -eq 0 ] && handle_action
                    elif [[ "$battery_percentage" -le "$battery_low_threshold" ]] && (( (last_notified_percentage - battery_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Low" "Battery is at $battery_percentage%. Connect the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
            esac
        done
}
main() { # Main function
    if is_laptop; then
        handle_power_supply # initiate the function
        last_notified_percentage=$battery_percentage
        prev_status=$battery_status
        dbus-monitor --system "type='signal',interface='org.freedesktop.DBus.Properties',path='$(upower -e | grep battery)'" 2> /dev/null | while read -r battery_status_change; do handle_power_supply ; done
    fi
}
main
cupenk commented 1 year ago

@cupenk Also this is the latest one can you please try this? This is to cater some device that reports Not Charging if plug in

#!/bin/bash

# User Variables
battery_critical_threshold=10     #? Set  Battery  Critical Limit to suspend
battery_low_threshold=20    #? Set Battery Low Limit
unplug_charger_threshold=80   #? Set Max Battery Status Warning
countdown=300      #? Countdown timer ; if set to less than 60 defaults to 60 seconds
action="suspend" #? will be appended to systemctl $action

is_laptop() { # Check if the system is a laptop
    if grep -q "Battery" /sys/class/power_supply/BAT*/type; then
        return 0  # It's a laptop
    else
        exit 0  # It's not a laptop
    fi
}
send_notification() { # Send notification
    notify-send -t 5000 $1 -u $2 "$3" "$4" # Call the notify-send command with the provided arguments \$1 is the flags \$2 is the urgency \$3 is the title \$4 is the message
}
check_battery() {  # Check battery status
    echo $(cat "$1/status") $(< "$1/capacity")    # Read and echo the battery status and capacity
}
handle_action () {
count=$(( countdown > 60 ? countdown : 60 )) # reset count
nohup systemctl $action
}

# Handle the power supply status
handle_power_supply() {
for battery in /sys/class/power_supply/BAT*; do
        read -r battery_status battery_percentage <<< "$(check_battery "$battery")"
case $battery_status in         # Handle the power supply status
                "Discharging")
                    if [[ "$prev_status" == "Charging" ]]; then
                        prev_status=$battery_status
                        urgency=$([[ $battery_percentage -le "$battery_low_threshold" ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification  "-r 10" "$urgency" "Charger Plug OUT" "Battery is at $battery_percentage%."
                    fi
                    # Check if battery is below critical threshold
                    if [[ "$battery_percentage" -le "$battery_critical_threshold" ]]; then
                        count=$(( countdown > 60 ? countdown : 60 )) # reset count
                        while [ $count -gt 0 ] && [[ "$(check_battery "$battery")" != "Charging"* ]]; do
                            send_notification "-r 10" "CRITICAL" "Battery Critically Low" "$battery_percentage% is critically low. Device will $action in $((count/60)):$((count%60)) ."
                            count=$((count-1))
                            sleep 1
                        done
                        if [ $count -eq 0 ]; then
                             handle_action
                        fi
                    elif [[ "$battery_percentage" -le "$battery_low_threshold" ]] && (( (last_notified_percentage - battery_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Low" "Battery is at $battery_percentage%. Connect the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
                "Charging")                     
                    if [[ "$prev_status" == "Discharging" ]]; then
                        prev_status=$battery_status
                        count=$(( countdown > 60 ? countdown : 60 )) # reset count
                        urgency=$([[ "$battery_percentage" -ge $unplug_charger_threshold ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification "-r 10" "$urgency" "Charger Plug In" "Battery is at $battery_percentage%."
                    fi
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
                "Full")
                    send_notification "-r 10" "CRITICAL" "Battery Full" "Please unplug your Charger"                    
                    ;;
                "Not Charging")
                    if [[ ! -f "/tmp/hyprdots.batterynotify.status.$battery_status-$$" ]]; then
                    touch "/tmp/hyprdots.batterynotify.status.$battery_status-$$"                    
                    echo "Status: '==>> "$battery_status" <<==' Device Reports Not Charging!,This may be device Specific errors.Please copy this line and raise an issue to the Github Repo.Also run 'ls /tmp/hyprdots.batterynotify' to see the list of lock files.*"
                    send_notification "-r 9876" "CRITICAL" "Device Reports Not Charging!" "Please Check your Charger or Device Temperature"
                    else
                    if  [[ "$prev_status" == "Discharging" ]]; then
                        prev_status=$battery_status
                        count=$(( countdown > 60 ? countdown : 60 )) # reset count
                        urgency=$([[ "$battery_percentage" -ge $unplug_charger_threshold ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification "-r 10" "$urgency" "Charger Plug In" "Battery is at $battery_percentage%."
                    fi   
                    fi                                    
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    fi                    
                    ;;                                       
                    *)
                   if [[ ! -f "/tmp/hyprdots.batterynotify.status.fallback.$battery_status-$$" ]]; then
                    echo "Status: '==>> "$battery_status" <<==' Script on Fallback mode,Unknown power supply status.Please copy this line and raise an issue to the Github Repo.Also run 'ls /tmp/hyprdots.batterynotify' to see the list of lock files.*"
                    touch "/tmp/hyprdots.batterynotify.status.fallback.$battery_status-$$"
                    fi               
                    #send_notification "-r 10" "CRITICAL" "Unknown power supply status." "Please raise an issue to the Github Repo(You will only see this once after boot)"
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    elif [[ "$battery_percentage" -le "$battery_critical_threshold" ]]; then
                        count=$(( $countdown > 60 ? $countdown : 60 ))
                        while [ $count -gt 0 ] && [[ "$(check_battery $battery)" != "Charging"* ]]; do
                            send_notification "-r 10" "CRITICAL" "Battery Critically Low" "$battery_percentage% is critically low. Device will $action in $((count/60)):$((count%60)) ."
                            count=$((count-1))
                            sleep 1
                        done
                        [ $count -eq 0 ] && handle_action
                    elif [[ "$battery_percentage" -le "$battery_low_threshold" ]] && (( (last_notified_percentage - battery_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Low" "Battery is at $battery_percentage%. Connect the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
            esac
        done
}
main() { # Main function
    if is_laptop; then
        handle_power_supply # initiate the function
        last_notified_percentage=$battery_percentage
        prev_status=$battery_status
        dbus-monitor --system "type='signal',interface='org.freedesktop.DBus.Properties',path='$(upower -e | grep battery)'" 2> /dev/null | while read -r battery_status_change; do handle_power_supply ; done
    fi
}
main

image I've changed it, there's a syntax error.

cupenk commented 1 year ago

@cupenk Also this is the latest one can you please try this? This is to cater some device that reports Not Charging if plug in

#!/bin/bash

# User Variables
battery_critical_threshold=10     #? Set  Battery  Critical Limit to suspend
battery_low_threshold=20    #? Set Battery Low Limit
unplug_charger_threshold=80   #? Set Max Battery Status Warning
countdown=300      #? Countdown timer ; if set to less than 60 defaults to 60 seconds
action="suspend" #? will be appended to systemctl $action

is_laptop() { # Check if the system is a laptop
    if grep -q "Battery" /sys/class/power_supply/BAT*/type; then
        return 0  # It's a laptop
    else
        exit 0  # It's not a laptop
    fi
}
send_notification() { # Send notification
    notify-send -t 5000 $1 -u $2 "$3" "$4" # Call the notify-send command with the provided arguments \$1 is the flags \$2 is the urgency \$3 is the title \$4 is the message
}
check_battery() {  # Check battery status
    echo $(cat "$1/status") $(< "$1/capacity")    # Read and echo the battery status and capacity
}
handle_action () {
count=$(( countdown > 60 ? countdown : 60 )) # reset count
nohup systemctl $action
}

# Handle the power supply status
handle_power_supply() {
for battery in /sys/class/power_supply/BAT*; do
        read -r battery_status battery_percentage <<< "$(check_battery "$battery")"
case $battery_status in         # Handle the power supply status
                "Discharging")
                    if [[ "$prev_status" == "Charging" ]]; then
                        prev_status=$battery_status
                        urgency=$([[ $battery_percentage -le "$battery_low_threshold" ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification  "-r 10" "$urgency" "Charger Plug OUT" "Battery is at $battery_percentage%."
                    fi
                    # Check if battery is below critical threshold
                    if [[ "$battery_percentage" -le "$battery_critical_threshold" ]]; then
                        count=$(( countdown > 60 ? countdown : 60 )) # reset count
                        while [ $count -gt 0 ] && [[ "$(check_battery "$battery")" != "Charging"* ]]; do
                            send_notification "-r 10" "CRITICAL" "Battery Critically Low" "$battery_percentage% is critically low. Device will $action in $((count/60)):$((count%60)) ."
                            count=$((count-1))
                            sleep 1
                        done
                        if [ $count -eq 0 ]; then
                             handle_action
                        fi
                    elif [[ "$battery_percentage" -le "$battery_low_threshold" ]] && (( (last_notified_percentage - battery_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Low" "Battery is at $battery_percentage%. Connect the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
                "Charging")                     
                    if [[ "$prev_status" == "Discharging" ]]; then
                        prev_status=$battery_status
                        count=$(( countdown > 60 ? countdown : 60 )) # reset count
                        urgency=$([[ "$battery_percentage" -ge $unplug_charger_threshold ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification "-r 10" "$urgency" "Charger Plug In" "Battery is at $battery_percentage%."
                    fi
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
                "Full")
                    send_notification "-r 10" "CRITICAL" "Battery Full" "Please unplug your Charger"                    
                    ;;
                "Not Charging")
                    if [[ ! -f "/tmp/hyprdots.batterynotify.status.$battery_status-$$" ]]; then
                    touch "/tmp/hyprdots.batterynotify.status.$battery_status-$$"                    
                    echo "Status: '==>> "$battery_status" <<==' Device Reports Not Charging!,This may be device Specific errors.Please copy this line and raise an issue to the Github Repo.Also run 'ls /tmp/hyprdots.batterynotify' to see the list of lock files.*"
                    send_notification "-r 9876" "CRITICAL" "Device Reports Not Charging!" "Please Check your Charger or Device Temperature"
                    else
                    if  [[ "$prev_status" == "Discharging" ]]; then
                        prev_status=$battery_status
                        count=$(( countdown > 60 ? countdown : 60 )) # reset count
                        urgency=$([[ "$battery_percentage" -ge $unplug_charger_threshold ]] && echo "CRITICAL" || echo "NORMAL")
                        send_notification "-r 10" "$urgency" "Charger Plug In" "Battery is at $battery_percentage%."
                    fi   
                    fi                                    
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    fi                    
                    ;;                                       
                    *)
                   if [[ ! -f "/tmp/hyprdots.batterynotify.status.fallback.$battery_status-$$" ]]; then
                    echo "Status: '==>> "$battery_status" <<==' Script on Fallback mode,Unknown power supply status.Please copy this line and raise an issue to the Github Repo.Also run 'ls /tmp/hyprdots.batterynotify' to see the list of lock files.*"
                    touch "/tmp/hyprdots.batterynotify.status.fallback.$battery_status-$$"
                    fi               
                    #send_notification "-r 10" "CRITICAL" "Unknown power supply status." "Please raise an issue to the Github Repo(You will only see this once after boot)"
                    if [[ "$battery_percentage" -ge $unplug_charger_threshold ]] && (( (battery_percentage - last_notified_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Charged" "Battery is at $battery_percentage%. You can unplug the charger."
                        last_notified_percentage=$battery_percentage
                    elif [[ "$battery_percentage" -le "$battery_critical_threshold" ]]; then
                        count=$(( $countdown > 60 ? $countdown : 60 ))
                        while [ $count -gt 0 ] && [[ "$(check_battery $battery)" != "Charging"* ]]; do
                            send_notification "-r 10" "CRITICAL" "Battery Critically Low" "$battery_percentage% is critically low. Device will $action in $((count/60)):$((count%60)) ."
                            count=$((count-1))
                            sleep 1
                        done
                        [ $count -eq 0 ] && handle_action
                    elif [[ "$battery_percentage" -le "$battery_low_threshold" ]] && (( (last_notified_percentage - battery_percentage) >= 1 )); then
                        send_notification "-r 10" "CRITICAL" "Battery Low" "Battery is at $battery_percentage%. Connect the charger."
                        last_notified_percentage=$battery_percentage
                    fi
                    ;;
            esac
        done
}
main() { # Main function
    if is_laptop; then
        handle_power_supply # initiate the function
        last_notified_percentage=$battery_percentage
        prev_status=$battery_status
        dbus-monitor --system "type='signal',interface='org.freedesktop.DBus.Properties',path='$(upower -e | grep battery)'" 2> /dev/null | while read -r battery_status_change; do handle_power_supply ; done
    fi
}
main

image

kRHYME7 commented 1 year ago

Nice ! I can Replicate this error now. I will issue a new Pr soon. But the latest Commit Should give us a Notification for battery Capacity levels. This issue is not the same for above. Thank you for Helping !!!!

cupenk commented 1 year ago

Nice ! I can Replicate this error now. I will issue a new Pr soon. But the latest Commit Should give us a Notification for battery Capacity levels. This issue is not the same for above. Thank you for Helping !!!!

very good thanks.

opticblu commented 1 year ago

@opticblu Hi! By any chance is your desktop having a UPS or a UPS enabled? Can we have the output for your

ls /sys/class/power_supply/*/type  &&
 cat /sys/class/power_supply/*/type | grep Battery

Good inference, nice catch

Matter of fact, this UPS is supposed to be connected to my mini-PC and not this PC (just moved, hasty desk setup whoops) so thanks for noticing this

Here's this

ls /sys/class/power_supply//type && cat /sys/class/power_supply//type | grep Battery

.r--r--r-- 4.1k root 1 Nov 18:59  /sys/class/power_supply/hidpp_battery_0/type .r--r--r-- 4.1k root 1 Nov 18:59  /sys/class/power_supply/hidpp_battery_1/type .r--r--r-- 4.1k root 1 Nov 18:59  /sys/class/power_supply/sony_controller_battery_ac:7a:4d:16:b2:b2/type Battery Battery Battery

Thanks for the prompt attention and reply

kRHYME7 commented 1 year ago

Thanks! The latest commit should Fix this. I limit this for now, exclusively for BAT ``` /sys/class/power_supply/BAT/type. ``` But maybe A UPS support soon if requested by someone that is willing to test.

@opticblu We can Close this now if the latest commit fixes the issue. Have a great day!

opticblu commented 1 year ago

Thanks! The latest commit should Fix this. I limit this for now, exclusively for BAT `/sys/class/power_supply/BAT/type.` But maybe A UPS support soon if requested by someone that is willing to test.

@opticblu We can Close this now if the latest commit fixes the issue. Have a great day!

Yep, works well

Thank you