JanDeDobbeleer/oh-my-posh · error

date field must be a valid number, got: %s

Error message

date field must be a valid number, got: %s

What it means

Nightscout entries carry their date as a JSON field that must deserialize into a numeric (unix) timestamp. The custom UnmarshalJSON first tries a direct JSON number, then a string-encoded number; if the Date field is neither, it errors with 'date field must be a valid number, got: <raw>'.

Source

Thrown at src/segments/nightscout.go:76

	if err := json.Unmarshal(data, aux); err != nil {
		return err
	}

	if aux.Date == "" {
		return nil
	}

	if i, err := aux.Date.Int64(); err == nil {
		n.Date = i
		return nil
	}

	if f, err := aux.Date.Float64(); err == nil {
		n.Date = int64(f)
		return nil
	}

	return fmt.Errorf("date field must be a valid number, got: %s", aux.Date)
}

func (ns *Nightscout) Template() string {
	return " {{ .Sgv }} "
}

func (ns *Nightscout) Enabled() bool {
	data, err := ns.getResult()
	if err != nil {
		return false
	}
	ns.NightscoutData = *data
	ns.TrendIcon = ns.getTrendIcon()

	return true
}

func (ns *Nightscout) getTrendIcon() string {

View on GitHub (pinned to 0976794618)

Solutions

  1. Check the raw JSON your Nightscout URL returns and confirm `date` is an epoch number or numeric string
  2. Configure the Nightscout server/API version to return epoch-based dates (date field as number)
  3. Update oh-my-posh in case a newer version handles ISO date strings
  4. If you control the middleware, convert ISO timestamps to epoch ms before the response reaches the segment

Example fix

// before (API response)
{ "sgv": 120, "date": "2024-01-01T12:00:00.000Z" }
// after
{ "sgv": 120, "date": 1704110400000 }
Defensive patterns

Strategy: type-guard

Validate before calling

// validate the date field before unmarshaling
var raw struct { Date json.RawMessage `json:"date"` }
json.Unmarshal(body, &raw)
var num float64
if err := json.Unmarshal(raw.Date, &num); err != nil {
    // date is not numeric (e.g. ISO string) — fix source data
}

Type guard

func isNumericDate(v json.RawMessage) bool {
    var n float64
    return json.Unmarshal(v, &n) == nil
}

Try / catch

// caller-side: tolerate malformed entries
var entries []Nightscout
if err := json.Unmarshal(body, &entries); err != nil {
    // render no glucose data rather than failing the prompt
}

Prevention

When it happens

Trigger: Unmarshaling a Nightscout API response whose `date` field is a non-numeric string (e.g. an ISO-8601 timestamp like "2024-01-01T12:00:00Z", or null), instead of a numeric or numeric-string epoch value.

Common situations: Nightscout server configured to emit ISO date strings (different plugin/version); a proxy or mock returning null dates; hitting a different endpoint whose schema differs from the expected entries format.

Related errors


AI-assisted analysis of JanDeDobbeleer/oh-my-posh@0976794618 (2026-08-31). Data as JSON: /api/errors/c94f976b34787278. Report an issue: GitHub.