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
sen5x: add TPS & PM number concentration #6694
base: dev
Are you sure you want to change the base?
sen5x: add TPS & PM number concentration #6694
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## dev #6694 +/- ##
==========================================
+ Coverage 53.70% 54.25% +0.54%
==========================================
Files 50 50
Lines 9408 9587 +179
Branches 1654 1689 +35
==========================================
+ Hits 5053 5201 +148
- Misses 4056 4062 +6
- Partials 299 324 +25 ☔ View full report in Codecov by Sentry. |
This change adds a new command, listed as being available in FW >0.7 New entities can be configured: - pm_n_x_x for PM number concentration in #/cm3 - tps for typical particle size
700ceba
to
8fc28a5
Compare
Hey there @martgras, mind taking a look at this pull request as it has been labeled with an integration ( |
esphome/components/sen5x/sen5x.cpp
Outdated
if (this->get_pm_number_concentration_and_tps_) { | ||
// delay the extra reading to allow update_measured_values to complete. | ||
this->set_timeout(30, [this]() { this->update_measured_pm_(); }); | ||
} | ||
} |
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.
There is no guarantee that this will be enough time. Ideally the component would be changes to have a small state machine and run through the stage one at a time in loop
once triggered by the update
function.
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.
That's very true. Wouldn't a loop in the update function block the main event loop though?
Another approach I was testing was to perform only one reading (either pm with rh/t/nox/vox or full pm) per update.
If get_pm_number_concentration_and_tps_
was true, we would need to halve the user specified update_interval
in the setup with set_update_interval
and while it was reporting the correct update interval when dumping the config, the sensor was still exporting entities with the original period (i.e. halving it didn't affect the actual period between update
calls).
I brainstormed a little and came up with another solution:
- use an FSM with 3 states: idle, update_measured_values_progress, update_measured_values_done
- set the idle, update_measured_values_progress and update_measured_values_done accordingly
- create a small helper function to determine the current state of the FSM. This would be responsible for caling itself with a
this->set_timeout(10)
if the state isn'tupdate_measured_values_done
- Finally, trigger
update_measured_pm_()
function (from the helper function) when the FSM is inupdate_measured_values_done
state and reset the FSM toidle
inupdate_measured_pm_()
This would ensure update_measured_values_()
has had enough time to complete without blocking the main event loop with a loop. What are your thoughts on this?
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.
No, in the loop state machine you can check the current time against the start time of the last state change and when enough time has passed, you read the data and publish. No delays and timeouts are needed.
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.
@jesserockz thank you. I misunderstood what you said earlier (and also didn't know loop
was still triggered for PollingComponent
)
I went ahead and implemented the brainstormed solution, because it's less code and less complicated than a full blown FSM for all the tasks to perform. If that's not an acceptable solution, let me know.
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
8e34154
to
c075183
Compare
What does this implement/fix?
This change adds a new command, listed as being available in FW >0.7
New entities can be configured:
The implementation is based on sensirion's own Embedded driver, available here.
This command returns the Mass Concentration, the Number Concentration and Typical Particle Size.
Since we already get the Mass Concentration from the
SEN5X_CMD_READ_MEASUREMENT
command, the new command will not update the Mass Concentration, to prevent doubling the export frequency of those entities.Types of changes
Related issue or feature (if applicable): fixes
Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#
Test Environment
Example entry for
config.yaml
:Checklist:
tests/
folder).If user exposed functionality or configuration variables are added/changed: