MQTT device tracker for heartbeating devices

Can the MQTT device tracker be configured to mark a device as “OFF” if it hasn’t received a message on the configured topic within a period of time, for those devices that send heartbeats on MQTT?

I am using hacked Dafang cameras that produce MQTT messages on a regular basis indicating the status of the camera. I’d like to use this for the MQTT device tracker as the heartbeat is very frequent (I believe 1/minute) and better than the nmap device tracking frequency and accuracy. But as far as I can tell, the MQTT device tracker never determines that a device is “OFF”. I pulled the plug on one camera and 30 minutes later, it still showed as “ON”.

The device doing the publishing, can that be altered to add a LWT (last will and testament) message on connection? Then the mqtt server knows that the device has gone.

Alas I’m mostly concerned about power loss or hangups.

what @psyciknz told you above is exactly what you want for your need.

the device publishes a “last will message” when it first connects to the broker. as long as the device stays connected to the broker for a certain period of time by receiving it’s “keep alive” message peridiocally then everything works normally. if the broker doesn’t receive the “keep alive” message after that time then it publishes that devices “last will” message to the network and the device is marked as “unavailable” or what ever you configure the unavailable state to be.

it doesn’t care why it dropped offline.

Ah, excellent, I’ll have to see what gets published as the last will. Thanks! (I’m learning MQTT, obv.)

Ok so it seems that Dafang Hacks has a way to specify a LWT, and given the MQTT device tracker code, it appears that it expects to see a “location_name” payload. So a LWT would be able to change the location_name and HASS could trigger an automation on that. Am I correct in this assessment?

Seems that components/device_tracker/mqtt.py could take a rich JSON payload with state. Or, for that matter, that it could use the generic “consider_home” timeout to consider any MQTT not received in that period to no longer be home.

There should be something like an $online or a heartbeat type topic.

So if I write a python service I’ll make a $online topic with a payload as True as the LWT, in teh connection of the mqtt client (in the python code) there’ll be a like that sets the LWT as $online False which is the message that will be posted when the mqtt server detects that client has disconnected.