commit 85a49d2edd6ceda704a6cdc084e2459174fcda36 Author: krolyxon Date: Fri Jan 16 23:56:38 2026 +0530 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..e4f23ef --- /dev/null +++ b/README.md @@ -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` diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..bcf7704 --- /dev/null +++ b/install.sh @@ -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" + diff --git a/power-watch.sh b/power-watch.sh new file mode 100644 index 0000000..5ed8297 --- /dev/null +++ b/power-watch.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +BOT_TOKEN="" +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 diff --git a/systemd/power-watch.service b/systemd/power-watch.service new file mode 100644 index 0000000..ef503cd --- /dev/null +++ b/systemd/power-watch.service @@ -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 diff --git a/systemd/power-watch.timer b/systemd/power-watch.timer new file mode 100644 index 0000000..852e0ca --- /dev/null +++ b/systemd/power-watch.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Run Power Watch every minute + +[Timer] +OnBootSec=30 +OnUnitActiveSec=60 +AccuracySec=5s + +[Install] +WantedBy=timers.target diff --git a/uninstall.sh b/uninstall.sh new file mode 100644 index 0000000..8a37073 --- /dev/null +++ b/uninstall.sh @@ -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."