Systemd needs to start mosquitto before HA

Every time I reboot my system, all the MQTT-based components fail to initialize. I know that mosquiitto is getting started at boot time, but must be starting after HA (or not early enough before). If I simply stop/start HA manually at that point, it connects up with the already running server.

I’m not very familiar with systemd scripts, but after some google work, I tried adding this line to the “[Unit]” section of /etc/systemd/system/[email protected]


but that did not solve the problem.

I’ve been having the same issue with zoneminder and home assistant. Modifying the systemd files of each with Before and After lines has no effect. Would appreciate any input.

This has always worked for me, but it will only work if mosquitto is started as a systemd service. How are you starting mosquitto?

The serivce is started automatically at boot time from /usr/lib/systemd/system/mosquitto.service which contains

Description=Mosquitto MQTT v3.1/v3.1.1 Broker
Documentation=man:mosquitto.conf(5) man:mosquitto(8)

ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf


and is configured to start automatically at boot time with systemctl enable mosquitto
(And it does get started. After boot, I can see that it’s running, and if I restart HA it starts working because it’s able to connect to the broker)

That is different from mine, which uses init.d installed from the package, but as far as I can see it should work.

You can check to see if mosquitto was actually started before HA by checking the process id numbers. They are give in order so HA should be higher if it was started after mosquitto

 $ ps -ef | grep mosquitto
 $ ps -ef | grep hass

Is there anythnig in the mosquitto log file that would indicate it is slow startup?

Immediately after rebooting, I have

[root@z200 ~]# ps -fe |grep [m]osquitto
mosquit+   947     1  0 07:52 ?        00:00:00 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
[root@z200 ~]# ps -fe |grep [h]ass
homeass+  1294     1  3 07:53 ?        00:00:06 /srv/homeassistant/bin/python3 /srv/homeassistant/bin/hass -c /home/homeassistant/.homeassistant

So, it looks like mosquitto did start first, but the info panel still shows:

Invalid config
The following components and platforms could not be set up:


Please check your config 

As usual, I can fix it with

[root@z200 ~]# systemctl stop  home-assistant@homeassistant
[root@z200 ~]# systemctl start  home-assistant@homeassistant

Maybe there is something slowing the startup of mosquitto. Is there anything in the mosquitto log file (normally in /var/log/mosquitto/mosquitto.log) that would indicate a problem. Mine starts with

1490613437: mosquitto version 1.4.11 (build date Mon, 20 Feb 2017 22:47:27 +0000) starting
1490613437: Config loaded from /etc/mosquitto/mosquitto.conf.
1490613437: Opening ipv4 listen socket on port 1883.
1490613437: Opening ipv6 listen socket on port 1883.
1490613437: New connection from on port 1883.
1490613437: New client connected from as mysensors-1 (c1, k15).
1490613439: New connection from on port 1883.
1490613439: New client connected from as 433toMQTTto433 (c1, k15).
1490613443: New connection from ::1 on port 1883.
1490613443: New client connected from ::1 as Energenie Controller (c1, k60).
1490614496: New connection from ::1 on port 1883.
1490614496: New client connected from ::1 as paho/1D04E8882DD1483555 (c1, k60).
1490616171: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.

Unfortunately the times haven’t been expanded in the mosquitto log, so comparing this to the start time of HA is going to need some work.

As an experiment, I moved the MQTT broker to another independent server. So the broker is running all the time, even when the HA system is rebooted. The MQTT components still fail to initialize at boot time. But once the system is up and I restart HA, then everything is fine. There must be “something” other than HA or MQTT that’s getting started in the wrong order, but I don’t know what that could be.

Does HA wait for the network to come up?

Description=Home Assistant for %i mosquitto.service

I don’t explicity wait for the network, but all the other network related services (Chromecast, Wemo, Insteon, etc.) work OK,

Hey guys,
I just installed a new raspberry pi, home-assistant and mosquitto.
I have the same issue as described above.
I added the mosquitto.service to the after condition of the homeassistant systemd unit, but still when I reboot the rpi mqtt is not working in HA and i need to manually restart HA. Then it is working fine.

Have you come up with a solution?

I’ll add an unhelpful me too here…