Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT sensors handling of publishing NaN values #7768

Merged
merged 15 commits into from
Dec 2, 2024
4 changes: 4 additions & 0 deletions esphome/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
CONF_USE_ABBREVIATIONS,
CONF_USERNAME,
CONF_WILL_MESSAGE,
CONF_PUBLISH_NAN_AS_NONE,
PLATFORM_BK72XX,
PLATFORM_ESP32,
PLATFORM_ESP8266,
Expand Down Expand Up @@ -296,6 +297,7 @@ def validate_fingerprint(value):
cv.Optional(CONF_QOS, default=0): cv.mqtt_qos,
}
),
cv.Optional(CONF_PUBLISH_NAN_AS_NONE, default=False): cv.boolean,
}
),
validate_config,
Expand Down Expand Up @@ -449,6 +451,8 @@ async def to_code(config):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)

cg.add(var.set_publish_nan_as_none(config[CONF_PUBLISH_NAN_AS_NONE]))


MQTT_PUBLISH_ACTION_SCHEMA = cv.Schema(
{
Expand Down
4 changes: 4 additions & 0 deletions esphome/components/mqtt/mqtt_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,10 @@ void MQTTClientComponent::set_log_message_template(MQTTMessage &&message) { this
const MQTTDiscoveryInfo &MQTTClientComponent::get_discovery_info() const { return this->discovery_info_; }
void MQTTClientComponent::set_topic_prefix(const std::string &topic_prefix) { this->topic_prefix_ = topic_prefix; }
const std::string &MQTTClientComponent::get_topic_prefix() const { return this->topic_prefix_; }
void MQTTClientComponent::set_publish_nan_as_none(bool publish_nan_as_none) {
this->publish_nan_as_none_ = publish_nan_as_none;
}
bool MQTTClientComponent::is_publish_nan_as_none() const { return this->publish_nan_as_none_; }
void MQTTClientComponent::disable_birth_message() {
this->birth_message_.topic = "";
this->recalculate_availability_();
Expand Down
6 changes: 6 additions & 0 deletions esphome/components/mqtt/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ class MQTTClientComponent : public Component {
void set_on_connect(mqtt_on_connect_callback_t &&callback);
void set_on_disconnect(mqtt_on_disconnect_callback_t &&callback);

// Publish None state instead of NaN for Home Assistant
void set_publish_nan_as_none(bool publish_nan_as_none);
bool is_publish_nan_as_none() const;

protected:
void send_device_info_();

Expand Down Expand Up @@ -328,6 +332,8 @@ class MQTTClientComponent : public Component {
uint32_t connect_begin_;
uint32_t last_connected_{0};
optional<MQTTClientDisconnectReason> disconnect_reason_{};

bool publish_nan_as_none_{false};
};

extern MQTTClientComponent *global_mqtt_client; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
Expand Down
2 changes: 2 additions & 0 deletions esphome/components/mqtt/mqtt_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ bool MQTTSensorComponent::send_initial_state() {
}
}
bool MQTTSensorComponent::publish_state(float value) {
if (mqtt::global_mqtt_client->is_publish_nan_as_none() && std::isnan(value))
return this->publish(this->get_state_topic_(), "None");
int8_t accuracy = this->sensor_->get_accuracy_decimals();
return this->publish(this->get_state_topic_(), value_accuracy_to_string(value, accuracy));
}
Expand Down
1 change: 1 addition & 0 deletions esphome/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@
CONF_PROJECT = "project"
CONF_PROTOCOL = "protocol"
CONF_PUBLISH_INITIAL_STATE = "publish_initial_state"
CONF_PUBLISH_NAN_AS_NONE = "publish_nan_as_none"
CONF_PULL_MODE = "pull_mode"
CONF_PULLDOWN = "pulldown"
CONF_PULLUP = "pullup"
Expand Down
1 change: 1 addition & 0 deletions tests/components/mqtt/common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mqtt:
- mqtt.publish:
topic: some/topic
payload: Good-bye
publish_nan_as_none: false

binary_sensor:
- platform: template
Expand Down
Loading