Files
owen 9170828b36 Show battery power draw from sensors in waybar
Replace the built-in battery module with a custom/battery script that
combines sysfs capacity/status with the power1 reading from `sensors`,
displaying it as "85% (5.4W)" next to the icon.
2026-08-22 13:43:19 +00:00

42 lines
964 B
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
bat_dir="/sys/class/power_supply/BAT0"
capacity=$(<"$bat_dir/capacity")
status=$(<"$bat_dir/status")
power=$(sensors -j 2>/dev/null | python3 -c '
import json, sys
data = json.load(sys.stdin)
for chip, fields in data.items():
if not chip.startswith("BAT"):
continue
power1 = fields.get("power1")
if power1:
print(round(next(iter(power1.values())), 1))
break
')
text="${capacity}%"
[[ -n "$power" ]] && text+=" (${power}W)"
icons=("󰁺" "󰁼" "󰁾" "󰂀" "󰁹")
if [[ "$status" == "Charging" ]]; then
icon="󰂄"
else
idx=$((capacity * ${#icons[@]} / 101))
icon="${icons[$idx]}"
fi
class="normal"
if [[ "$status" != "Charging" ]]; then
if ((capacity <= 15)); then
class="critical"
elif ((capacity <= 30)); then
class="warning"
fi
fi
printf '{"text": "%s %s", "class": "%s", "tooltip": "%s, %s"}\n' \
"$icon" "$text" "$class" "$status" "${power:+${power}W}"