Skip to content

[sensor] Fix delta filter percentage mode regression#14302

Merged
swoboda1337 merged 1 commit intoesphome:devfrom
swoboda1337:fix-delta-percentage
Feb 26, 2026
Merged

[sensor] Fix delta filter percentage mode regression#14302
swoboda1337 merged 1 commit intoesphome:devfrom
swoboda1337:fix-delta-percentage

Conversation

@swoboda1337
Copy link
Member

@swoboda1337 swoboda1337 commented Feb 25, 2026

What does this implement/fix?

Fixes a regression introduced in #12605 where the _get_delta() helper does not divide percentage values by 100 when converting to the linear coefficient used by DeltaFilter.

The old code used cv.percentage() which correctly converts "5%"0.05. The new code replaced this with manual string parsing (float(value[:-1])) but forgot the division, so "5%"5.0. This means delta: 5% actually requires a 500% change instead of 5%, effectively filtering out all values after the first one.

The fix: divide by 100 in _get_delta():

# Before (broken):
return 0.0, float(value[:-1])        # "5%" → 5.0 (500%)

# After (fixed):
return 0.0, float(value[:-1]) / 100.0  # "5%" → 0.05 (5%)

An integration test for percentage mode is added to catch this regression.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Developer breaking change (an API change that could break external components)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

Pull request in esphome-docs with documentation (if applicable):

  • N/A

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • LN882x
  • nRF52840

Example entry for config.yaml:

sensor:
  - platform: adc
    pin: GPIO36
    name: "ADC Voltage"
    filters:
      - delta: 5%

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

The _get_delta() helper introduced in esphome#12605 converts percentage strings
like "5%" to float but doesn't divide by 100. This means delta: 5%
actually requires a 500% change instead of 5%, effectively filtering
out all values after the first one.

Fixes esphome#14301

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@swoboda1337 swoboda1337 requested a review from a team as a code owner February 25, 2026 23:21
Copilot AI review requested due to automatic review settings February 25, 2026 23:21
@github-actions
Copy link
Contributor

👋 Hi there! This PR modifies 1 file(s) with codeowners.

@esphome/core - As codeowner(s) of the affected files, your review would be appreciated! 🙏

Note: Automatic review request may have failed, but you're still welcome to review.

@github-actions
Copy link
Contributor

To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file:

external_components:
  - source: github://pr#14302
    components: [sensor]
    refresh: 1h

(Added by the PR bot)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a critical regression introduced in PR #12605 where percentage-based delta filters (e.g., delta: 5%) were incorrectly converted to their linear coefficients. The bug caused the filter to require a 500% change instead of 5%, effectively filtering out all values after the first one. The fix adds the missing division by 100 when converting percentage strings to decimal coefficients.

Changes:

  • Fixed _get_delta() helper in esphome/components/sensor/__init__.py to divide percentage values by 100
  • Added integration test for percentage mode delta filter to prevent future regressions

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
esphome/components/sensor/init.py Fixed percentage conversion in _get_delta() by adding division by 100
tests/integration/test_sensor_filters_delta.py Added Test 5 for percentage mode delta filter with proper assertions
tests/integration/fixtures/sensor_filters_delta.yaml Added source sensor 5, filter_percentage sensor, test script, and button for percentage mode testing

@codecov-commenter
Copy link

codecov-commenter commented Feb 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.25%. Comparing base (534857d) to head (b3a8ab4).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #14302      +/-   ##
==========================================
- Coverage   74.28%   74.25%   -0.03%     
==========================================
  Files          55       55              
  Lines       11595    11595              
  Branches     1583     1583              
==========================================
- Hits         8613     8610       -3     
- Misses       2578     2580       +2     
- Partials      404      405       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Contributor

Memory Impact Analysis

Components: sensor
Platform: esp8266-ard

Metric Target Branch This PR Change
RAM 29,344 bytes 29,344 bytes ➡️ +0 bytes (0.00%)
Flash 289,323 bytes 289,323 bytes ➡️ +0 bytes (0.00%)

Note: This analysis measures static RAM and Flash usage only (compile-time allocation).
Dynamic memory (heap) cannot be measured automatically.
⚠️ You must test this PR on a real device to measure free heap and ensure no runtime memory issues.

This analysis runs automatically when components change. Memory usage is measured from a representative test configuration.

@swoboda1337 swoboda1337 merged commit 0d5b7df into esphome:dev Feb 26, 2026
51 checks passed
@swoboda1337
Copy link
Member Author

Thanks

jesserockz pushed a commit that referenced this pull request Feb 26, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jesserockz jesserockz mentioned this pull request Feb 26, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Feb 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Delta" sensor filter stopped working starting from ESPHome 2026.2.0

5 participants