esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
290 stars 36 forks source link

SCD30 sampling interval not controlled through 'update_interval' #2410

Closed geoffrey-vl closed 3 years ago

geoffrey-vl commented 3 years ago

The problem

After having completed some test with SCD30 on ESP32 I started to dig into more adventurous use cases such as lowering the power usage. I wanted to use the SCD30's RDY pin, but after monitoring that pin I noticed it was not aligned with the update_interval I've set in ESPHome.

At this point I started looking into the ESPHome code and noticed the update_inverval is more or less an ordinary software timer in ESPHome that polls the sensors frequently. When the timer expires, it triggers the update() function, which (when SCD30's data is available) will read out sensor data and transmit that to HomeAssistant. However, the SCD30 also has the ability to set its own sampling interval. In fact, since I first started using the SCD30 on other platforms, I was expecting that the ESPHome update_interval was actually controlling the SCD30's sampling interval. But after going through the code it seems it is not. From the source code:

/// Commands for future use
static const uint16_t SCD30_CMD_STOP_MEASUREMENTS = 0x0104;
static const uint16_t SCD30_CMD_MEASUREMENT_INTERVAL = 0x4600;
...

The problem is that ESPHome's sampling rate and the SCD30's sampling rate are not synchronized. Therefor the sensor may as well by samping each 30 seconds, while ESPHome checks each second and notice there is no data available. That would be kind of misleading the user. Also, adjusting the SCD30's sampling interval should also allows control over the average power consumption of the sensor (and thereby your ESPHome device). See design note Low Power Mode for SCD30. In the end, having the sampling intervals aligned would also allow to trustfully use the SCD's RDY pin.

Now I'm wondering if the current source code is due to "greater design" considerations? If so, it is indeed not that easy to correctly sync both interval. As I'm thinking now it would be better if the ESPHome update_inverval should no longer be configurable but still used internally and would be fixed to a constant 1 second rate. Next the SCD30 sensor platform should receive a new setting that sets the sampling rate. Or, if you don't want to change any of the outside configurations for SCD30 sensors in ESPHome, we could keep the update_inverval setting but have it used for controlling the SCD30's sampling rate.

I'm not sure if all of that makes complete sense and fits ESPHome design considerations. I'm also not sure if this might be a bug somehow. But anyway what are your thoughts?

Which version of ESPHome has the issue?

1.19.3 - 2021.9

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

2021.4.4

What platform are you using?

ESP32

Board

ESP32devkit

Component causing the issue

SCD30

Example YAML snippet

i2c:
  sda: 21
  scl: 22
  scan: True
  id: bus_a

sensor:    
  # CO2 sensor
  - platform: scd30
    co2:
      name: "Slaapkamer CO2"
      accuracy_decimals: 1
    temperature:
      name: "Slaapkamer Temperature"
      accuracy_decimals: 2
    humidity:
      name: "Slaapkamer Humidity"
      accuracy_decimals: 1
    address: 0x61
    i2c_id: bus_a
    update_interval: 120s
    temperature_offset: 1.5 °C

Anything in the logs that might be useful for us?

No response

Additional information

No response

oxan commented 3 years ago

I would say the best approach is to convert the component from a PollingComponent to a regular Component, but manually add the update_interval setting. That way the software timer won't run. Then, the SCD30 sampling interval can be configured to the specified update interval, and the component will just read all available samples and report them.

This way it's both compatible with older configs, and the low power mode can be used.