Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sgp40 fix #2462

Merged
merged 5 commits into from Oct 10, 2021
Merged

Sgp40 fix #2462

merged 5 commits into from Oct 10, 2021

Conversation

natelust
Copy link
Contributor

@natelust natelust commented Oct 7, 2021

What does this implement/fix?

The spg40 air quality sensor was incorrectly using the manufacturers algorithm. The documentation here indicates that to get a correct VOC index, this sensor must be sampled and supplied to their algorithm at 1Hz (With discussion here suggesting tolerances should allow slight differences that would make software timing loops sufficient).

Previously this sensor (and related value) were only sampled at the update interval, which would lead to an incorrect VOC index of unknown magnitude. This change introduces a separate timer for reading the sensor and updating the VOC index at the correct cadence. The update interval is then used only to publish this state to other components or the front end, saving power with the wifi module, or limiting the number of samples supplied to the front end. To allow the old behavior a new config parameter is introduced, optimal_sampling, which can be set to false.

In a separate commit, a fix is made to the dump_config method, adding missing config entries.

This is a breaking change not in the sense that a user needs to make a change to benefit from the fix. However, since the sensor will be read out at the correct interval, it may cause more wake-ups or power consumption on a device when a user is not expecting it.

Quick description and explanation of changes

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)
  • Other

Related issue or feature (if applicable): fixes

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

Test Environment

  • ESP32
  • [x ] ESP8266

Example entry for config.yaml:

New config parameter defaults to new correct behavior. To restore the old
incorrect behavior the config would look like:

sensor:
  - platform: sgp40
    name: "Air Quality"
    optimal_sampling: false
# Example config.yaml

Checklist:

  • [x ] 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 spg40 sensor must be sampled at 1Hz for the VOC index algorithm
to work correctly. This commit introduces a on device timer to
sample correctly seperately from updating the public state of the
component.
The SGP40 component was not printing all of it's configuration in
dump_config, add in the missing store_baseline value.
@project-bot project-bot bot added this to Needs Review in ESPHome Dev Oct 7, 2021
@probot-esphome
Copy link

probot-esphome bot commented Oct 7, 2021

Hey there @SenexCrenshaw, mind taking a look at this pull request as it has been labeled with an integration (sgp40) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

*/
if (this->optimal_sampling_) {
ESP_LOGD(TAG, "Using optimal sampling, setting up background sampler");
App.scheduler.set_interval(this, "", 1000, [this](){this->update_voc_index();});
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
App.scheduler.set_interval(this, "", 1000, [this](){this->update_voc_index();});
this->set_interval(1000, [this](){this->update_voc_index();});

Component already has this method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh right, sorry, I'm still trying to load this code base into my mental cache


At configuration the component can be configured to turn off optimal sampling in which
case the sensor will be read out with the same cadence as the update method, leading to
likely inacurate measurements, but possibly accurate enough for certain use cases.
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need this? I mean if you have to update close to 1Hz anyway, why should we even expose this option?

I'd say we remove that option, and if someone does come up with a real use-case we can always add it back

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't really know what your projects stance on backwards compatibility was, and I wanted to provide a way for people who may be running this on a battery or have somehow coded a flow around the incorrect behavior to retain the existing behavior. If that is not a priority I am happy to change it and the accompanying documentation.

Copy link
Member

Choose a reason for hiding this comment

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

Ah sure no problem

We try to not have breaking changes, though I think in this case the previous behavior would have been broken anyway unless it's polled at 1Hz, no? In that case it wouldn't really be a breaking change and ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, how it currently is the VOC measurement is just not an accurate reflection of reality, it may happen to be close enough for some, but will not behave correctly overtime.

Thanks for the heads up on the way the project approaches things. I am pretty new to ESPHome, and it's pretty amazing. I have a few other things I hope to contribute, so I hope to make things easier on you all reviewing things in future PRs.

Copy link
Member

@OttoWinter OttoWinter left a comment

Choose a reason for hiding this comment

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

Thanks!

Just some smaller things re config option, and also see the lint checks on the PR - a bit of code needs to be reformatted

@natelust
Copy link
Contributor Author

natelust commented Oct 9, 2021

Sorry about the formatting, I'll get that fixed up, and shoot something back to you. I'm not sure about the eradicate on commits of your project. Would you like the changes re-based away, or on a separate commit to maintain the github history?

@OttoWinter
Copy link
Member

Sorry about the formatting, I'll get that fixed up, and shoot something back to you. I'm not sure about the eradicate on commits of your project. Would you like the changes re-based away, or on a separate commit to maintain the github history?

Only necessary when there are merge conflicts. We'll squash merge the commit anyways, so just adding more commits to the branch is fine

@natelust
Copy link
Contributor Author

natelust commented Oct 9, 2021

I think I have addressed your concerns and cleaned up the formatting, whenever you want to take another look. I am happy to do any additional work you may request. Thank you.

Copy link
Member

@OttoWinter OttoWinter left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

ESPHome Dev automation moved this from Needs Review to Reviewer Approved Oct 10, 2021
@OttoWinter OttoWinter merged commit 92b85f9 into esphome:dev Oct 10, 2021
ESPHome Dev automation moved this from Reviewer Approved to Done Oct 10, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Oct 12, 2021
ESPHome Dev automation moved this from Done to Reviewer Approved Sep 23, 2023
ESPHome Dev automation moved this from Reviewer Approved to Done Sep 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants