Compare commits

...
Author SHA1 Message Date
J. Nick Koston b18b5e5697 Merge remote-tracking branch 'origin/dev' into HEAD 2026-11-14 08:38:15 -06:00
J. Nick Koston d7b54b20ff [sgp30] Attribute the Buck ice formula to Buck (1996) 2026-10-30 06:42:25 -10:00
J. Nick Koston e52069c1d8 [sgp30] Use the Buck ice coefficient below 0 °C
The ice branch of the humidity compensation used the water coefficient
0.061121; Buck (1981) over ice is 6.1115 hPa, so 0.061115. Move the
calculation into an inline function so a unit test can reach it.
2026-10-30 01:18:29 -10:00
4 changed files with 39 additions and 12 deletions

No files matched your search

+1 -12
View File
@@ -162,18 +162,7 @@ void SGP30Component::send_env_data_() {
ESP_LOGD(TAG, "External compensation data received: Temperature %0.2f°C", temperature);
}
float absolute_humidity;
if (temperature < 0) {
absolute_humidity =
216.67f *
((humidity * 0.061121f * std::exp((23.036f - temperature / 333.7f) * (temperature / (279.82f + temperature)))) /
(273.15f + temperature));
} else {
absolute_humidity =
216.67f *
((humidity * 0.061121f * std::exp((18.678f - temperature / 234.5f) * (temperature / (257.14f + temperature)))) /
(273.15f + temperature));
}
const float absolute_humidity = compensation_absolute_humidity(temperature, humidity);
uint8_t data[4] = {
SGP30_CMD_SET_ABSOLUTE_HUMIDITY & 0xFF,
uint8_t(std::floor(absolute_humidity)), // humidity_full
+19
View File
@@ -15,6 +15,25 @@ struct SGP30Baselines {
uint16_t tvoc;
} PACKED;
/// Absolute humidity in g/m³ from temperature in °C and relative humidity in %, for the compensation command.
/// Buck (1996): over ice below 0 °C, over water above: https://en.wikipedia.org/wiki/Arden_Buck_equation
inline float compensation_absolute_humidity(float temperature, float humidity) {
float a, b, c, d;
if (temperature < 0) {
a = 0.061115f;
b = 23.036f;
c = 333.7f;
d = 279.82f;
} else {
a = 0.061121f;
b = 18.678f;
c = 234.5f;
d = 257.14f;
}
return 216.67f * ((humidity * a * std::exp((b - temperature / c) * (temperature / (d + temperature)))) /
(273.15f + temperature));
}
/// This class implements support for the Sensirion SGP30 i2c GAS (VOC and CO2eq) sensors.
class SGP30Component final : public PollingComponent, public sensirion_common::SensirionI2CDevice {
SUB_SENSOR(eco2)
@@ -0,0 +1 @@
"""Builds the sgp30 sensor platform into the C++ unit tests."""
+18
View File
@@ -0,0 +1,18 @@
#include <gtest/gtest.h>
#include "esphome/components/sgp30/sgp30.h"
namespace esphome::sgp30::testing {
// Reference values from the Buck equations in double precision; the ice and water
// coefficients differ by 1e-4, so the tolerance must stay well below that
TEST(SGP30CompensationHumidity, UsesTheIceCoefficientBelowZero) {
EXPECT_NEAR(compensation_absolute_humidity(-10.0f, 50.0f), 1.0701634f, 0.00002f);
EXPECT_NEAR(compensation_absolute_humidity(-30.0f, 50.0f), 0.1694181f, 0.000004f);
}
TEST(SGP30CompensationHumidity, UsesTheWaterCoefficientAboveZero) {
EXPECT_NEAR(compensation_absolute_humidity(20.0f, 50.0f), 8.6414485f, 0.0002f);
}
} // namespace esphome::sgp30::testing