MQTT handling of nan values to align with Hass

This commit is contained in:
Long Vo 2024-11-14 16:17:30 +07:00
parent 6ff866cf5b
commit 0ce94826e5
2 changed files with 6 additions and 4 deletions

View file

@ -255,16 +255,16 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) {
} else { } else {
for (auto *s : this->gate_move_sensors_) { for (auto *s : this->gate_move_sensors_) {
if (s != nullptr && !std::isnan(s->get_state())) { if (s != nullptr && !std::isnan(s->get_state())) {
s->publish_state(0); s->publish_state(NAN);
} }
} }
for (auto *s : this->gate_still_sensors_) { for (auto *s : this->gate_still_sensors_) {
if (s != nullptr && !std::isnan(s->get_state())) { 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())) { 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 #endif

View file

@ -70,6 +70,8 @@ bool MQTTSensorComponent::send_initial_state() {
} }
bool MQTTSensorComponent::publish_state(float value) { 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)); return this->publish(this->get_state_topic_(), value_accuracy_to_string(value, accuracy));
} }
std::string MQTTSensorComponent::unique_id() { return this->sensor_->unique_id(); } std::string MQTTSensorComponent::unique_id() { return this->sensor_->unique_id(); }