Set state of sensor to 'unavailable' after not reporting for X time

Hi All,

Until now I found an answer to all my questions on the forum or in the documentation. But now I having a ‘issue’ for which I do not find the solution. (if there is one)

Setup:
NodeMCU with reed contact to detect open/closed state of the garage door.
This reports ‘online’ periodically on the “states/doors/garage” topic.

Sensor:

- platform: mqtt
  name: door_outdoor_garage
  device_class: garage_door
  availability_topic : "states/doors/garage"
  payload_available: "online"
  state_topic: "doors/garage"
  payload_on: "open"
  payload_off: "closed"

Issue:
I’m not able to send the ‘offline’ message on the state topic when the nodeMCU has WIFI issues for example. Therefore I would like to mark the sensor as ‘unavailable’ after a specific time the ‘online’ message wasn’t reported.

I can not imagine this isn’t possible, but I do not have any clue on how to start.
Can someone please provide me the solution?

You need to set up your device to send LWT messages:

Indeed, the library I’m using does support this.

Unthinkable I didn’t think about that!

Thanks!