How do I update the state of the Hassio button if ESP8266 has gone offline?

Ladies and gentlemen of HA, I have a problem.

I am trying to set up my own ESP8266 and integrate it with Home assistant in order to make my own smart home devices. So far I was able to get the ESP to connect to HA using MQTT with the use of the “PubSubClient” Arduino library. I am able to send data to and from the ESP and have it show up on the HA dashboard. I have a button on the HA dashboard that allows me to toggle a light on the ESP.

The issue I am facing is that right now, I am able to freely toggle the HA dashboard light switch regardless of if the ESP is online. I do not like this because I do not know if the ESP is receiving the commands and I do not know if the ESP is currently online or not. So far I have tried to use the “availability_topic” to solve this issue but this only seems to bring the HA button online once my ESP has connected for the first time, but does not work when the ESP has gone offline since the ESP is not able to send the offline availability payload.

If anyone can help me, that would be greatly appreciated.

My ESP8266 code:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const int LED = D1;
const int window_sensor = D2;

const char* mqtt_server = "";
const char* mqtt_username = "xxx";
const char* mqtt_password = "xxx";
const int   mqtt_port = 1883;
const char* Wifi_SSID = "xxx";
const char* Wifi_password = "xxxx";

WiFiClient espClient;
PubSubClient client(espClient);

void callback(char* topic, byte* payload, unsigned int length) 
  Serial.print("Message arrived in topic: ");

  Serial.print("Message: ");

  String message;
  for (unsigned int i = (unsigned int) 0; i < length; i++) 
    message = message + (char)payload[i];
  if(message == "OFF") {digitalWrite(LED, LOW);}
  if(message == "ON") {digitalWrite(LED, HIGH);}


void setup() 
  pinMode(LED, OUTPUT);
  pinMode(window_sensor, INPUT_PULLUP);

  Serial.println("Readings will apear here.");

  WiFi.begin(Wifi_SSID, Wifi_password);
  while(WiFi.status() != WL_CONNECTED)
  Serial.print("Connected, IP address: ");

  client.setServer(mqtt_server, mqtt_port);

  if(client.connect("Light", mqtt_username, mqtt_password)) // clientID, Username, Password
    Serial.println("Light - MQTT - OK");
    Serial.println("Light - MQTT - ERROR");

  if(client.connect("Window", mqtt_username, mqtt_password))
    Serial.println("Window - MQTT - OK");
    Serial.println("Window - MQTT - ERROR");

void loop() 
      Serial.println("Window - Connecting...");

    bool currentWindowValue = digitalRead(window_sensor);
    static bool lastWindowValue = 0; 

    if(currentWindowValue != lastWindowValue)
      if(digitalRead(window_sensor) == LOW)
        Serial.println("Windows is closed.");
      else if(digitalRead(window_sensor) == HIGH)
        Serial.println("Windows is opened.");
      lastWindowValue = currentWindowValue; 


My current configuration.yaml:

  - platform: mqtt
    name: "Light"
    command_topic: "inLight"
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
  - platform: mqtt
    name: "Window"
    state_topic: "outWindow"
    icon: mdi:window-closed

This is the button that I want to show “unavailable” when the ESP is offline.
Screenshot (139)