Skip to content

Fix radar distances and zones staying unknown or stale when a target returns - #102

Draft
Bunton33 wants to merge 2 commits into
betafrom
fix/radar-distance-unknown
Draft

Bunton33 wants to merge 2 commits into
betafrom
fix/radar-distance-unknown

Conversation

@Bunton33

@Bunton33 Bunton33 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Version: 26.10.12.1 (no change; versioning handled at release time)

What does this implement/fix?

Do not merge until bench-tested on spare units. This is meant for the next normal firmware release after that bench test, not a hotfix.

Radar Still Distance, Radar Moving Distance, Radar Detection Distance and the zone sensors (Radar Zone 1/2/3 Occupancy and Radar Zone Occupancy) could stay unknown, stale or Clear while Radar Still Target was on. Radar Target, Radar Moving Target and Radar Still Target are not touched: they come straight from the radar, and this PR does not change them. The same fix for the MSR-1 is in ApolloAutomation/MSR-1#122.

What went wrong

  1. With Reduce DB Reporting on (every release since 25.7.29.1). While no target was seen, the distance filters returned unknown but kept the last published distance as their reference. When the target came back within 4 cm of that distance, the 5 cm Reduce DB Reporting check threw the new reading away. The distance stayed unknown, and every zone read Clear while Radar Still Target was on, until the distance moved 5 cm or more. Radar noise usually ends this within seconds, but a very still person can hold it for much longer: in the model below, a target at 200 cm that is lost for 5 s and comes back at 196 to 204 cm keeps Radar Zone Occupancy Clear for the whole run.
  2. With Reduce DB Reporting off too (every 26.x build, since they use ESPHome 2025.8 or later). The ld2410 component now sends a distance only when it changes, so our filters run only then. If the distance changed while the target was lost, and the target came back at that same distance, the filters never ran again. The result was the same: an unknown distance and Clear zones with Radar Still Target on, until the radar's distance next changed.
  3. The 1 s throttle had no timeout in front of it (26.3.2.1 and later, from Update for ESPHome 2025.8.0 release #40). For the same reason as case 2, a final reading that arrived inside the throttle window was lost for good. Someone who walked in and sat down could keep an older distance: 250 cm for someone seated at 230 cm in the model. The zones followed that stale distance, even with Reduce DB Reporting off. ESPHome's own default chain for these sensors has had this timeout since ESPHome PR 11920, "to prevent stuck targets".
  4. Minor. While nobody was there, unknown was sent again on every distance change that passed the throttle (61 a minute in the model). Home Assistant does not record the repeats, but they still go over the API.

What changed

  • 9d68d71 Publish radar distances again when a target returns, and keep the last one. The three distance filters use a NAN transition pattern: unknown is published once per loss, and the first distance the filter sees after the target returns always publishes. The 5 cm Reduce DB Reporting delta still applies while a target is tracked, so the publish rate while tracking is unchanged. Each throttle now has timeout: 1s with value: last in front of it, the same pair ESPHome uses for these sensors when no filters are set: after 1 s with no new distance it sends the last one again, once. Fixes 1, 3 and 4, and case 2 for losses under about 1 s.
  • 087e69e Check the radar zones against the radar's own distances. Zone 1-3 compare the radar's own distances (get_raw_state()) instead of the values sent to Home Assistant, so they follow the radar even when the filters did not run or the throttle held a value back. While the radar holds a target after a short loss it reports 0 cm, which is not a real position, so a 0 cm distance never counts toward Zone 1. Zones 2 and 3 only count distances above the previous zone's end. Fixes 2 for the zones, and also the case where someone settles a few cm past a zone edge and stays in the old zone, or in none.

The two commits must ship together. With the first commit alone, the timeout sends the radar's 0 cm hold distance through, and Zone 1 turns on for the whole hold (9 s of a 10 s hold in the model). The 0 cm guard in the second commit stops that.

Not changed:

  • After case 2 with a loss longer than about 1 s, the distance sent to Home Assistant can still read unknown until the radar's distance next changes. The zones are right in that case.
  • The radar dropping a very still person and not picking them up again until they move happens inside the LD2410 module. This YAML cannot change it.

Behavior changes to watch:

  • Zone edges. The zones now follow every change in the radar's distance instead of the throttled value. Someone seated within a few cm of a zone edge moves between zones as often as the distance changes. In the model, with the distance changing once a second (about what our own MSR-2 shows), Reduce DB Reporting off stays at 46 Zone 3 changes a minute and Reduce DB Reporting on goes from 12 to 46. With a distance that changes on every frame, it goes from 12 to 450 a minute either way.
  • 0 cm during the radar's hold. After a short blip, the timeout passes on the radar's own 0 cm hold distance, so Radar Still Distance can read 0 cm during the hold where today it reads unknown. The zones ignore it.

How it was validated

  • esphome config (ESPHome 2026.10.0b3, the version the investigation used) on every device YAML: MSR-2.yaml, MSR-2_BLE.yaml, MSR-2_Factory.yaml, beta-channel/MSR-2.yaml and beta-channel/MSR-2_BLE.yaml are valid. The only warning is one the current beta already has (esp32_improv on Factory).
  • Full esphome compile Integrations/ESPHome/MSR-2.yaml (ESP32-C3, ESP-IDF, same ESPHome version): compiled with 0 compiler warnings. apollo-msr-2.bin is 0x196f30 bytes with 9% of the app partition free (RAM 47.9%, flash 90.8%).
  • A host model compiled the lambdas copied verbatim from this branch and from beta against a model of ESPHome 2026.10's ld2410 publishing (flags first, then each changed distance), the 1 s settle on the target flags, the sensor filter chain (timeout, throttle_with_priority, lambda) and the template zone sensors. Zone 1 = 0-50 cm, Zone 2 = 50-150 cm, Zone 3 = 150-250 cm, one radar frame every 100 ms:
Scenario beta today This PR
Reduce DB Reporting on: still target at 200 cm, lost for 5 s, back at 202 cm for 30 s Still Distance unknown, all zones Clear for the full 30 s 202 cm, Zone 3 on
Same, returning anywhere from 196 to 204 cm unknown correct
Reduce DB Reporting off: distance moves to 180 cm during a 2 s loss, target returns at 180 cm unknown, all zones Clear Still Distance still unknown, Zone 3 on for the whole 27 s
Walk in from 300 cm and sit at 230 cm (Reduce DB Reporting off and on) Still Distance stuck at 250 cm 230 cm
One-frame blip, then a 10 s hold with 0 cm distances Zone 1 off, Still Distance unknown Zone 1 off, Still Distance 0 cm
Tracked at 200 cm with ±3 cm noise: Still Distance publishes a minute (Reduce DB Reporting off / on) 61 / 12 61 / 12
Nobody there, distance field noise: publishes a minute 61 unknowns 0
Seated at the End Zone 3 edge, ±3 cm, distance changing once a second: Zone 3 changes a minute (off / on) 46 / 12 46 / 46

CI on this PR builds the three CI YAMLs with ESPHome stable, beta and dev.

Bench-test plan (spare units only)

Flash a spare MSR-2 with this branch and put an unpatched unit beside it as a control. Run each step with Reduce DB Reporting off, then again with it on, and note Radar Timeout first.

  1. Sit still at a zone edge (set End Zone 1 or 2 about where you sit) for 5 minutes. Radar Still Distance stays a number while Radar Still Target is on, and the zone you sit in stays on. Count zone changes in the logbook against the control.
  2. Step in and out. Leave the radar's view for longer than Radar Timeout, then come back to the same spot and keep still. Repeat 10 times. Each time, the distance comes back as a number, and the right zone turns on, within about a second of Radar Still Target turning on.
  3. Walk in and sit down. Radar Still Distance settles on the seated distance within 1 to 2 s and stays there.
  4. Nobody in the room for 10 minutes. Distances read unknown, every zone is Clear, and Zone 1 never turns on.
  5. Restart the radar (Restart Radar) with someone seated. The radar may drop the seated person until they move; that is the module's own behavior. Zone 1 must not turn on from 0 cm readings during the radar's hold, and distances and zones must come back when Radar Still Target does.
  6. OTA and power cycle. OTA the spare unit to this build, then power-cycle it. After boot, distances and zones come back with the first target, and Reduce DB Reporting keeps its setting.
  7. Publish rate. With someone moving around for 10 minutes, the number of Radar Still Distance changes in the history is about the same as on the control.

Types of changes

  • Bugfix (fixed change that fixes an issue)
  • New feature (thanks!)
  • Breaking change (repair/feature that breaks existing functionality)
  • Dependency Update - Does not publish
  • Other - Does not publish
  • Website of github readme file update - Does not publish
  • Github workflows - Does not publish

Checklist / Checklijst:

  • The code change has been tested and works locally
  • The code change has not yet been tested

If user-visible functionality or configuration variables are added/modified:

  • Added/updated documentation for the web page

🤖 Generated with Claude Code

Bunton33 and others added 2 commits November 2, 2026 18:48
…t one

The three radar distance filters returned NAN while no target was seen
but never updated last_reported_value. With Reduce DB Reporting on, a
target that came back within 4 cm of the last published distance was
dropped by the 5 cm delta check, so Radar Still/Moving/Detection
Distance stayed unknown and Radar Zone 1-3 and Radar Zone Occupancy read
Clear while Radar Still Target was on, until the distance moved 5 cm.

Use the NAN state transition pattern: unknown is published once per
loss, and the first distance after the target comes back always
publishes. The 5 cm delta still applies while a target is tracked.

The throttle_with_priority: 1000ms in front of each lambda also had no
timeout partner. ESPHome passes on only changed distances, so a final
reading that arrived inside the throttle window was lost for good and
the distance (and the zones) kept an older value, even with Reduce DB
Reporting off. Put timeout: 1s with value: last in front of the
throttle, the same pair ESPHome uses for these sensors by default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The zone sensors compared the distances sent to Home Assistant. ESPHome
runs those filters only when the radar's distance changes, so when a
target came back at the distance it had while lost, Radar Still/Moving
Distance stayed unknown and Radar Zone 1-3 and Radar Zone Occupancy read
Clear while Radar Still Target was on, even with Reduce DB Reporting
off. The 1 s throttle and, with Reduce DB Reporting on, the 5 cm delta
also left the zones on an older distance, so someone who settled a few
cm past a zone edge stayed in the old zone, or in none.

Compare the radar's own distances (get_raw_state) instead. While the
radar holds a target after a short loss it reports 0 cm, which is not a
real position, so a 0 cm distance never counts toward Zone 1. Zones 2
and 3 only count distances above the previous zone's end. This also
keeps the timeout added in the previous commit from turning Zone 1 on
during that hold.

Someone seated right at a zone edge now moves between zones as often as
the radar's distance changes, not at most once a second.

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

coderabbitai Bot commented Nov 2, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants