Skip to content

Filter LD2450 NaN to 0 when no target detected - #91

Merged
TrevorSchirmer merged 2 commits into
betafrom
fix/ld2450-nan-when-no-target
May 3, 2026
Merged

TrevorSchirmer merged 2 commits into
betafrom
fix/ld2450-nan-when-no-target

Conversation

@bharvey88

@bharvey88 bharvey88 commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

Version: 26.3.2.2

What does this implement/fix?

Restores the pre-2026.3 behavior of the LD2450 per-target numeric sensors so they report `0` instead of `unknown` when a target slot is empty.

Background

ESPHome PR esphome/esphome#13602 changed the `ld2450` component to publish `NaN` for `x`, `y`, `speed`, `angle`, `distance`, and `resolution` when no target is present in a given slot. Home Assistant renders `NaN` as `unknown`, which breaks numeric_state triggers, history graphs, and statistics built around these entities.

The change was intentional upstream, intended to disambiguate "no target" from coordinate (0, 0). However, in Home Assistant the resulting `unknown` state breaks numeric_state triggers, history graphs, and statistics built on these entities. This PR restores the prior numeric behavior for Apollo users until upstream provides a non-breaking opt-in.

Closes #89.

Change

Adds a single-line lambda filter (`isnan(x) ? 0.0 : x`) to all 18 per-target numeric sensors (target_1/2/3 x x/y/speed/angle/distance/resolution).

The `direction` text_sensor (already shows "NA"), target/zone counts, and binary sensors are unaffected.

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

Restores pre-2026.3 behavior so the LD2450 per-target numeric sensors
report 0 instead of unknown when a target slot is empty.

Upstream ESPHome PR esphome/esphome#13602 changed the ld2450 component
to publish NaN for x, y, speed, angle, distance, and resolution when
no target is present. Home Assistant renders NaN as unknown, which
breaks numeric_state triggers, history graphs, and statistics built
around these entities.

Adds an isnan(x) ? 0.0 : x lambda filter to all 18 per-target numeric
sensors (target_1/2/3 x x/y/speed/angle/distance/resolution).

Closes #89
@github-actions github-actions Bot added the bugfix Bug fix label Apr 30, 2026
@coderabbitai

coderabbitai Bot commented Apr 30, 2026 •

Copy link
Copy Markdown

Warning

Rate limit exceeded

@bharvey88 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 53 minutes and 1 second before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5c81a2e9-12a0-4823-bd65-c09ab59a1892

📥 Commits

Reviewing files that changed from the base of the PR and between 77498b0 and 12736fc.

📒 Files selected for processing (1)
  • Integrations/ESPHome/Core.yaml

Walkthrough

Adds filter lambdas to LD2450 target numeric sensors (target_1, target_2, target_3) to convert NaN values to 0.0 for proper state representation when no target is detected.

Changes

Cohort / File(s) Summary
LD2450 Target Sensor Filters
Integrations/ESPHome/Core.yaml
Added filters lambdas to all numeric sensors for targets 1–3 (x, y, speed, angle, distance, resolution) to replace incoming NaN values with 0.0.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Poem

🐰 When targets vanish without a trace,
NaN values haunt the sensor's face,
But filters bring a zero so bright,
Now unknown states fade from sight! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: filtering LD2450 NaN values to 0 when no target is detected, directly matching the PR's primary objective.
Linked Issues check ✅ Passed The PR directly addresses issue #89 by converting NaN to 0 for all 18 per-target numeric sensors, restoring the previous behavior where empty target slots report 0 instead of unknown.
Out of Scope Changes check ✅ Passed All changes are within scope: only lambda filters for NaN-to-0 conversion are added to the 18 per-target numeric sensors specified in the objective; text_sensors, binary sensors, and target/zone counts are appropriately left unchanged.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/ld2450-nan-when-no-target

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
Review rate limit: 0/1 reviews remaining, refill in 53 minutes and 1 second.

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@Integrations/ESPHome/Core.yaml`:
- Around line 326-397: The YAML inline lambda filters (e.g., the filters under
Target-1 X/ Y/ Speed/ Angle/ Distance/ Resolution and the corresponding filters
under target_2 and target_3) use a ternary with a colon which breaks YAML
parsing; replace each inline "- lambda: return isnan(x) ? 0.0 : x;" with a
block-scalar lambda (use "- lambda: |" followed by the lambda body on the next
indented line) for all 18 filter blocks so the colon inside the expression is
not interpreted as a mapping separator.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ff0419d6-5968-4f83-8e14-fa043d8f7443

📥 Commits

Reviewing files that changed from the base of the PR and between 34cb34d and 77498b0.

📒 Files selected for processing (1)
  • Integrations/ESPHome/Core.yaml

Comment thread Integrations/ESPHome/Core.yaml Outdated
YAML plain scalars cannot contain ': ' (colon followed by whitespace),
so the inline form 'return isnan(x) ? 0.0 : x;' fails to parse on the
second colon in the ternary. Switch all 18 per-target filters to the
block-scalar form (- lambda: |- ... ) which preserves the colon and
parses correctly.
@TrevorSchirmer
TrevorSchirmer merged commit c651431 into beta May 3, 2026
11 checks passed
@TrevorSchirmer
TrevorSchirmer deleted the fix/ld2450-nan-when-no-target branch May 3, 2026 16:19
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.

3 participants