Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active August 24, 2026 17:46
Show Gist options
  • Select an option

  • Save scivision/38f2013976c78e9d4288582c81ac9cb8 to your computer and use it in GitHub Desktop.

Select an option

Save scivision/38f2013976c78e9d4288582c81ac9cb8 to your computer and use it in GitHub Desktop.
Raspberry Pi low voltage and temperature Python check
#!/usr/bin/env python3
"""Decode Raspberry Pi "vcgencmd throttled" bitmask into text."""
import argparse
import re
import subprocess
import sys
BITMAP = {
0: "Under-voltage detected",
1: "Arm frequency capped",
2: "Currently throttled",
3: "Soft temperature limit active",
16: "Under-voltage has occurred",
17: "Arm frequency capping has occurred",
18: "Throttling has occurred",
19: "Soft temperature limit has occurred",
}
def parse_throttled_value(text: str) -> int:
"""Parse a throttled value from vcgencmd output or a raw integer string."""
s = text.strip()
# Accept full vcgencmd output like "throttled=0x50000".
if "=" in s:
s = s.split("=", 1)[1].strip()
if re.fullmatch(r"0x[0-9a-fA-F]+", s):
return int(s, 16)
if re.fullmatch(r"\d+", s):
return int(s, 10)
raise ValueError(f"Could not parse throttled value: {text!r}")
def read_vcgencmd() -> int:
"""Run vcgencmd get_throttled and return decoded integer bitmask."""
output = subprocess.check_output(["vcgencmd", "get_throttled"], text=True)
return parse_throttled_value(output)
def main() -> int:
parser = argparse.ArgumentParser(
description="Decode Raspberry Pi throttled flags to human-readable text."
)
parser.add_argument(
"value",
nargs="?",
help=(
"Optional throttled value, e.g. 0x50000, 327680, or "
"'throttled=0x50000'. If omitted, runs vcgencmd get_throttled."
),
)
args = parser.parse_args()
value = parse_throttled_value(args.value) if args.value else read_vcgencmd()
print(f"throttled=0x{value:x}")
set_known_bits = [bit for bit in sorted(BITMAP) if value & (1 << bit)]
if not set_known_bits:
print("No known throttled bits are set.")
else:
print("Set throttled bits:")
for bit in set_known_bits:
print(f" bit {bit}: {BITMAP[bit]}")
unknown_bits = [bit for bit in range(value.bit_length()) if value & (1 << bit) and bit not in BITMAP]
if unknown_bits:
print("Unknown set bits: " + ", ".join(str(bit) for bit in unknown_bits))
return 0
if __name__ == "__main__":
raise SystemExit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment