From 85a49d2edd6ceda704a6cdc084e2459174fcda36 Mon Sep 17 00:00:00 2001 From: krolyxon Date: Fri, 16 Jan 2026 23:56:38 +0530 Subject: initial commit --- power-watch.sh | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 power-watch.sh (limited to 'power-watch.sh') 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 -- cgit v1.2.3