What is the proper way for software to tell HA that it is alive/dead via MQTT?

There is some custom software that is integrated with HA via MQTT. For now it is integrated with HA without autodiscovery topic. In HA configuration.yaml there are couple mqtt sensors and mqtt switches.

It is possible to change that software. I’m sure in some time it will send autodiscovery info for home assistant and there will be no need to configure it manually in configuration.yaml

The task is add to home assistant information if the software is working or not. I can think of several ways how to do it, but I’m not sure what is the best way (and maybe I’m missing some ways). It will be great it this solution can be added to HA with autodiscovery without the need to add anything to configuration.yaml. So, my question is what do you think should be done to solve this task.

There can be several instances of this software on the computer, so monitoring if the computer is online is not the solution.

This is the ways I’m considering to implement:

  • the software every second sends data to the special topic /software_name/heartbeat. It sends the current timestamp. HA reads from that topic and if the timestamp from that topic is different from the current timestamp in HA more than 3 seconds then the needed action is performed (for example it turns on input_boolean “software_name_is_alive”
  • the second way that I’m thinking is about MQTT feature “Last Will”. I have no experience with using that feature, I think that is should create topic when the software disconnects from MQTT broker. And it can be read from HA and HA can switch off the “software_name_is_alive” input_boolean

I don’t like the first way because there will be a lot of events in HA, and I’m not sure that this logic can be implemented in autodiscovery. So, for now I think that the second way is better. But I would like to hear what do you think.