How to actually use mqtt with add-on and discovery?

Tags: #<Tag:0x00007f738fd1a240> #<Tag:0x00007f738fd1a178>


I have tried to make use of mqtt with a sensor that I have made.

But I am having trouble getting “things” to work properly, and the sea of old and new information is a bit overwhelming so I wanted to see if anyone could help me understand what is actually needed of configuration in home assistant and the c++ code on the esp8266.

I have installed the add-on Mosquitto broker (version 5.1), and the current config is like this:

mqtt configuration
  - username: mqtt_user
    password: mqtt_password
anonymous: true
  active: false
  folder: mosquitto
certfile: fullchain.pem
keyfile: privkey.pem
require_certificate: false

After the installation I added the integration in home assistant and enabled discovery.

The current code I have on the esp8266 is like this, I am using the pubsubclient (version 2.8):

esp8266 code
#include <ESP8266WiFi.h>
//#include <WiFiClient.h>
#include <PubSubClient.h>
#include <secrets.h>

String ssid = SECRET_SSID;
String password = SECRET_PASS;
String hostName = "esp12_mailboxsensor01";
//IPAddress ip(192,168,1,154);
//IPAddress gw(192,168,1,1);
//IPAddress dns(192,168,1,2);
//IPAddress subnet(255,255,255,0);
#define MQTT_SERVER ""

int sensorPin = 12;
int enablePin = 5;
int sensor;
int code;
String temp = "homeassistant/" + hostName + "/opening";
const char* stateTopic = temp.c_str();

//Enable getvcc

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {

  //Enable power signal as soon as possible
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, HIGH);



  //Connect to WIFI
  //WiFi.config(ip, subnet, gw, dns);
  WiFi.begin(ssid, password);

  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    WiFi.begin(ssid, password);
    Serial.println("WiFi failed, retrying.");

  client.setServer(MQTT_SERVER, 1883);
  while (!client.connected()) {
    // Attempt to connect
    if (client.connect(hostName.c_str())) {
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

  //Get the battery voltage and send to HA
  float vcc = ESP.getVcc();
  vcc = vcc / 1000;
  String svcc = String(vcc,3);
//  Serial.println(code);
//  Serial.println();

  sensor = digitalRead(sensorPin);
  //Send new data to HA
  if (sensor == 0){
    client.publish(stateTopic, "open");
    Serial.println("Published open.");
  else if (sensor == 1) {
    client.publish(stateTopic, "closed");
    Serial.println("Published closed.");
  //Turn of the latching circuit
  digitalWrite(enablePin, LOW);

void loop() {
  //Don't want to loop anything

In the logs on the core_mosquitto I get these messages:

1600258898: New connection from on port 1883.
1600258898: New client connected from as esp12_mailboxsensor01 (p2, c1, k15).
1600258920: Client esp12_mailboxsensor01 has exceeded timeout, disconnecting.

And I can’t find any information information in home assistant that it has received anything.
There are neither any devices showing up in the MQTT integration.

Some questions I have that don’t need to be answered if they don’t contribute to the solution.

  1. I have read on that it requires configuration written in the yaml file. Is this true when enabling the discovery when enabling the integration?
    And if not, what is the discovery_prefix?
  2. The example in shows that the sensor should be created manually before turning it on, maybe this is what I am missing? If so, how could I write that in the c++ code?
  3. Is there anything else I am missing?