Krolyxon
Commit 85a49d2

Branches

Refs →

power-watch85a49d2

initial commit

Author
krolyxon
Date
2026-01-16 23:56 +0530
Parents
None
Commit
85a49d2edd6ceda704a6cdc084e2459174fcda36

+102 −0

6 files changed

  1. A README.md +15 −0
  2. A install.sh +23 −0
  3. A power-watch.sh +33 −0
  4. A systemd/power-watch.service +7 −0
  5. A systemd/power-watch.timer +10 −0
  6. A uninstall.sh +14 −0
A README.md View
@@ -0,0 +1,15 @@
1+# Power Watch
2+
3+Telegram notifier for your laptop homelabs.
4+
5+Sends a Telegram message when your system switches between AC and battery, perfect for power sensitive deployments.
6+
7+## Install (Configure before installation)
8+```bash
9+git clone https://github.com/YOURNAME/power-watch.git
10+cd power-watch
11+./install.sh
12+```
13+
14+## Configuration
15+- Edit `./power-watch.sh`, and set `BOT_TOKEN` & `CHAT_ID`
A install.sh View
@@ -0,0 +1,23 @@
1+#!/bin/bash
2+set -e
3+
4+echo "Installing power-watch..."
5+
6+mkdir -p ~/.local/bin
7+mkdir -p ~/.config/systemd/user
8+
9+cp power-watch.sh ~/.local/bin/power-watch.sh
10+chmod +x ~/.local/bin/power-watch.sh
11+
12+cp systemd/power-watch.service ~/.config/systemd/user/
13+cp systemd/power-watch.timer ~/.config/systemd/user/
14+
15+systemctl --user daemon-reload
16+systemctl --user enable --now power-watch.timer
17+
18+echo "Enabling lingering..."
19+sudo loginctl enable-linger "$USER"
20+
21+echo "Done! Power Watch is running."
22+echo "Test with: systemctl --user start power-watch.service"
23+
A power-watch.sh View
@@ -0,0 +1,33 @@
1+#!/bin/bash
2+
3+BOT_TOKEN="<BOT_TOKEN>"
4+CHAT_ID="<CHAT_ID>"
5+
6+STATE_FILE="/tmp/power_state"
7+
8+POWER=$(upower -i $(upower -e | grep BAT) | grep "state:" | awk '{print $2}')
9+
10+
11+# First run
12+if [ ! -f "$STATE_FILE" ]; then
13+ echo "$POWER" > "$STATE_FILE"
14+ exit 0
15+fi
16+
17+LAST=$(cat "$STATE_FILE")
18+
19+# If power state changed
20+if [ "$POWER" != "$LAST" ]; then
21+ echo "$POWER" > "$STATE_FILE"
22+
23+ if [ "$POWER" = "discharging" ]; then
24+ MSG="⚠️ Homelab running on BATTERY!
25+Switching off heavy services recommended."
26+ else
27+ MSG="🔌 Power restored — running on AC"
28+ fi
29+
30+ curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
31+ -d chat_id="$CHAT_ID" \
32+ -d text="$MSG" > /dev/null
33+fi
A systemd/power-watch.service View
@@ -0,0 +1,7 @@
1+[Unit]
2+Description=Power Watch Telegram Notifier
3+
4+[Service]
5+Type=oneshot
6+ExecStart=%h/.local/bin/power-watch.sh
7+Environment=PATH=/usr/bin:/bin
A systemd/power-watch.timer View
@@ -0,0 +1,10 @@
1+[Unit]
2+Description=Run Power Watch every minute
3+
4+[Timer]
5+OnBootSec=30
6+OnUnitActiveSec=60
7+AccuracySec=5s
8+
9+[Install]
10+WantedBy=timers.target
A uninstall.sh View
@@ -0,0 +1,14 @@
1+#!/bin/bash
2+set -e
3+
4+echo "Removing power-watch..."
5+
6+systemctl --user disable --now power-watch.timer || true
7+
8+rm -f ~/.config/systemd/user/power-watch.service
9+rm -f ~/.config/systemd/user/power-watch.timer
10+rm -f ~/.local/bin/power-watch.sh
11+
12+systemctl --user daemon-reload
13+
14+echo "Removed."