Skip to content

tyres: use HA's unit metadata instead of assuming kPa (#39) - #63

Open
ThinkOffApp wants to merge 1 commit into
mainfrom
fix/tyre-pressure-units
Open

ThinkOffApp wants to merge 1 commit into
mainfrom
fix/tyre-pressure-units

Conversation

@ThinkOffApp

Copy link
Copy Markdown
Owner

Fixes #39@eclass reported "tyres holding steady at thirty bar" on 11 Sep. Thirty bar is ten times a car tyre, and it had been saying so for a week.

The trace, raw value to spoken value

@codexmb asked for this before any conversion was chosen, and it changes what the fix is:

  1. GET /api/states returns each entity with state and attributes.unit_of_measurement.
  2. mercedesme.py reads attrs (it already did, line 327) but passed only the state string to _norm_value.
  3. The value was filed under the hardcoded key tires_kpa whatever unit HA actually reported.
  4. agent.py emitted "tyre pressures kPa <n>" — a bare number plus a unit word.
  5. The model converted it itself, and a decade went missing.

So the bug is not a missing conversion. The unit was fetched and thrown away.

What changed

  • _pressure_kpa() converts from bar / psi / kPa / mbar / hPa explicitly, and returns None on an unknown or missing unit rather than assuming kPa. An unlabelled pressure is not a kPa reading.
  • The fetch passes attributes.unit_of_measurement through for tyre entities.
  • fmt_tyres_bar() renders bar to one decimal, as the issue asks, so the model never converts at all.
  • A physically implausible reading (outside 1.0–4.5 bar) is flagged as a sensor fault instead of stated as the pressure.
  • webchat.py was interpolating the raw dict into the prompt (tyres {'front_left': 250, ...} kPa); it now uses the same formatter.

Tests

tests/test_tyre_units.py, 7 cases: every unit HA might report, the refusal on an unknown unit, 30 psi no longer becoming thirty-anything, bar-with-one-decimal output, wheel ordering, the implausibility flag, and non-pressure entities untouched.

Run in isolation and with the suite. tests.test_dash_loop fails on main as well — pre-existing, unrelated to this change.

Not verified

Which unit this particular car's HA reports is still unconfirmed — the Mini's snapshot is 59 h stale and the local one is empty. "30 meant psi" remains a hypothesis. The fix removes the assumption either way, and the implausibility flag catches the symptom regardless of cause. Worth confirming against the live car when it is next reachable.

🤖 Generated with Claude Code

@vercel

vercel Bot commented Sep 18, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
carwatch-dev Ready Ready Preview Sep 18, 2026 4:56pm UTC

Request Review

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create an environment for this repo.

@eclass told petrus his tyres were at "thirty bar" on 11 Sep. Thirty bar is
ten times a car tyre, and it had been saying so for a week.

The fetch already asks Home Assistant for every entity's attributes, which
carry unit_of_measurement, and then dropped it: _norm_value only ever received
the state string. Whatever HA reported - psi, bar, kPa - was filed under the
hardcoded key "tires_kpa". The prompt then handed the model a bare number and
a unit word and left it to convert, which is where a decade can go missing.

- _pressure_kpa() converts from bar/psi/kPa/mbar/hPa explicitly, and returns
  None on an unknown or missing unit rather than assuming kPa. An unlabelled
  pressure is not a kPa reading.
- the fetch passes attributes.unit_of_measurement through for tyre entities.
- fmt_tyres_bar() renders bar to one decimal, so the model never converts at
  all, and flags a physically implausible reading (outside 1.0-4.5 bar) as a
  sensor fault instead of stating it as the pressure.
- webchat was interpolating the raw dict into the prompt ("tyres {'front_left':
  250, ...} kPa"); it now uses the same formatter as the agent.

Tests cover each unit, the refusal on an unknown unit, 30 psi no longer
becoming thirty-anything, wheel ordering, and the implausibility flag.

Not yet confirmed against the live car: the Mini's snapshot is 59h stale and
the local one is empty, so which unit this particular HA reports is still
unverified. The fix removes the assumption either way.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Car agent reports tyre pressure as "thirty bar"

2 participants