Krolyxon
Commit 5634afb

Branches

Refs →

power-watchpower-watch.sh

33 lines · 729 B · Bourne Again Shell (bash)

1#!/bin/bash
2
3BOT_TOKEN="<BOT_TOKEN>"
4CHAT_ID="<CHAT_ID>"
5
6STATE_FILE="/tmp/power_state"
7
8POWER=$(upower -i $(upower -e | grep BAT) | grep "state:" | awk '{print $2}')
9
10
11# First run
12if [ ! -f "$STATE_FILE" ]; then
13 echo "$POWER" > "$STATE_FILE"
14 exit 0
15fi
16
17LAST=$(cat "$STATE_FILE")
18
19# If power state changed
20if [ "$POWER" != "$LAST" ]; then
21 echo "$POWER" > "$STATE_FILE"
22
23 if [ "$POWER" = "discharging" ]; then
24 MSG="⚠️ Homelab running on BATTERY!
25Switching 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
33fi