Mosquitto MQTT 4.1 doesnt work after update

Just an update on this I eventually got it working by changing the IP address of my raspberry pi causing a change in the mqtt ip used, interestingly I still couldn’t get integrations to work however with the mqtt settings in configuration.yaml it did work and I’m now on 4.3 at long last.
Not sure why this made it work perhaps some sort of conflict with the original ip ??

Shout out to @DavidFW1960 for taking the time to remotely look at my install and help me get things up and running, Thankyou

1 Like

Ok turns out i’m still having problems however i have narrowed it down to my google wifi mesh network… if i plug my raspberry pi directly into my modem router (before my mesh network) it works and mqtt integrates, if i plug it into the network after my modem (the one that features various switches and 5 google mesh nodes) it will not integrate ?? any suggestions

I’m on version 4.3 and what @cogneato suggested on March 13 worked for me. This is my first attempt at using MQTT on HA. The client connects to the broker as you’d expect but nothing would get written when I published. I’m using the default config but this time I went and created a user in configuration > user and that is what used and the user id and password when creating the connection to the broker. That fixed the issue… Thank you all

New Hass.io install here, installed within a docker container.

My issue seems even weirder - going into the integrations page for MQTT, nada. Nothing to select, not even a tick box.

For now I’ve just installed Mosquitto seperately in its own container.

Finally got it working with the latest 5 addon version.

The steps:

  1. Install MQTT broker plugin
  2. In the config enter the following:
{
  "logins": [
    {
      "username": "mqttm",
      "password": "mqttm"
    }
  ],
  "anonymous": true,
  "customize": {
    "active": true,
    "folder": "mosquitto"
  },
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem"
}

You can turn anonymous false, as it appears that this function doesnt work (at least for me)
3. save config and start mqtt
4.create file
/share/mosquitto/acl.conf with contents

acl_file /share/mosquitto/accesscontrollist

and /share/mosquitto/accesscontrollist with contents

user mqttm
topic readwrite #
user homeassistant
topic readwrite #

change user name with yours
5. save everything
6.restartt Hassio
Do not need to enter anything in configuration to get it working.
I have tested with a couple of clients.

in configuration added a dummy sensor

sensor:
  - platform: mqtt
    state_topic: "home/minde/temp"
    name: "MQTT Sensor 1"

after rebooting sent the mqtt message from another raspberry pi:

mosquitto_pub -h 192.168.0.101 -t home/minde/temp -u "mqttm" -P "mqttm" -m 75

and it worked.
mqttsensor

Hope this helps.

4 Likes

Thanks for this! It worked so far. One small thing, it wasn’t clear you had to run the Configure in Integrations with discovery not ticked. Again thank you so so much!

1 Like

You need to tick discovery in integrations.

It works without if you’re happy to use define in yaml

I tried this set up in 5.0 and appear to still be having issues. Here is my log. Any ideas?

[13:54:14] INFO: Setup mosquitto configuration
[13:54:14] WARNING: SSL not enabled - No valid certs found!
[13:54:15] INFO: Found local users inside config
[13:54:16] INFO: Initialize Hass.io Add-on services
[13:54:16] INFO: Initialize Home Assistant discovery
[13:54:16] INFO: Start Mosquitto daemon
1562439256: Loading config file /share/mosquitto/acl.conf
1562439256: mosquitto version 1.6.3 starting
1562439256: Config loaded from /etc/mosquitto.conf.
1562439256: Loading plugin: /usr/share/mosquitto/auth-plug.so
1562439256: |-- *** auth-plug: startup
1562439256: ├── Username/password checking enabled.
1562439256: ├── TLS-PSK checking enabled.
1562439256: └── Extended authentication not enabled.
1562439256: Opening ipv4 listen socket on port 1883.
1562439256: Opening ipv6 listen socket on port 1883.
1562439256: Opening websockets listen socket on port 1884.
1562439256: Warning: Mosquitto should not be run as root/administrator.
1562439256: New connection from 192.168.0.101 on port 1883.
[INFO] found user on local database

Recent mosquitto has given me nothing but headaches. It seems to mangle all the packet/payloads. After attempting a bunch of suggestions here I looked for other possibilities and just wanted to share that I have installed emqx as a docker container and have no issues. (I run hassio/ha as docker containers on an aarch64 box) The community version is in active maintenance.


https://hub.docker.com/r/emqx/emqx
docker run -dit --restart always  --name emqx-mqtt-broker -p 18083:18083 -p 1883:1883 emqx/emqx:latest 

Up side is that emqx runs a nice dashboard 18083 (admin/public login).

Hello everyone. I am very new to all of this and have one final hurdle left before I am fully up and running. I am almost finished!! But I am getting stuck where everyone else is on this page. I can’t get mqtt to connect AND I have no previous snapshots. If someone could please point me in the right direction, I will give you unlimited amounts of good karma!!

I am running MQTT 5 and have user and password set.

MQTT config:

{
  "logins": [
    {
      "username": "mqtt_admin",
      "password": "beatles"
    }
  ],
  "anonymous": false,
  "customize": {
    "active": true,
    "folder": "mosquitto"
  },
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem"
}

Config-yaml code:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

sensor 1:
  platform: mqtt
  state_topic: 'DHT22-1/sensor1'
  name: 'Temperature'
  unit_of_measurement: '°F'
  value_template: '{{ value_json.temperature }}'

sensor 2:
  platform: mqtt
  state_topic: 'DHT22-1/sensor1'
  name: 'Humidity'
  unit_of_measurement: '%'
  value_template: '{{ value_json.humidity }}'

Why did you ever put that in config yaml file??? That is part of the ACL configuration.

Hey thanks for your help. I did it because I am completely new to this and have no idea what I’m doing. I’ve been following a Youtube video and when I couldn’t get MQTT to pull the humidity and temp reading from a Node mcu, I was troubleshooting and saw someone mention that code so I just pasted it there lol. If I remove that part of the code, it still won’t work. It’s what I did originally. I believe this has to do with the update to MQTT, but I don’t know how to troubleshoot unfortunately. I removed it from the code above to avoid confusion.

Can you show me your Acl.conf and accesscontrollist files please.

I don’t have them. My share folder is empty. I’m guessing these need to be created…?

My Arduino code:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#include <ArduinoJson.h>

#define MQTT_VERSION MQTT_VERSION_3_1_1

// Wifi: SSID and password
const char* WIFI_SSID = "and Yoko";
const char* WIFI_PASSWORD = "lifeinprison";

// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "DHT22-1";
const PROGMEM char* MQTT_SERVER_IP = "192.168.0.105";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "mqtt_admin";
const PROGMEM char* MQTT_PASSWORD = "beatles";

// MQTT: topic
const PROGMEM char* MQTT_SENSOR_TOPIC = "DHT22-1/sensor1";

// DHT - D1/GPIO5
#define DHTPIN D2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
WiFiClient wifiClient;
PubSubClient client(wifiClient);

long lastMsg = 0;
int sleepSeconds = 10;

// function called to publish the temperature and the humidity
void publishData(float p_temperature, float p_humidity) {
  // create a JSON object
  // doc : https://github.com/bblanchon/ArduinoJson/wiki/API%20Reference
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  // INFO: the data must be converted into a string; a problem occurs when using floats...
  root["temperature"] = (String)p_temperature;
  root["humidity"] = (String)p_humidity;
  root.prettyPrintTo(Serial);
  Serial.println("");
  char data[200];
  root.printTo(data, root.measureLength() + 1);
  client.publish(MQTT_SENSOR_TOPIC, data, true);
  yield();
}

// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("INFO: Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
      Serial.println("INFO: connected");
    } else {
      Serial.print("ERROR: failed, rc=");
      Serial.print(client.state());
      Serial.println("DEBUG: try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  // init the serial
  Serial.begin(115200);

  dht.begin();

  // init the WiFi connection
  Serial.println();
  Serial.println();
  Serial.print("INFO: Connecting to ");
  WiFi.mode(WIFI_STA);
  Serial.println(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("INFO: WiFi connected");
  Serial.println("INFO: IP address: ");
  Serial.println(WiFi.localIP());

  // init the MQTT connection
  client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  // Send a message every minute
  if (now - lastMsg > 1000 * sleepSeconds) {
    lastMsg = now;
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();

    if (isnan(h) || isnan(t)) {
      Serial.println("ERROR: Failed to read from DHT sensor!");
      return;
    } else {
      publishData(t, h);
    }
  }
} 

My configuration.yaml:


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:


# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

sensor 1:
  platform: mqtt
  state_topic: 'DHT22-1/sensor1'
  name: 'Temperature'
  unit_of_measurement: '°F'
  value_template: '{{ value_json.temperature }}'

sensor 2:
  platform: mqtt
  state_topic: 'DHT22-1/sensor1'
  name: 'Humidity'
  unit_of_measurement: '%'
  value_template: '{{ value_json.humidity }}'
  

MQTT Log:

Log
[21:58:59] INFO: Setup mosquitto configuration
[21:58:59] WARNING: SSL not enabled - No valid certs found!
[21:58:59] INFO: No local user available
[21:59:00] INFO: Initialize Hass.io Add-on services
[21:59:00] ERROR: Can't setup Hass.io service mqtt
[21:59:00] INFO: Initialize Home Assistant discovery
[21:59:00] INFO: Start Mosquitto daemon
1568685540: mosquitto version 1.6.3 starting
1568685540: Config loaded from /etc/mosquitto.conf.
1568685540: Loading plugin: /usr/share/mosquitto/auth-plug.so
1568685540:  ├── Username/password checking enabled.
1568685540:  ├── TLS-PSK checking enabled.
1568685540:  └── Extended authentication not enabled.
1568685540: |-- *** auth-plug: startup
1568685540: Opening ipv4 listen socket on port 1883.
1568685540: Opening ipv6 listen socket on port 1883.
1568685540: Opening websockets listen socket on port 1884.
1568685540: Warning: Mosquitto should not be run as root/administrator.
1568685544: New connection from 192.168.0.106 on port 1883.
[INFO] found mqtt_admin on Home Assistant
1568685545: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685583: Client DHT22-1 has exceeded timeout, disconnecting.
1568685590: New connection from 192.168.0.106 on port 1883.
1568685590: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685614: Client DHT22-1 has exceeded timeout, disconnecting.
1568685614: New connection from 192.168.0.106 on port 1883.
1568685614: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685681: Client DHT22-1 has exceeded timeout, disconnecting.
1568685682: New connection from 192.168.0.106 on port 1883.
1568685682: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685723: Client DHT22-1 has exceeded timeout, disconnecting.
1568685724: New connection from 192.168.0.106 on port 1883.
1568685724: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685765: Client DHT22-1 has exceeded timeout, disconnecting.
1568685766: New connection from 192.168.0.106 on port 1883.
1568685766: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685879: New connection from 172.30.32.1 on port 1883.
[INFO] found homeassistant on local database
1568685880: New client connected from 172.30.32.1 as auto-B217936D-C2DD-2850-6C01-1DC467B8CEE9 (p2, c1, k60, u'homeassistant').
1568685880: Client auto-B217936D-C2DD-2850-6C01-1DC467B8CEE9 disconnected.
1568685913: Client DHT22-1 has exceeded timeout, disconnecting.
1568685913: New connection from 192.168.0.106 on port 1883.
[INFO] found mqtt_admin on Home Assistant
1568685913: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568685923: New connection from 172.30.32.1 on port 1883.
1568685923: New client connected from 172.30.32.1 as auto-16F91CE4-8773-142D-6660-3FA3B9D597DB (p2, c1, k60, u'homeassistant').
1568685997: Client DHT22-1 has exceeded timeout, disconnecting.
1568685997: New connection from 192.168.0.106 on port 1883.
1568685997: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').
1568686019: Client DHT22-1 has exceeded timeout, disconnecting.
1568686020: New connection from 192.168.0.106 on port 1883.
1568686020: New client connected from 192.168.0.106 as DHT22-1 (p2, c1, k15, u'mqtt_admin').

Only if you want it to work… didn’t you notice acl is mandatory for the broker since v4?

Yes, I see what you mean now. Here is what I have:

MQTT config:

{
  "logins": [
    {
      "username": "mqtt_admin",
      "password": "beatles"
    }
  ],
  "anonymous": false,
  "customize": {
    "active": true,
    "folder": "mosquitto"
  },
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem"
}

/usr/share/hassio/share/mosquitto/accesscontrollist

user mqtt_admin
topic readwrite #

/usr/share/hassio/share/mosquitto/acl.conf

acl_file /share/mosquitto/accesscontrollist

Same arduino as before
Same configure.yaml

Try this

user mqtt_admin
topic readwrite # 
user homeassistant
topic readwrite # 

Then restart the broker…

Ok, I just tried this. I don’t receive any notifications from Home Assistant, but I’m not pulling any readings either.

I don’t use ACL. That warning needs to be changed. If you use HA user credentials, leave logins empty, and anon as false it doesn’t apply.