initial commit

This commit is contained in:
krolyxon 2026-01-16 23:56:38 +05:30
commit 85a49d2edd
6 changed files with 102 additions and 0 deletions

15
README.md Normal file
View File

@ -0,0 +1,15 @@
# Power Watch
Telegram notifier for your laptop homelabs.
Sends a Telegram message when your system switches between AC and battery, perfect for power sensitive deployments.
## Install (Configure before installation)
```bash
git clone https://github.com/YOURNAME/power-watch.git
cd power-watch
./install.sh
```
## Configuration
- Edit `./power-watch.sh`, and set `BOT_TOKEN` & `CHAT_ID`

23
install.sh Normal file
View File

@ -0,0 +1,23 @@
#!/bin/bash
set -e
echo "Installing power-watch..."
mkdir -p ~/.local/bin
mkdir -p ~/.config/systemd/user
cp power-watch.sh ~/.local/bin/power-watch.sh
chmod +x ~/.local/bin/power-watch.sh
cp systemd/power-watch.service ~/.config/systemd/user/
cp systemd/power-watch.timer ~/.config/systemd/user/
systemctl --user daemon-reload
systemctl --user enable --now power-watch.timer
echo "Enabling lingering..."
sudo loginctl enable-linger "$USER"
echo "Done! Power Watch is running."
echo "Test with: systemctl --user start power-watch.service"

33
power-watch.sh Normal file
View File

@ -0,0 +1,33 @@
#!/bin/bash
BOT_TOKEN="<BOT_TOKEN>"
CHAT_ID="<CHAT_ID>"
STATE_FILE="/tmp/power_state"
POWER=$(upower -i $(upower -e | grep BAT) | grep "state:" | awk '{print $2}')
# First run
if [ ! -f "$STATE_FILE" ]; then
echo "$POWER" > "$STATE_FILE"
exit 0
fi
LAST=$(cat "$STATE_FILE")
# If power state changed
if [ "$POWER" != "$LAST" ]; then
echo "$POWER" > "$STATE_FILE"
if [ "$POWER" = "discharging" ]; then
MSG="⚠️ Homelab running on BATTERY!
Switching off heavy services recommended."
else
MSG="🔌 Power restored — running on AC"
fi
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
-d chat_id="$CHAT_ID" \
-d text="$MSG" > /dev/null
fi

View File

@ -0,0 +1,7 @@
[Unit]
Description=Power Watch Telegram Notifier
[Service]
Type=oneshot
ExecStart=%h/.local/bin/power-watch.sh
Environment=PATH=/usr/bin:/bin

10
systemd/power-watch.timer Normal file
View File

@ -0,0 +1,10 @@
[Unit]
Description=Run Power Watch every minute
[Timer]
OnBootSec=30
OnUnitActiveSec=60
AccuracySec=5s
[Install]
WantedBy=timers.target

14
uninstall.sh Normal file
View File

@ -0,0 +1,14 @@
#!/bin/bash
set -e
echo "Removing power-watch..."
systemctl --user disable --now power-watch.timer || true
rm -f ~/.config/systemd/user/power-watch.service
rm -f ~/.config/systemd/user/power-watch.timer
rm -f ~/.local/bin/power-watch.sh
systemctl --user daemon-reload
echo "Removed."