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.
This commit is contained in:
2026-08-22 13:43:19 +00:00
parent 3d3717d0fb
commit 9170828b36
3 changed files with 46 additions and 5 deletions
+2 -2
View File
@@ -5,7 +5,7 @@
"spacing": 6, "spacing": 6,
"modules-left": ["sway/workspaces", "sway/mode"], "modules-left": ["sway/workspaces", "sway/mode"],
"modules-center": ["clock"], "modules-center": ["clock"],
"modules-right": ["cpu", "memory", "custom/network", "pulseaudio", "backlight", "battery", "tray", "custom/power"], "modules-right": ["cpu", "memory", "custom/network", "pulseaudio", "backlight", "custom/battery", "tray", "custom/power"],
"sway/workspaces": { "sway/workspaces": {
"disable-scroll": false, "disable-scroll": false,
"all-outputs": false, "all-outputs": false,
@@ -17,7 +17,7 @@
"custom/network": { "exec": "~/.config/waybar/scripts/network-cycle.sh", "return-type": "json", "interval": 300, "signal": 8, "on-click": "~/.config/waybar/scripts/network-cycle.sh next", "on-click-right": "~/.config/waybar/scripts/network-cycle.sh copy" }, "custom/network": { "exec": "~/.config/waybar/scripts/network-cycle.sh", "return-type": "json", "interval": 300, "signal": 8, "on-click": "~/.config/waybar/scripts/network-cycle.sh next", "on-click-right": "~/.config/waybar/scripts/network-cycle.sh copy" },
"pulseaudio": { "format": "{icon} {volume}%", "format-muted": "󰝟 muted", "format-icons": ["󰕿", "󰖀", "󰕾"], "tooltip-format": "{desc}", "on-click": "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle", "on-click-right": "~/.config/waybar/scripts/audio-output-cycle.sh" }, "pulseaudio": { "format": "{icon} {volume}%", "format-muted": "󰝟 muted", "format-icons": ["󰕿", "󰖀", "󰕾"], "tooltip-format": "{desc}", "on-click": "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle", "on-click-right": "~/.config/waybar/scripts/audio-output-cycle.sh" },
"backlight": { "format": "󰃠 {percent}%" }, "backlight": { "format": "󰃠 {percent}%" },
"battery": { "states": { "warning": 30, "critical": 15 }, "format": "{icon} {capacity}%", "format-charging": "󰂄 {capacity}%", "format-icons": ["󰁺", "󰁼", "󰁾", "󰂀", "󰁹"] }, "custom/battery": { "exec": "~/.config/waybar/scripts/battery-power.sh", "return-type": "json", "interval": 10, "tooltip": true },
"tray": { "spacing": 8 }, "tray": { "spacing": 8 },
"custom/power": { "custom/power": {
"format": "󰐥", "format": "󰐥",
+41
View File
@@ -0,0 +1,41 @@
#!/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}"
+3 -3
View File
@@ -12,9 +12,9 @@ window#waybar { background: @bg; color: @text; }
#workspaces button.focused { color: @bg; background: @blue; } #workspaces button.focused { color: @bg; background: @blue; }
#workspaces button.focused:hover { background: @blue; color: @bg; } #workspaces button.focused:hover { background: @blue; color: @bg; }
#workspaces button.urgent { background: @blue-dark; color: @text; } #workspaces button.urgent { background: @blue-dark; color: @text; }
#mode, #clock, #cpu, #memory, #custom-network, #pulseaudio, #backlight, #battery, #tray, #custom-power { padding: 0 10px; margin: 4px 2px; background: transparent; border-radius: 6px; } #mode, #clock, #cpu, #memory, #custom-network, #pulseaudio, #backlight, #custom-battery, #tray, #custom-power { padding: 0 10px; margin: 4px 2px; background: transparent; border-radius: 6px; }
#mode:hover, #clock:hover, #cpu:hover, #memory:hover, #custom-network:hover, #pulseaudio:hover, #backlight:hover, #battery:hover, #tray:hover, #custom-power:hover { background: @surface; } #mode:hover, #clock:hover, #cpu:hover, #memory:hover, #custom-network:hover, #pulseaudio:hover, #backlight:hover, #custom-battery:hover, #tray:hover, #custom-power:hover { background: @surface; }
#clock { color: @blue; font-weight: bold; background: transparent; } #clock { color: @blue; font-weight: bold; background: transparent; }
#cpu, #memory { color: @text; } #cpu, #memory { color: @text; }
#custom-power { color: @blue; font-size: 16px; } #custom-power { color: @blue; font-size: 16px; }
#battery.warning, #battery.critical { color: @blue; } #custom-battery.warning, #custom-battery.critical { color: @blue; }