Getting MQTT to work

Hi all!

Pretty new to this HASS business, have loads of fun exploring all the possibilities, but running into an issue with MQTT.

I have 2 rPi’s, one with HASS installed (IP *.29). I would like to send values from and to the rPi without HASS. I figured out MQTT would be a great way to do this. So I installed mosquitto on both PI’s and am able to send message back and forth:
mosquitto_sub -d -t dev/test //On either rPi

mosquitto_pub -d -t dev/test -m "test" //On the Hass rPi

Now I would like to be able to send/receive these messages with HASS. I add the following line to my config file:

When I now try to subscribe to the MQTT, I get error: connection refused. From both rPi’s.

The only way to resolve the error is to reinstall mosquitto.

What am I messing up?


Edit: Same issue when I try to add these to the mosquitto config:
allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883

Mhmmm, it seems to be an issue with mosquitto not starting at boot. Everything seems to be in place, but I still need to manually start mosquitto. Anyone?

sudo systemctl enable mosquitto

should solve it

1 Like

There are a couple of things in your post that are not clear, so I would like to make sure everyone is not talking at cross purposes

.[quote=“boneless, post:1, topic:19338”]
So I installed mosquitto on both PI’s

You only need one MQTT broker running on your network. This is normally running on the machine with HA running on it, as then you only have to keep this machine running for MQTT connections to work, as well as HA. But you can install it elsewhere.

All connections from all machines go to this one broker on the network.

If you do this without the -h option, it connects to the broker on the machine it was run on. This does not automatically communicate with a broker on another machine.

To connect to the broker on the HA machine from the remote machine do

$ mosquitto_sub -h -d -t dev/test

If you do have a broker on the second pi, it won’t hurt but will confuse you if you accidentally connect to it, so you can disable it with

sudo systemctl stop mosquitto
sudo systemctl disable mosquitto

on that pi.

And do as crz11880 says on the machine with HA, which will cause mosquitto to start after each reboot.

Thanks for the great answers!

You are right. I already added the host part while connecting from the second rPi and that works. It turned around a removed mosq. from the HASS rPi, changed the settings in HASS to point to the second rPi and that works for me. I just noticed some more people with the same issue where mosquitto not starting.

Now I run into another issue. I run the broker on the 2nd rPi and have added that config to HASS. I can publish messages from HASS and receive them on the other end.

Now when I try to add a sensor:

# Example configuration.yml entry
  - platform: mqtt
    state_topic: "home/bedroom/temperature"

And reload the configuration, it ends up in an eternal loading loop. When I reboot, HASS never loads. The config checks out. What am I doing wrong now… :slight_smile:

I think the latest mosquitto update turned auto restart off for some reason. It was probably by mistake.

What do you mean by this? Can you post a log file?

1 Like

Okay, I am a dingus…
I think the problem was that I the standard sensor for weather from YR was declared longer up. I was trying to declare another sensor longer down. That is a no-no. Argh!