Skip to content

Conversation

@gapple
Copy link

@gapple gapple commented Oct 10, 2025

What does this implement/fix?

Add a filter to round sensor values to a configured number of significant digits.

Useful for sensors like ambient light which use variable measurement interval and gain to cover a wide value range (0.0072 to 120000 lx), and the current round or round_to_multiple filters aren't effective.

Original Value round_to_sigfig: 2 round_to_sigfig: 4
123456.789 120000 123500
123.4567 120 123.5
12.3456 12 12.35
1.23456 1.2 1.235
0.123456 0.12 0.1235

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)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

  • fixes

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

Test Environment

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

Example entry for config.yaml:

sensor:
  - platform: veml7700
    address: 0x10
    accuracy_decimals: 0
    ambient_light: 
      name: "Ambient light"
      filters:
        - sliding_window_moving_average:
            window_size: 5
            send_every: 5
        - round_to_sigfig: 2

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:

@gapple gapple requested a review from a team as a code owner October 10, 2025 05:14
@github-actions
Copy link
Contributor

github-actions bot commented Oct 10, 2025

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#11157
    components: [const, sensor]
    refresh: 1h

(Added by the PR bot)

@codecov-commenter
Copy link

codecov-commenter commented Oct 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.71%. Comparing base (c38c2a1) to head (e5c19b7).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #11157      +/-   ##
==========================================
- Coverage   71.74%   71.71%   -0.03%     
==========================================
  Files          53       53              
  Lines       11031    11031              
  Branches     1482     1482              
==========================================
- Hits         7914     7911       -3     
- Misses       2719     2721       +2     
- Partials      398      399       +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.

@gapple gapple requested a review from clydebarrow as a code owner October 10, 2025 20:09
@github-actions
Copy link
Contributor

👋 Hi there! I've automatically requested reviews from codeowners based on the files changed in this PR.

@clydebarrow - You've been requested to review this PR as codeowner(s) of 5 file(s) that were modified. Thanks for your time! 🙏

@clydebarrow
Copy link
Contributor

  1. You would need docs and tests. Best mark as draft until those are done.
  2. In the absence of docs, it's not obvious what this does and why the existing round filter doesn't suffice. Some examples would help.

Comment on lines +826 to +839
"round_to_sigfig",
RoundSigfigFilter,
cv.maybe_simple_value(
{
cv.Required(CONF_DIGITS): cv.positive_not_null_int,
},
key=CONF_DIGITS,
),
)
async def round_sigfig_filter_to_code(config, filter_id):
return cg.new_Pvariable(
filter_id,
config[CONF_DIGITS],
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure this is the best name for the filter - "fig" doesn't gel with the "digits" used in the config, and generally speaking abbreviating words in config options is not ideal. Maybe "maximum_significant_digits"? It's wordy, but clear, and eliminates the need to have "digits" as a sub-key.

Suggested change
"round_to_sigfig",
RoundSigfigFilter,
cv.maybe_simple_value(
{
cv.Required(CONF_DIGITS): cv.positive_not_null_int,
},
key=CONF_DIGITS,
),
)
async def round_sigfig_filter_to_code(config, filter_id):
return cg.new_Pvariable(
filter_id,
config[CONF_DIGITS],
)
"maximum_significant_digits",
RoundSigfigFilter,
cv.positive_not_null_int,
)
async def round_sigfig_filter_to_code(config, filter_id):
return cg.new_Pvariable(
filter_id,
config,
)

@esphome esphome bot marked this pull request as draft October 16, 2025 10:51
@esphome
Copy link

esphome bot commented Oct 16, 2025

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants