You are not logged in.

#1 2024-02-07 07:42:01

iskander9908
Member
Registered: 2022-10-30
Posts: 31

[SOLVED] Battery notification

I wrote bash script for battery notifications

/home/iskander/.local/bin/battery-notification.sh
#!/bin/bash

# Battery thresholds for notifications (change as needed)
LOW_THRESHOLD=15
FULL_THRESHOLD=100

# Get current battery percentage using /sys/class/power_supply

BATTERY_STATUS=$(cat /sys/class/power_supply/BAT1/status)
BATTERY_CAPACITY=$(cat /sys/class/power_supply/BAT1/capacity)

# Check if battery is fully charged
if [ "$BATTERY_STATUS" == "Full" ] || [ "$BATTERY_CAPACITY" -eq 100 ]; then
# Display a notification for full battery
    notify-send "Battery Full" -u critical -i "battery-full"
fi

# Check if battery percentage is below the low threshold
if [ "$BATTERY_CAPACITY" -le "$LOW_THRESHOLD" ]; then
# Display a low battery notification
    notify-send "Battery low" "Battery is at ${BATTERY_CAPACITY}%" -u critical -i "battery-caution"
fi

and created systemd timer and service:

/etc/systemd/system/battery-notification.service
[Unit]
Description=Battery Notification Service

[Service]
ExecStart=/home/iskander/.local/bin/battery-notification.sh

[Install]
WantedBy=battery-notification.timer
[Unit]
Description=Battery Notification Timer

[Timer]
OnActiveSec=2min
OnUnitActiveSec=2min

[Install]
WantedBy=timers.target

But after reloading daemon and starting timer notification doesn't appear

Last edited by iskander9908 (2024-02-14 06:46:52)

Offline

#2 2024-02-07 09:45:09

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,231

Re: [SOLVED] Battery notification

Your systemd system service does not have access to the user bus that your notification daemon is running on.

Since this is user specific and doesn't require root rights, setup a proper user service instead. https://wiki.archlinux.org/title/Systemd/User https://wiki.archlinux.org/title/System … user_units

Last edited by V1del (2024-02-07 09:45:45)

Offline

#3 2024-02-07 10:19:02

iskander9908
Member
Registered: 2022-10-30
Posts: 31

Re: [SOLVED] Battery notification

You saved me, thanks

Last edited by iskander9908 (2024-02-14 06:46:23)

Offline

Board footer

Powered by FluxBB