Skip to content

feat(obd): let OBD sensors extract values by byte offset - #62

Merged
eigger merged 1 commit into
mainfrom
feat/obd-offset-decode
Jul 30, 2026
Merged

feat(obd): let OBD sensors extract values by byte offset#62
eigger merged 1 commit into
mainfrom
feat/obd-offset-decode

Conversation

@eigger

@eigger eigger commented Jul 30, 2026

Copy link
Copy Markdown
Owner

formula variables address only the first 20 bytes of a response, which is not enough for values that sit deep in a long frame. The Hyundai 21 03 block is the case at hand: it answers on the engine ECU with DPF data and the total odometer in one response, and the odometer (OBDb's HYUNDAI_ODO_V2) lives at bytes 57-60 as a 32-bit value divided by 1000.

Accept the existing decode block on OBD sensors and prefer it over formula when both are set. DecodeConfig and Decoder.decodeStructured are already used by advertisement and notify sensors, so this only wires the OBD dispatch path to them — offset, length, type, endian, scale, offset_value, bitmask and map all behave as they do elsewhere.

decodeStructured returns null when offset + length runs past the response, and emit() drops nulls, so a car whose frame is too short publishes nothing rather than a wrong number. A gasoline car with a short 21 03 block therefore stays silent instead of reporting garbage.

Also warn during validation when an OBD sensor has a pid but neither formula nor decode, since it can never produce a value. Presets are expanded into formula at load time, so preset-based sensors do not trip this.

formula variables address only the first 20 bytes of a response, which is not
enough for values that sit deep in a long frame. The Hyundai 21 03 block is
the case at hand: it answers on the engine ECU with DPF data and the total
odometer in one response, and the odometer (OBDb's HYUNDAI_ODO_V2) lives at
bytes 57-60 as a 32-bit value divided by 1000.

Accept the existing `decode` block on OBD sensors and prefer it over
`formula` when both are set. DecodeConfig and Decoder.decodeStructured are
already used by advertisement and notify sensors, so this only wires the OBD
dispatch path to them — offset, length, type, endian, scale, offset_value,
bitmask and map all behave as they do elsewhere.

decodeStructured returns null when offset + length runs past the response, and
emit() drops nulls, so a car whose frame is too short publishes nothing rather
than a wrong number. A gasoline car with a short 21 03 block therefore stays
silent instead of reporting garbage.

Also warn during validation when an OBD sensor has a pid but neither formula
nor decode, since it can never produce a value. Presets are expanded into
formula at load time, so preset-based sensors do not trip this.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@eigger
eigger merged commit 5b1da69 into main Jul 30, 2026
3 checks passed
@eigger
eigger deleted the feat/obd-offset-decode branch July 30, 2026 02:15
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.

1 participant