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 = "192.168.0.xxx";
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.println(topic);

  Serial.print("Message: ");

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

  Serial.println();
}

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

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

  WiFi.begin(Wifi_SSID, Wifi_password);
  Serial.print("Connecting");
  while(WiFi.status() != WL_CONNECTED)
  {
    delay(1000);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());

  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);

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

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

void loop() 
{
    if(!client.connect("Window"))
    {
      Serial.println("Window - Connecting...");
      client.connect("Window");
    }

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

    if(currentWindowValue != lastWindowValue)
    {
      if(digitalRead(window_sensor) == LOW)
      {
        client.publish("outWindow","CLOSED");
        Serial.println("Windows is closed.");
      }
      else if(digitalRead(window_sensor) == HIGH)
      {
        client.publish("outWindow","OPENED");
        Serial.println("Windows is opened.");
      }
      lastWindowValue = currentWindowValue; 
    }

  client.loop();
}

My current configuration.yaml:

switch:
  - platform: mqtt
    name: "Light"
    command_topic: "inLight"
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
      
sensor:
  - 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)