Skip to content

fix(vag_vehicle): select telemetry by timestamp and explain SoC selection - #213

Merged
frahlg merged 1 commit into
srcfl:mainfrom
segran2:fix/vag-vehicle-measurement-timestamps
Dec 1, 2026
Merged

frahlg merged 1 commit into
srcfl:mainfrom
segran2:fix/vag-vehicle-measurement-timestamps

Conversation

@segran2

@segran2 segran2 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Portal datasets may contain multiple points for the same field or different SoC candidates. index_points previously kept the last row per key/name and lookup chose the greatest row number, allowing an older measurement to hide a newer one.

Both stages now compare parsed measurement timestamps, including timezone offsets and fractional seconds. Valid timestamps outrank missing or invalid ones; equal or unavailable timestamps retain row-order fallback. Before indexing, non-numeric SoC values and values outside 0–100% are discarded so a newer invalid row cannot hide an older valid measurement. Discarded candidates remain visible in the selection diagnostics. Bumps vag_vehicle to 0.3.1 and refreshes generated metadata.

Adds diagnostics once per newly downloaded dataset: VIN-masked filename, portal fetch time, candidate count, selected row and selection reason. Logs up to 32 SoC candidate rows (including those discarded by indexing), with key/name, value, timestamp and selection flag; excess rows are counted. No raw datasets or credentials are logged. This distinguishes stale upstream measurements from incorrect local selection.

Validation:

  • Synthetic regression harness covers duplicate keys/shared names, candidate GUIDs, both row orders, missing/invalid/equal timestamps, offsets, fractional seconds, empty values and nil inputs. Original 0.3.0 fails the newest-first case.

  • Diagnostic tests cover discarded candidates, selection reasons, no candidates, truncation and VIN masking.

  • make test-driver ID=vag_vehicle: 40 passed, 16 skipped.

  • make check: 5091 passed, 951 skipped on the branch updated against main; manifests, generated metadata, source history, host API and sandbox checks passed.

  • Updated code installed locally on FTW Core v0.159.0-beta.1+local.3 with an Audi e-tron: restart/authentication/download succeeded; actual dataset diagnostics showed one SoC candidate with an old measurement timestamp and correctly masked VIN. Firmware was not recorded. Competing SoC candidates in a single dataset are covered by the regression harness.

  • Live follow-up on 12 November 2026: two separate portal datasets were fetched at 17:04 and 17:14 CET. Each contained one candidate at 80%, with the same measurement timestamp of 11 November 2026 at 05:31:06 UTC. The driver selected that observation by timestamp and logged the fetch and measurement times separately. This confirms that a newly delivered file can contain an old measurement. These runs do not exercise competing candidates in the same real dataset.

No credentials, VINs or raw site datasets are included. This does not refresh stale upstream data. After merge, FTW must advance its bundled-driver source pin and regenerate its recovery snapshot to reach bundled installations.

Fixes #188.

@segran2 segran2 changed the title fix(vag_vehicle): select telemetry by measurement timestamp fix(vag_vehicle): select telemetry by timestamp and explain SoC selection Nov 6, 2026
@segran2
segran2 marked this pull request as ready for review November 12, 2026 16:20

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vag_vehicle 0.3.1: picks the newest SoC point by timestamp instead of dataset order, both when indexing duplicate keys/names and when choosing between candidate fields. Adds a bounded, VIN-masked diagnostic log per dataset.

  • Logic reads right: valid timestamp beats missing/invalid, equal or unknown times fall back to row order, fractional seconds compared. parse_dataset passes index_points straight through, so the second return value reaches driver_poll.
  • Out-of-range SoC values (<0, >100, non-numeric) are now dropped at index time but still kept as candidates for the log. Intended per the tests, but it does change behavior for any dataset that used to surface a bad value.
  • Low: up to 33 info log lines per dataset poll. Fine for now, worth watching if the poll interval is short.
  • Security screen: no secrets, no new hosts or dependencies, VIN masked in the log (tested). Synthetic test data only.
  • CI green (drivers, dco, secret scan).

Safe to merge from my read.

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Picks the SoC row by parsed measurement time (offset- and fraction-aware) instead of last-row-wins, falls back to row order on ties or missing times, and logs a bounded, VIN-masked selection summary per new dataset.

Read the whole diff, nothing blocking.

  • Security: nothing. The diagnostics log only SoC key/name/value/timestamp and a masked filename, capped at 32 rows. server_now_s is defined at file scope, so the fetched-at field is fine.
  • Needs a decision (behaviour change): index_points now drops SoC rows that are non-numeric or outside 0–100 before the timestamp comparison. So a newer N/A or 101 loses to an older valid 73 %, and the driver reports the older value as current. The test asserts this on purpose, but it isn't in the PR description. If an invalid newest row should mean "unknown", this will mask a stale-upstream case, which is the very thing the diagnostics are meant to expose. Fine if intended, just worth saying out loud.
  • Low: iso_epoch doesn't range-check month/day/hour, so a malformed-but-matching timestamp like 2026-13-45T… gets an epoch and can outrank a valid one. Unlikely from the portal, cheap to guard.
  • Nit: the test names a local select, shadowing the builtin. Harmless.

Safe to merge from my read, assuming the invalid-SoC fallback is intended.

@frahlg

frahlg commented Nov 21, 2026

Copy link
Copy Markdown
Member

This PR is the fix for #188; main still picks the SoC by sequence number, so the bug is live there. One point from the review: SoC rows that are not numbers or fall outside 0–100 are now dropped before the timestamp compare. Please say why that is better than keeping them as candidates. Do you have a real portal dataset, with the VIN removed, with two SoC rows at different times? The issue only has synthetic input.

segran2 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up to @frahlg’s review.

The SoC value is validated before the timestamp comparison because indexing keeps only one row per key or name. If a newer row contains a non-numeric value such as N/A, or a percentage outside 0–100, it could overwrite an older valid row before lookup. Rejecting the value after indexing would be too late to recover that older measurement. The rejected rows remain visible in the bounded candidate diagnostics. The regression tests cover N/A, -1 and 101.

I checked the available logs for 146 real portal datasets. Every file contained at most one SoC candidate. There are real measurements with different timestamps across separate files, but I have yet to find a real file with two timestamped SoC candidates. Selection between competing rows in the same file remains covered by the synthetic regression tests. The existing diagnostics can capture both rows if the portal delivers such a file.

Separately, VAG has acknowledged problems with SoC updates in its EU Data Act portal since late October, according to the operator. SoC updates are still arriving sporadically. For example, two portal files fetched on 21 November at 14:27 and 14:47 CET both carried an 80% measurement from 20 November at 08:09:56 UTC. The patch chooses the latest valid measurement available in a dataset. The freshness of that measurement still depends on the portal.

frahlg added a commit that referenced this pull request Nov 21, 2026
Fredrik, 21 Nov 2026: a driver that can command its device ships with
control on; one that cannot says so plainly.

- Remove local easee and zaptec. Easee and Zaptec document no local
  API; the two drivers read an invented Modbus map and a made-up HTTP
  path. Use easee_cloud, zaptec_cloud or the charger's OCPP to FTW.
- tesla_wall_connector 0.2.0: Tesla offers no way for another system to
  set its current. driver_command now refuses instead of answering true
  while writing nothing.
- teslamate_vehicle 0.2.1, home_assistant_vehicle 0.1.1, tesla_cloud
  0.2.1: the description and notes say why FTW cannot control the car
  through them and where control goes instead.

vag_vehicle waits for #213, which has right of way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
frahlg added a commit that referenced this pull request Nov 21, 2026
* fix: read-only EV drivers say why, drop two that read nothing

Fredrik, 21 Nov 2026: a driver that can command its device ships with
control on; one that cannot says so plainly.

- Remove local easee and zaptec. Easee and Zaptec document no local
  API; the two drivers read an invented Modbus map and a made-up HTTP
  path. Use easee_cloud, zaptec_cloud or the charger's OCPP to FTW.
- tesla_wall_connector 0.2.0: Tesla offers no way for another system to
  set its current. driver_command now refuses instead of answering true
  while writing nothing.
- teslamate_vehicle 0.2.1, home_assistant_vehicle 0.1.1, tesla_cloud
  0.2.1: the description and notes say why FTW cannot control the car
  through them and where control goes instead.

vag_vehicle waits for #213, which has right of way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>

* docs: drop two unsourced claims from driver comments

Review: 'most business cars' skipping Tesla's signed commands has no
source, and Home Assistant answering 500 on a failed action was not
confirmed. Both were comments only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>

---------

Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@frahlg

frahlg commented Nov 21, 2026

Copy link
Copy Markdown
Member

Thanks, that answers both points. Rejecting a bad value before indexing keeps the older good row, and the tests cover N/A, -1 and 101. The 146 real files with at most one SoC row each help: the timestamp choice matters only once the portal sends two, and the diagnostics will show it when it does.

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What it does: vag_vehicle now picks the SoC point by measured timestamp (offset and fractional-second aware) in both index_points and lookup, and logs a bounded, VIN-masked diagnostic per downloaded dataset.

  • note: index_points now also drops SoC rows outside 0-100 or non-numeric before indexing (vag_vehicle.lua index_points). That is a behaviour change beyond the title. The tests cover it ("N/A", -1, 101, and 0 kept), so I think it is intended, but it deserves a line in the description.
  • nit: the comment above point_is_newer says the same row keeps the earlier candidate priority, but a full tie resolves to the higher seq (candidate.seq > current.seq). Behaviour matches the tests, the comment just reads the other way.
  • security screen: no secrets, no new dependencies, no new hosts. Diagnostics log only SoC key/name/value/timestamp and a masked VIN. Filename goes through a character whitelist and is truncated.

Safe to merge from my read. CI is green on cb6befa.

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes SoC selection in vag_vehicle so the newest measured point wins by timestamp (with row order as the tie/missing-time fallback) instead of the last row in the dataset. Adds date validation in iso_epoch, drops out-of-range SoC rows before indexing, and logs a bounded, VIN-masked selection summary.

  • No findings from the security screen: no new hosts, deps or secrets. The log helper sanitises and caps values (32 candidates, 100 chars each).
  • Checked: sha256 and size_bytes in index.yaml/manifest match the driver file at this head (699729…4647, 50958 B).
  • Minor: fetched_at_epoch_s relies on the server_now_s global and prints unknown if it isn't set. Fine, just not useful in that case.
  • Tests cover duplicate keys, ordering, missing/invalid/offset/fractional timestamps, leap days and range-checked SoC.

Safe to merge from my read.

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vag_vehicle 0.3.1: picks the newest SoC/telemetry point by timestamp instead of last row, validates timestamps (month/day/hour/offset ranges, leap years), drops non-numeric or out-of-range SoC rows before indexing, and logs a bounded, VIN-masked selection summary. CI is green on all 5 checks.

  • Logic reads right to me: valid time outranks missing/invalid, fractional seconds break same-second ties, row order is the last fallback.
  • Minor: log_soc_selection reads server_now_s. I couldn't see where it is defined in the part of the diff I read. If it's a global that can be nil it just logs "unknown", so harmless, but worth a glance.
  • Security screen: no secrets, no new deps, no new network hosts. The log fields are sanitized (safe(), 32-candidate cap), so no raw dataset leaks.
  • I read the driver, changelog and manifest changes. I did not read the new Lua harness test or the Python test in full.

Safe to merge from my read, with the server_now_s question as the one thing for the author to confirm.

@segran2
segran2 force-pushed the fix/vag-vehicle-measurement-timestamps branch from 6f3234e to b52a10a Compare November 30, 2026 19:12
Signed-off-by: Goran Andersson <goran@andersson-stehag.se>
@segran2
segran2 force-pushed the fix/vag-vehicle-measurement-timestamps branch from b52a10a to 27ada71 Compare November 30, 2026 19:16

segran2 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

Hi @frahlg, PR #213 has been rebased onto the latest main. It is now mergeable, and CI is green. Could you please merge it? Afterward, I’ll update FTW’s bundled-driver pin and regenerate the recovery snapshot.

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Selects the newest SoC point by parsed timestamp (offsets, fractional seconds) instead of dataset row order, drops non-numeric / out-of-range SoC rows before indexing, and adds a bounded per-dataset diagnostic log (VIN-masked, max 32 candidates). Bumps vag_vehicle to 0.3.1.

  • Checked: parse_dataset passes the second return value through, so candidates_or_error in driver_poll gets the candidate list, and the decode-failure path still returns nil, err.
  • Checked: point_is_newer only calls ts:match after iso_epoch succeeded, so a nil timestamp can't error. Equal or invalid times fall back to row order as described.
  • Security screen: no secrets, no new hosts or dependencies. The log line prints values and timestamps for SoC rows only, never the raw dataset.
  • Minor: a soc = 0 row is kept (zero is valid), which the test covers. Nice.

Safe to merge from my read. CI is green. I haven't run the Lua harness myself.

@frahlg
frahlg merged commit fe6584d into srcfl:main Dec 1, 2026
5 checks passed
@frahlg

frahlg commented Dec 1, 2026

Copy link
Copy Markdown
Member

Thanks! Merged as fe6584d. The signed channel built it on main, so vag_vehicle 0.3.1 is out. The selection log will show us quickly whether a stale SoC came from the portal or from the driver.

Please go ahead with FTW's bundled-driver pin when you have time. The same move will carry deye-svk 0.2.3 from #224.

frahlg added a commit that referenced this pull request Dec 1, 2026
Brings in deye-svk 0.2.3 (#224) and vag_vehicle 0.3.1 (#213). The only
conflict was CHANGELOG.md, where both sides added an Unreleased entry;
both are kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@segran2

segran2 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Reqdy for merge

@segran2

segran2 commented Dec 1, 2026 via email

Copy link
Copy Markdown
Contributor Author

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.

VAG: select SoC observations by measurement timestamp instead of dataset order

3 participants