aboutsummaryrefslogtreecommitdiff
path: root/power-watch.sh
diff options
context:
space:
mode:
authorkrolyxon <krolyxon@tutanota.com>2026-01-16 23:56:38 +0530
committerkrolyxon <krolyxon@tutanota.com>2026-01-16 23:56:38 +0530
commit85a49d2edd6ceda704a6cdc084e2459174fcda36 (patch)
treee353142aa4b5fb2dd55964a52a780e5de456e9ee /power-watch.sh
initial commit
Diffstat (limited to 'power-watch.sh')
-rw-r--r--power-watch.sh33
1 files changed, 33 insertions, 0 deletions
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="<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