From 0ce94826e5484edfb3826a01dbfdf640aeadf313 Mon Sep 17 00:00:00 2001 From: Long Vo Date: Thu, 14 Nov 2024 16:17:30 +0700 Subject: [PATCH] MQTT handling of nan values to align with Hass --- esphome/components/ld2410/ld2410.cpp | 6 +++--- esphome/components/mqtt/mqtt_sensor.cpp | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/esphome/components/ld2410/ld2410.cpp b/esphome/components/ld2410/ld2410.cpp index 9a7fc9ade7..c3b57815d6 100644 --- a/esphome/components/ld2410/ld2410.cpp +++ b/esphome/components/ld2410/ld2410.cpp @@ -255,16 +255,16 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) { } else { for (auto *s : this->gate_move_sensors_) { if (s != nullptr && !std::isnan(s->get_state())) { - s->publish_state(0); + s->publish_state(NAN); } } for (auto *s : this->gate_still_sensors_) { if (s != nullptr && !std::isnan(s->get_state())) { - s->publish_state(0); + s->publish_state(NAN); } } if (this->light_sensor_ != nullptr && !std::isnan(this->light_sensor_->get_state())) { - this->light_sensor_->publish_state(0); + this->light_sensor_->publish_state(NAN); } } #endif diff --git a/esphome/components/mqtt/mqtt_sensor.cpp b/esphome/components/mqtt/mqtt_sensor.cpp index fff75a3c00..2586e1ebc3 100644 --- a/esphome/components/mqtt/mqtt_sensor.cpp +++ b/esphome/components/mqtt/mqtt_sensor.cpp @@ -69,7 +69,9 @@ bool MQTTSensorComponent::send_initial_state() { } } bool MQTTSensorComponent::publish_state(float value) { - int8_t accuracy = this->sensor_->get_accuracy_decimals(); + int8_t accuracy = this->sensor_->get_accuracy_decimals(); + if(std::isnan(value)) + return this->publish(this->get_state_topic_(), "None"); return this->publish(this->get_state_topic_(), value_accuracy_to_string(value, accuracy)); } std::string MQTTSensorComponent::unique_id() { return this->sensor_->unique_id(); }