How to support MQTT Discovery in microcontrollers

I apologize if this isn’t the correct place to post. I’ver recently returned to Home Assistant. I have an open source Particle.io Photon project that uses MQTT to communicate between devices, and Particle.io to interact via Alexa, iOS, etc. I’d like to integrate my Photons into Home Assistant stop creating my own Alexa, iOS, and Mac apps since HA provides all that and more. Anyways, MQTT appears to make that very simple.
I’ve integrated a few devices manually using input_booleans and MQTT messages in automations. This is pretty time consuming and fragile. I’ve read about MQTT discovery, and I’m not sure how to leverage it with my devices. I’ve read all the doc I can find about MQTT discovery, and that looks very promising, but I don’t understand the sequence of things.
In my system, each Photon controller defines and exposes the various devices that it implements. I’d like them to tell HA about themselves, and when new devices are added, etc. That appears to be what MQTT discovery does.
So does it all begin by sending a config message from my Photon controller to HA every time it powers up? Or do I have to write an automation to send MQTT messages upon power up? Or something else?

Yes.

Quite a bit on the format required and examples lower down on this page:

Awesome. I’ll try coding that up and see what happens. Thank you.

Yep, works like a charm. Thank you.
I’ve posted the working code on Github for anyone else interested in programming Particle.io devices for use with Home Assistant.

I believe you’re publishing a non-retained configuration message here.

Because the MQTT broker is not retaining the configuration message, it’s only available to currently connected and subscribed clients. If Home Assistant is offline at the moment the configuration message is published, when it reconnects to the broker it won’t receive the message (because it’s not retained by the broker).

If you publish it as a retained message, it will accessible to all subscribers at all times no matter when they connect to the broker.

1 Like

Ok, thanks for the info. I’ve added retain:true to the device configs.

Take note that it’s a bit of a double-edged sword. Publishing the discovery information as a retained message ensures it is always available to Home Assistant even if the actual device has been removed.

I helped someone solve the problem of what they called a ‘ghost device’. It was no longer physically present nor was it defined in configuration.yaml or in .storage/core.entity_registry. Yet it continued to be found by Home Assisant even after he had completely re-installed Home Assistant!

The answer was that the device was found by MQTT Discovery because it’s configuration was posted as a retained message. The retained message must be purged from the MQTT Broker and then Home Assistant will cease to discover it.

Thanks for the additional info. Upon further thought, I’ll probably change things so that the Photons send their config upon receiving a message that Home Assistant or MQTT have restarted, or upon request for update for whatever other reasons. I really want to be able to modify the Photon code whenever, and have the rest of the system adapt.

I’m thinking of switching from Mosquitto to the build in MQTT. Any thoughts about that?

If you are referring to the embedded MQTT broker, HBMQTT, the documentation indicates it is deprecated and the preference is to use some other broker.

I’ve been using Mosquitto for over a year and I have no complaints. It runs on a separate machine (Raspberry Pi) along with Node-Red.

Ok, I’ll stick with Mosquitto. Thanks for pointing that out.

Just to follow up, I added retain to the config messages and all is working great. Code on Github has been updated. Thanks for the help.
Now I’m ready to dive into ESPHome and some ESP8266 projects.

Correction, it isn’t working great. When I restart Home Assistant, it does not remember the previously automatically discovered devices.

I suspect that I’ve not configured Mosquitto correctly, but I’m not going to pursue fixing that. Instead I’m going to add an automation to have Home Assistant send an MQTT message upon starting up that will request every controller to send their config data (again). I prefer this method because it eliminates “ghost” devices when modifying Photon code.

Also, I’m currently running Mosquitto on the same Mac Mini on which Home Assistant is running. I think this potentially could cause startup race condition problems when the Mac Mini is rebooted. So I’m going to move Mosquitto over to the Raspberry Pi where I was previously running HA.

Does anyone know if running Mosquitto on RPi causes the same SD card burn-out problem that HA experiences?

This seems to imply the device’s configuration topic does not contain a retained message.

There’s an easy way to confirm if everything is working properly. Use an MQTT client to publish (as a retained message) any one of the examples found in the documentation for MQTT Discovery. For example, publish this:

{"name": "garden", "command_topic": "homeassistant/switch/irrigation/set", "state_topic": "homeassistant/switch/irrigation/state"}

to:

homeassistant/switch/irrigation/config

I just did that, using MQTT Explorer, and switch.garden appeared in the States page. I restarted Home Assistant and switch.garden is still present.

Screenshot%20from%202019-05-24%2009-58-59