ESP8266 and HA LWT

I have a aquarium controller running on a ESP, I can send it Mqtt messages and turn lights and read its temperature. The ESP is using PubSubClient library. I trying to setup LWT messages with HA. I set up PubSubClient using https://pubsubclient.knolleary.net/api.html#connect4 , and when i plug in the ESP i get the willMessage on HA which is “tank/status online”.
My question is how can i get “offline” when ESP is disconnected? Does HA need to send message the ESP periodically until it does not respond and then change the payload of tank/status to “offline” ?

I have a device called Sonoff running Tasmota code https://github.com/arendst/Sonoff-Tasmota and it does this automatically without me doing anything in HA.

The will message is something that is stored in the broker. When the broker detects that this client is disconnected, it sends the message to all clients subscribed to the topic.

So the message you get when you plug in your Esp is not being sent as a will, but probably by the Esp start up code as a welcome message.

I have an esp using the pubsub client that sends a will message, so I am fairly sure the library works in that respect. Which broker are you using?

The broker is mosquito on same raspberry as HA

Which is the same configuration as I have.

But looking at my code, I can see I used this library https://github.com/Imroy/pubsubclient, which is different even though it has the same name.

So I can’t really say if the library you used actually works. If you can’t get your existing library to work, I suggest you try installing the one I use. The code signatures are slightly different, but are fairly straightforward. My code to connect is

      // Attempt to connect
      if (mqttClient.connect("433toMQTTto433", "sensor/433/status", 1, true,
                             "offline")) {
        // Once connected, publish an announcement...
        mqttClient.publish("sensor/433/status","online", true);

This is what I have in that section right now.

  //if connected, subscribe to the topic(s) we want to be notified about
      if (client.connect((char*) clientName.c_str(),mqtt_user, mqtt_password, "tank/status",0,0,"online")) {
        delay(50);
        Serial.println("\tMTQQ Connected");
        client.subscribe(TankTopic1);
        client.subscribe(TankTopic2);
        client.subscribe(TankTopic3);
        
      }

      //otherwise print failed for debugging
      else{Serial.println("\tFailed."); abort();}

I will try to change what i have to do something similar with the code as you, if that fails i will try changing the library.


Nice, worked great. thank you.

  //if connected, subscribe to the topic(s) we want to be notified about
  if (client.connect((char*) clientName.c_str(),mqtt_user, mqtt_password, "tank/status",1,true,"offline")) {
    client.publish("tank/status","online", true);
    delay(50);
    Serial.println("\tMTQQ Connected");
    client.subscribe(TankTopic1);
    client.subscribe(TankTopic2);
    client.subscribe(TankTopic3);
    
  }

  //otherwise print failed for debugging
  else{Serial.println("\tFailed."); abort();}

OH DEAR GOD!!! Thank you so much!!!

I have been trying to get this working for WEEKS, and you saved me!!!

Me too!! :grinning: Thank You!!