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
Sgp40 fix #2462
Conversation
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.
Hey there @SenexCrenshaw, mind taking a look at this pull request as it has been labeled with an integration ( |
esphome/components/sgp40/sgp40.cpp
Outdated
*/ | ||
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();}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
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
esphome/components/sgp40/sgp40.cpp
Outdated
|
||
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. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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
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 |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
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
Related issue or feature (if applicable): fixes
Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#1515
Test Environment
Example entry for
config.yaml
:New config parameter defaults to new correct behavior. To restore the old
incorrect behavior the config would look like:
# Example config.yaml
Checklist:
tests/
folder).If user exposed functionality or configuration variables are added/changed: