16static SEN6XComponent::Sen6xType infer_type_from_product_name(
const char *product_name) {
21#ifdef SEN6X_STATUS_SENSOR_COUNT
23static uint8_t status_bit(
Sen6xStatus status, SEN6XComponent::Sen6xType
type) {
34 return type == SEN6XComponent::SEN66 ? 9 : 12;
44static constexpr uint8_t POLL_RETRIES = 24;
45static constexpr uint32_t I2C_READ_DELAY = 20;
46static constexpr uint32_t CMD_EXEC_DELAY = 20;
47static constexpr uint32_t POLL_INTERVAL = 50;
49static constexpr uint32_t TIMEOUT_POLL = 1;
50static constexpr uint32_t TIMEOUT_SETUP_STEP = 2;
51static constexpr uint32_t TIMEOUT_STARTUP = 3;
54static constexpr uint32_t START_MEASUREMENT_DELAY = 50;
55static constexpr uint32_t STOP_MEASUREMENT_DELAY = 1400;
56static constexpr uint32_t FAN_CLEANING_DELAY = 10000;
57static constexpr uint32_t SHT_HEATER_DELAY = 20000;
58static constexpr uint32_t CO2_CONDITIONING_DELAY = 24000;
60static constexpr float AMBIENT_PRESSURE_MIN_HPA = 700.0f;
61static constexpr float AMBIENT_PRESSURE_MAX_HPA = 1200.0f;
62static constexpr uint16_t SEN6X_CMD_GET_DATA_READY_STATUS = 0x0202;
63static constexpr uint16_t SEN6X_CMD_GET_FIRMWARE_VERSION = 0xD100;
64static constexpr uint16_t SEN6X_CMD_GET_PRODUCT_NAME = 0xD014;
65static constexpr uint16_t SEN6X_CMD_GET_SERIAL_NUMBER = 0xD033;
66static constexpr uint16_t SEN6X_CMD_READ_AND_CLEAR_DEVICE_STATUS = 0xD210;
68static constexpr uint16_t SEN6X_CMD_READ_MEASUREMENT = 0x0300;
69static constexpr uint16_t SEN6X_CMD_READ_MEASUREMENT_SEN62 = 0x04A3;
70static constexpr uint16_t SEN6X_CMD_READ_MEASUREMENT_SEN63C = 0x0471;
71static constexpr uint16_t SEN6X_CMD_READ_MEASUREMENT_SEN65 = 0x0446;
72static constexpr uint16_t SEN6X_CMD_READ_MEASUREMENT_SEN68 = 0x0467;
73static constexpr uint16_t SEN6X_CMD_READ_MEASUREMENT_SEN69C = 0x04B5;
75#ifdef USE_SEN6X_NUMBER_CONCENTRATION
76static constexpr uint16_t SEN6X_CMD_READ_NUMBER_CONCENTRATION = 0x0316;
78static constexpr uint16_t SEN6X_CMD_START_MEASUREMENTS = 0x0021;
79static constexpr uint16_t SEN6X_CMD_STOP_MEASUREMENTS = 0x0104;
80static constexpr uint16_t SEN6X_CMD_START_FAN_CLEANING = 0x5607;
81static constexpr uint16_t SEN6X_CMD_ACTIVATE_SHT_HEATER = 0x6765;
82static constexpr uint16_t SEN6X_CMD_RESET = 0xD304;
83static constexpr uint16_t SEN6X_CMD_VOC_ALGORITHM_TUNING = 0x60D0;
84static constexpr uint16_t SEN6X_CMD_NOX_ALGORITHM_TUNING = 0x60E1;
85static constexpr uint16_t SEN6X_CMD_TEMPERATURE_COMPENSATION = 0x60B2;
86static constexpr uint16_t SEN6X_CMD_TEMPERATURE_ACCELERATION = 0x6100;
87static constexpr uint16_t SEN6X_CMD_CO2_AUTOMATIC_SELF_CAL = 0x6711;
88static constexpr uint16_t SEN6X_CMD_AMBIENT_PRESSURE = 0x6720;
89static constexpr uint16_t SEN6X_CMD_SENSOR_ALTITUDE = 0x6736;
91static inline void set_read_command_and_words(SEN6XComponent::Sen6xType
type, uint16_t &read_cmd, uint8_t &read_words) {
92 read_cmd = SEN6X_CMD_READ_MEASUREMENT;
95 case SEN6XComponent::SEN62:
96 read_cmd = SEN6X_CMD_READ_MEASUREMENT_SEN62;
99 case SEN6XComponent::SEN63C:
100 read_cmd = SEN6X_CMD_READ_MEASUREMENT_SEN63C;
103 case SEN6XComponent::SEN65:
104 read_cmd = SEN6X_CMD_READ_MEASUREMENT_SEN65;
107 case SEN6XComponent::SEN66:
108 read_cmd = SEN6X_CMD_READ_MEASUREMENT;
111 case SEN6XComponent::SEN68:
112 read_cmd = SEN6X_CMD_READ_MEASUREMENT_SEN68;
115 case SEN6XComponent::SEN69C:
116 read_cmd = SEN6X_CMD_READ_MEASUREMENT_SEN69C;
125 ESP_LOGCONFIG(TAG,
"Setting up sen6x...");
131 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
139 uint16_t raw_serial_number[16];
140 if (!this->
get_register(SEN6X_CMD_GET_SERIAL_NUMBER, raw_serial_number, 16, 20)) {
141 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
153 uint16_t raw_product_name[17]{};
154 if (!this->
get_register(SEN6X_CMD_GET_PRODUCT_NAME, raw_product_name, 16, 20)) {
155 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
162 Sen6xType inferred_type = infer_type_from_product_name(product_name);
165 if (inferred_type == UNKNOWN) {
166 ESP_LOGE(TAG,
"Unknown product '%s'", product_name);
170 ESP_LOGD(TAG,
"Type inferred from product: %s", product_name);
171 }
else if (this->
sen6x_type_ != inferred_type && inferred_type != UNKNOWN) {
172 ESP_LOGW(TAG,
"Configured type (used) mismatches product '%s'", product_name);
174 ESP_LOGI(TAG,
"Product: %s", product_name);
181 if (this->voc_sensor_ && !has_voc_nox) {
182 ESP_LOGE(TAG,
"VOC requires SEN65, SEN66, SEN68, or SEN69C");
183 this->voc_sensor_ =
nullptr;
185 if (this->nox_sensor_ && !has_voc_nox) {
186 ESP_LOGE(TAG,
"NOx requires SEN65, SEN66, SEN68, or SEN69C");
187 this->nox_sensor_ =
nullptr;
189 if (this->co2_sensor_ && !has_co2) {
190 ESP_LOGE(TAG,
"CO2 requires SEN63C, SEN66, or SEN69C");
191 this->co2_sensor_ =
nullptr;
193 if (this->hcho_sensor_ && !has_hcho) {
194 ESP_LOGE(TAG,
"Formaldehyde requires SEN68 or SEN69C");
195 this->hcho_sensor_ =
nullptr;
197#ifdef SEN6X_STATUS_SENSOR_COUNT
205 ESP_LOGE(TAG,
"'%s' needs a variant with that feature", LOG_STR_ARG(entry.sensor->get_log_name()));
208 entry.bit = status_bit(entry.status, this->sen6x_type_);
209 this->status_sensors_[kept++] = entry;
211 this->status_sensors_.resize(kept);
216 uint16_t raw_firmware_version = 0;
217 if (!this->
get_register(SEN6X_CMD_GET_FIRMWARE_VERSION, raw_firmware_version, 20)) {
218 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
272 if (this->co2_sensor_ ==
nullptr || !this->
co2_asc_.has_value())
293 uint16_t params[4] = {
static_cast<uint16_t
>(compensation.
offset),
299 uint16_t params[4] = {acceleration.
k, acceleration.
p, acceleration.
t1, acceleration.
t2};
309 ESP_LOGE(TAG,
"Write 0x%04X failed, error %d", SEN6X_CMD_START_MEASUREMENTS, this->
last_error_);
316 ESP_LOGD(TAG,
"Initialized");
323 ESP_LOGE(TAG,
"Write 0x%04X failed, error %d", i2c_command, this->
last_error_);
341void SEN6XComponent::dump_config() {
348 " Startup delay: %" PRIu32
" ms",
351 LOG_UPDATE_INTERVAL(
this);
354 ESP_LOGCONFIG(TAG,
" Temperature compensation: offset=%.2f slope=%.4f time_constant=%us", comp.offset / 200.0f,
355 comp.normalized_offset_slope / 10000.0f, comp.time_constant);
359 ESP_LOGCONFIG(TAG,
" Temperature acceleration: K=%.1f P=%.1f T1=%.1f T2=%.1f", accel.k / 10.0f, accel.p / 10.0f,
360 accel.t1 / 10.0f, accel.t2 / 10.0f);
363 if (this->co2_sensor_ !=
nullptr) {
365 ESP_LOGCONFIG(TAG,
" CO2 automatic self-calibration: %s", ONOFF(this->
co2_asc_.value()));
371 ESP_LOGCONFIG(TAG,
" Ambient pressure compensation: %u hPa", this->
ambient_pressure_);
373 if (this->ambient_pressure_source_sensor_ !=
nullptr) {
374 ESP_LOGCONFIG(TAG,
" Ambient pressure compensation source: %s",
375 LOG_STR_ARG(this->ambient_pressure_source_sensor_->get_log_name()));
378 LOG_SENSOR(
" ",
"PM 1.0", this->pm_1_0_sensor_);
379 LOG_SENSOR(
" ",
"PM 2.5", this->pm_2_5_sensor_);
380 LOG_SENSOR(
" ",
"PM 4.0", this->pm_4_0_sensor_);
381 LOG_SENSOR(
" ",
"PM 10.0", this->pm_10_0_sensor_);
382#ifdef USE_SEN6X_NUMBER_CONCENTRATION
383 LOG_SENSOR(
" ",
"PMC 0.5", this->pmc_0_5_sensor_);
384 LOG_SENSOR(
" ",
"PMC 1.0", this->pmc_1_0_sensor_);
385 LOG_SENSOR(
" ",
"PMC 2.5", this->pmc_2_5_sensor_);
386 LOG_SENSOR(
" ",
"PMC 4.0", this->pmc_4_0_sensor_);
387 LOG_SENSOR(
" ",
"PMC 10.0", this->pmc_10_0_sensor_);
389 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
390 LOG_SENSOR(
" ",
"Humidity", this->humidity_sensor_);
391 LOG_SENSOR(
" ",
"VOC", this->voc_sensor_);
392 LOG_SENSOR(
" ",
"NOx", this->nox_sensor_);
393 LOG_SENSOR(
" ",
"HCHO", this->hcho_sensor_);
394 LOG_SENSOR(
" ",
"CO2", this->co2_sensor_);
395#ifdef SEN6X_STATUS_SENSOR_COUNT
396 for (
const auto &entry : this->status_sensors_) {
397 LOG_BINARY_SENSOR(
" ",
"Device status", entry.sensor);
402void SEN6XComponent::update() {
410 this->
set_timeout(TIMEOUT_POLL, wait, [
this]() { this->update(); });
414 if (this->ambient_pressure_source_sensor_ !=
nullptr && this->co2_sensor_ !=
nullptr) {
454 ESP_LOGD(TAG,
"Data not ready");
462 ESP_LOGV(TAG,
"Data ready polling attempt %u",
468 ESP_LOGD(TAG,
"write data ready status error (%d)", this->
last_error_);
472 this->
set_timeout(TIMEOUT_POLL, I2C_READ_DELAY, [
this]() {
473 uint16_t raw_read_status;
474 if (!this->
read_data(&raw_read_status, 1)) {
476 ESP_LOGD(TAG,
"read data ready status error (%d)", this->
last_error_);
480 if ((raw_read_status & 0x0001) == 0) {
493 ESP_LOGD(TAG,
"Read measurement failed (%d)", this->
last_error_);
501 uint16_t measurements[10];
505 ESP_LOGD(TAG,
"Read data failed (%d)", this->
last_error_);
508 int8_t voc_index = -1;
509 int8_t nox_index = -1;
510 int8_t hcho_index = -1;
511 int8_t co2_index = -1;
512 bool co2_uint16 =
false;
544 float pm_1_0 = measurements[0] / 10.0f;
545 if (measurements[0] == 0xFFFF)
547 float pm_2_5 = measurements[1] / 10.0f;
548 if (measurements[1] == 0xFFFF)
550 float pm_4_0 = measurements[2] / 10.0f;
551 if (measurements[2] == 0xFFFF)
553 float pm_10_0 = measurements[3] / 10.0f;
554 if (measurements[3] == 0xFFFF)
556 float humidity =
static_cast<int16_t
>(measurements[4]) / 100.0f;
557 if (measurements[4] == 0x7FFF)
559 float temperature =
static_cast<int16_t
>(measurements[5]) / 200.0f;
560 if (measurements[5] == 0x7FFF)
568 if (voc_index >= 0) {
569 voc =
static_cast<int16_t
>(measurements[voc_index]) / 10.0f;
570 if (measurements[voc_index] == 0x7FFF)
573 if (nox_index >= 0) {
574 nox =
static_cast<int16_t
>(measurements[nox_index]) / 10.0f;
575 if (measurements[nox_index] == 0x7FFF)
579 if (hcho_index >= 0) {
580 const uint16_t hcho_raw = measurements[hcho_index];
581 hcho = hcho_raw / 10.0f;
582 if (hcho_raw == 0xFFFF)
586 if (co2_index >= 0) {
588 const uint16_t co2_raw = measurements[co2_index];
589 co2 =
static_cast<float>(co2_raw);
590 if (co2_raw == 0xFFFF)
593 const int16_t co2_raw =
static_cast<int16_t
>(measurements[co2_index]);
594 co2 =
static_cast<float>(co2_raw);
595 if (co2_raw == 0x7FFF)
601 ESP_LOGD(TAG,
"Startup delay, ignoring values");
606 PUBLISH_STATE(this->pm_1_0_sensor_, pm_1_0);
607 PUBLISH_STATE(this->pm_2_5_sensor_, pm_2_5);
608 PUBLISH_STATE(this->pm_4_0_sensor_, pm_4_0);
609 PUBLISH_STATE(this->pm_10_0_sensor_, pm_10_0);
610 PUBLISH_STATE(this->temperature_sensor_,
temperature);
611 PUBLISH_STATE(this->humidity_sensor_, humidity);
612 PUBLISH_STATE(this->voc_sensor_, voc);
613 PUBLISH_STATE(this->nox_sensor_, nox);
614 PUBLISH_STATE(this->hcho_sensor_, hcho);
615 PUBLISH_STATE(this->co2_sensor_, co2);
617#ifdef USE_SEN6X_NUMBER_CONCENTRATION
631#ifdef SEN6X_STATUS_SENSOR_COUNT
632 if (!this->status_sensors_.empty()) {
638 if (this->
write_command(SEN6X_CMD_READ_AND_CLEAR_DEVICE_STATUS)) {
642 ESP_LOGD(TAG,
"Read device status failed (%d)", this->
last_error_);
648#ifdef SEN6X_STATUS_SENSOR_COUNT
653 for (
const auto &entry : this->status_sensors_)
654 entry.sensor->publish_state(((
status >> entry.bit) & 1) != 0);
656 ESP_LOGD(TAG,
"Read device status failed (%d)", this->
last_error_);
662#ifdef USE_SEN6X_NUMBER_CONCENTRATION
664 return this->pmc_0_5_sensor_ !=
nullptr || this->pmc_1_0_sensor_ !=
nullptr || this->pmc_2_5_sensor_ !=
nullptr ||
665 this->pmc_4_0_sensor_ !=
nullptr || this->pmc_10_0_sensor_ !=
nullptr;
669 if (!this->
write_command(SEN6X_CMD_READ_NUMBER_CONCENTRATION)) {
671 ESP_LOGD(TAG,
"Read number concentration failed (%d)", this->
last_error_);
679 sensor::Sensor *sensors[] = {this->pmc_0_5_sensor_, this->pmc_1_0_sensor_, this->pmc_2_5_sensor_,
680 this->pmc_4_0_sensor_, this->pmc_10_0_sensor_};
681 uint16_t measurements[std::size(sensors)];
684 ESP_LOGD(TAG,
"Read data failed (%d)", this->
last_error_);
688 for (
size_t i = 0; i < std::size(sensors); i++) {
689 PUBLISH_STATE(sensors[i], measurements[i] == 0xFFFF ? NAN : measurements[i] / 10.0f);
700 if (std::isnan(pressure_hpa))
703 if (pressure_hpa < AMBIENT_PRESSURE_MIN_HPA || pressure_hpa > AMBIENT_PRESSURE_MAX_HPA) {
705 ESP_LOGW(TAG,
"Ambient pressure out of range: %.0f hPa", pressure_hpa);
711 const auto value =
static_cast<uint16_t
>(lroundf(pressure_hpa));
735 this->
co2_wait_ = CO2_CONDITIONING_DELAY;
742void SEN6XComponent::start_measurement() {
747 ESP_LOGW(TAG,
"Device busy");
752 ESP_LOGW(TAG,
"Start measurement failed (%d)", this->
last_error_);
758void SEN6XComponent::stop_measurement() {
762 ESP_LOGW(TAG,
"Device busy");
769 ESP_LOGW(TAG,
"Stop measurement failed (%d)", this->
last_error_);
776void SEN6XComponent::start_fan_cleaning() {
781 ESP_LOGW(TAG,
"Fan cleaning requires idle mode; stop the measurement first");
785 ESP_LOGW(TAG,
"Device busy");
790 ESP_LOGW(TAG,
"Fan cleaning failed (%d)", this->
last_error_);
796void SEN6XComponent::activate_sht_heater() {
801 ESP_LOGW(TAG,
"SHT heater requires idle mode; stop the measurement first");
805 ESP_LOGW(TAG,
"Device busy");
810 ESP_LOGW(TAG,
"SHT heater failed (%d)", this->
last_error_);