dotfiles/systemd/system/timer-scripts/log_battery.sh

45 lines
1.3 KiB
Bash
Raw Normal View History

2023-12-23 20:14:15 -07:00
#!/usr/bin/env bash
# Look into /sys/class/power_supply for your battery-name. It's one of the
# symlinks
2023-12-23 20:14:15 -07:00
declare -r battery_name="$1"
declare -r write_path="$2"
2023-12-23 20:14:15 -07:00
if [[ $# -ne 2 ]]; then
cat <<HELP
log_battery systemd.timer v2.1
2023-12-23 20:14:15 -07:00
USAGE: $(basename "$0") <battery-name> <csv-path>
2023-12-23 20:14:15 -07:00
EXAMPLES:
$(basename "$0") BAT0 /home/emiliko/loggers/battery.csv
HELP
exit 1
elif [[ ! -e "$write_path" && -d "$(dirname "$write_path")" ]]; then
echo "hostname,time,charge,charge_full,percent,state,energy_rate" > "$write_path"
fi
2023-12-23 20:14:15 -07:00
declare -r $(xargs -0 < "/sys/class/power_supply/${battery_name}/uevent")
awk \
-v hostname="$(hostname)" \
-v time="$(date -u +"%Y-%m-%d %H:%M:%S UTC")" \
-v state="$POWER_SUPPLY_STATUS" \
-v a="$POWER_SUPPLY_CHARGE_NOW" \
-v b="$POWER_SUPPLY_CHARGE_FULL" \
-v c="$POWER_SUPPLY_CHARGE_FULL_DESIGN" \
-v d="$POWER_SUPPLY_VOLTAGE_NOW" \
-v e="$POWER_SUPPLY_CURRENT_NOW" \
-v f="$POWER_SUPPLY_VOLTAGE_MIN_DESIGN" \
'
BEGIN {
printf "%s,%s,", hostname, time
printf "%0.4f,", a*f/10**12 # Charge in Wh
printf "%0.4f,", b*f/10**12 # Charge when full in Wh
printf "%0.2f%%,", a/b*100 # Battery capacity in percent
printf "%s,", state
# Energy rate in W is always absolute, so use state to determine +/-
printf "%0.4f\n", e*d/10**12
2023-12-23 20:14:16 -07:00
}
' | awk '{print tolower($0)}' >> "$write_path"