JanDeDobbeleer/oh-my-posh · warning
unable to find battery state based on output
Error message
unable to find battery state based on output
What it means
On macOS the battery segment shells out to 'pmset -g batt' and parses the output with a regex expecting 'NNN%; STATE;'. If the regex doesn't produce exactly the PERCENTAGE and STATE captures, the output shape is unrecognized and this error is returned.
Source
Thrown at src/runtime/battery/battery_darwin.go:34
case "discharging":
return Discharging
case "AC attached":
return NotCharging
case "full":
return Full
case "empty":
return Empty
case "charged":
return Full
default:
return Unknown
}
}
func parseBatteryOutput(output string) (*Info, error) {
matches := regex.FindNamedRegexMatch(`(?P<PERCENTAGE>[0-9]{1,3})%; (?P<STATE>[a-zA-Z\s]+);`, output)
if len(matches) != 2 {
return nil, errors.New("unable to find battery state based on output")
}
var percentage int
var err error
if percentage, err = strconv.Atoi(matches["PERCENTAGE"]); err != nil {
return nil, errors.New("unable to parse battery percentage")
}
// sometimes it reports discharging when at 100, so let's force it to Full
// https://github.com/JanDeDobbeleer/oh-my-posh/issues/3729
if percentage == 100 {
return &Info{
Percentage: percentage,
State: Full,
}, nil
}
return &Info{View on GitHub (pinned to 0976794618)
Solutions
- Guard the battery segment in your theme to skip when no battery exists (check env or wrap in a template conditional)
- Verify 'pmset -g batt' output manually in the terminal; if localized, expect this failure and disable the segment
- Update oh-my-posh - newer versions may handle more pmset output variants
Example fix
// template guard before rendering battery
{{ if ne .Error "unable to find battery state based on output" }}
{{ .Percentage }}%
{{ end }} Defensive patterns
Strategy: try-catch
Validate before calling
out, _ := exec.Command("pmset", "-g", "batt").Output()
if !strings.Contains(string(out), "%") {
// no battery data; skip battery segment
} Try / catch
info, err := battery.Get()
if err != nil {
if strings.Contains(err.Error(), "battery state") {
return nil // render nothing; machine has no battery
}
return err
} Prevention
- Conditionally enable the battery segment per machine (templates on .Error)
- Test the segment on desktops/VMs where pmset output differs
- Keep oh-my-posh updated for improved pmset parsing
When it happens
Trigger: parseBatteryOutput receives pmset output that lacks the pattern '<digits>%; <state>;' - e.g. 'No Batteries Found', 'Using AC Power' without percentage, or a localized pmset output.
Common situations: Desktop Mac or Mac without battery; external display/UPS reporting unusual pmset text; non-English system locale changing pmset wording; running in a VM where pmset returns nothing useful.
Related errors
- unable to parse battery percentage
- unable to find battery state based on output
- unable to parse battery percentage
- unable to parse energy_full
- no battery found
AI-assisted analysis of JanDeDobbeleer/oh-my-posh@0976794618 (2026-08-31).
Data as JSON: /api/errors/22839b5f82bf5e3d.
Report an issue: GitHub.