How do I set MQTT device to "unavailable state" when it has gone offline?

Hello, everyone.

I recently made a device with the use of an ESP8266 that allows me to communicate with HA. It works as expected, and I am able to send and reserve messages to and from the ESP.

The issue I am facing is that when the ESP is unplugged from power, HA remembers the last known state the ESP was in, rather than updating the front end to show that the device has gone offline like with my Sonoff switches do.

I have thought of using the Ping (ICMP) sensor to periodically ping my ESP to see if it is still online, but unfortunately, I do not know the HA configuration YAML syntax.

If anyone is able to help solve my problem, that would be awesome! (I have been trying to solve this issue for nearly a month, and I’m yet to find an answer on how to do this!)

2 Likes

https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament/

Thank you for your response.

Iv has seen LWT before. My issue Is that I do not know how to implement it in HA. Can you help me?

Cheers.

Every core home assistant mqtt integration includes LWT. You only have to worry about sending the correct LWT messages from your custom ESP device.

e.g. For the MQTT sensor:

payload_available string(Optional, default: online)
The payload that represents the available state.

payload_not_available string(Optional, default: offline)
The payload that represents the unavailable state.

MQTT Sensor - Home Assistant

BTW, have you seen ESPHome?

It takes all the hard work out of developing ESP devices. Define devices with a simple yaml configureation file and it complies the binary for you. It supports MQTT or the home asistant API (much better unless you use deep sleep) and all of the common sensors and peripherals.

Thank you so much! After more hours then I can count, I finally got it working!!!

Arduino code:

void setup()
{
  //connect to MQTT broker and send LWT offline packet. 
  if (MQTTclient.connect(mqtt_clientID, mqtt_username, mqtt_password, availabilityTopic, 1, true, "offline"))
  {
    Serial.println("MQTT - OK");
    MQTTclient.subscribe("Light/cmd");
    //send the LWT online packet. 
    MQTTclient.publish(availabilityTopic, "online", true);
  }
  else
    Serial.println("MQTT - ERROR");
}

HA config.yaml file:

switch:
  - platform: mqtt
    name: Light
    command_topic: "inLight/cmd"
    state_topic: "inLight/state"
    availability_topic: "inLight/availability"
    payload_available: "online"
    payload_not_available: "offline"
    payload_on: "ON"
    payload_off: "OFF"
    qos: 1
    unique_id: MQTT_light
3 Likes

You didn’t actually have to include these lines as they are the default values, but it does not hurt and aids memorability for debugging when you come back to it years later.

    payload_available: "online"
    payload_not_available: "offline"

Which was why I mentioned ESPhome. Even people with no programming experience can spin up a mulitisensor or doorbell monitor in minutes.

How would you go about setting it unavailable when the device loses power.

1 Like