diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 7571f7def..06455a1c9 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -55,6 +55,9 @@ uint8_t Mqtt::connectcount_ = 0; uint32_t Mqtt::mqtt_message_id_ = 0; char will_topic_[Mqtt::MQTT_TOPIC_MAX_SIZE]; // because MQTT library keeps only char pointer +std::string Mqtt::lasttopic_ = ""; +std::string Mqtt::lastpayload_ = ""; + // Home Assistant specific // icons from https://materialdesignicons.com used with the UOMs (unit of measurements) MAKE_PSTR_WORD(measurement) @@ -277,6 +280,11 @@ void Mqtt::on_message(const char * topic, const char * payload, size_t len) cons } return; } + // for misconfigured mqtt servers and publish2command ignore echos + if (publish_single_ && publish_single2cmd_ && lasttopic_ == topic && lastpayload_ == message) { + LOG_DEBUG("Received echo message %s: %s", topic, message); + return; + } // check first against any of our subscribed topics for (const auto & mf : mqtt_subfunctions_) { @@ -857,6 +865,8 @@ void Mqtt::process_queue() { // else try and publish it uint16_t packet_id = mqttClient_->publish(topic, mqtt_qos_, message->retain, message->payload.c_str(), message->payload.size(), false, mqtt_message.id_); + lasttopic_ = topic; + lastpayload_ = message->payload; LOG_DEBUG("Publishing topic %s (#%02d, retain=%d, retry=%d, size=%d, pid=%d)", topic, mqtt_message.id_, diff --git a/src/mqtt.h b/src/mqtt.h index b709c3f9b..406752fa8 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -297,6 +297,9 @@ class Mqtt { static uint8_t connectcount_; static bool ha_climate_reset_; + static std::string lasttopic_; + static std::string lastpayload_; + // settings, copied over static std::string mqtt_base_; static std::string mqtt_basename_;