Device status connected to MQTT server

Hi,

Wanted to show the connection status of the MQTT switches as “Connected” or “Disconnected” on the Front-end UI.
How can this be done.

Thanks,
Abhay

provided your MQTT devices present that info, create a sensor that extracts the value from the MQTT topic and display it on your front end?

Is there a way to look up the MQTT log and have this represented on the UI.
As a few switches are getting disconnected and needed restarting.

Log below:

starting version 3.2.2
1521554390: mosquitto version 1.4.12 (build date 2017-06-01 13:03:48+0000) starting
1521554390: Config loaded from /etc/mosquitto.conf.

1521558254: Client MomsRoom5c:cf:7f:ac:5e:43 has exceeded timeout, disconnecting.
1521558254: Socket error on client MomsRoom5c:cf:7f:ac:5e:43, disconnecting.

I track the MQTT Device’s wifi connection on my router then input that through a sensor that changes the Home/Not Home to Online/Offline.

Thank you for your response. Can you kindly elaborate on this, would like to try this out…

I also have challenges in some cases, the device is connected to the router, but disconnected from the MQTT server.
Would like a way to read the log or database and depict the status of the device on the frontend.

For what I have, I use device tracker to monitor the signal:

- platform: snmp
  host: 192.168.10.1
  community: public
  baseoid: 1.3.6.1.2.1.4.22.1.2
  track_new_devices: yes
  interval_seconds: 60
  consider_home: 240

Then input the device (from known_devices.yaml) into this:

multi1:
  value_template: '{% if is_state("device_tracker.84d6d07fd95c", "home") %}Online{% else %}Offline{%- endif %}'
  friendly_name: 'Living Room Sensor'

To get what you are looking for, I think you would need to monitor the event stream and apply a counter or similar scenario. If the counter is increased within X seconds or minutes then it is “connected”, if not then “disconnected”.

For some of my devices that can be ping, I use ping binary sensor.
ping binary sensor

Thank you @Corey_Johnson. You have been very helpful.

Thank you @JTPublic, tried the ping binary sensor method and works like magic.

Will try and see if I can get something similar to this to check if the device is connected to the MQTT server too.

You have been of great help.

1 Like

On MQTT item configuration (MQTT Switch, MQTT Light…) set the Availability Topic and payloads as needed by your device.

For example this is the config for my Sonoff:

    availability_topic: "tele/sonoff/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"

Thank you @namadori, I am not sure how i can use the topics, can you kindly elaborate.
If the device is hung, it wouldnt be able to send or receive any messages.

This is done through the Last Will and Testament (LWT) mechanism of MQTT. The MQTT client (your switch) sends this to the broker when it connects, and when it disconnects, the broker sends it to all clients subscribing to the topic.

Obviously, the HA configuration will only work if you can set the LWT in your switch, and have it send the corresponding Online message when it starts.

good explanation @gpbenton. I just wanted to add: the MQTT broker monitors the connection to the clients via keepalive messages. When the broker doesn’t receive the keepalive in the timeout period (usually 30s) it publishes the LWT message on behalf of the missing device.

Check here for a more thorough explanation:
https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament

thank you @namadori