[SOLVED] HA won't talk to mosquitto after Upgrade to 60.1 on autostart (SystemD)

Hello HA-Community for Vienna!

I have been using HA for almost a year now, and I am very happy with this large community and possibilities!
Recently I have bought an Xiaomi Aquara Gateway an wanted to Integrate it with HA - unfortunately I was running version 53 of HA and it did not have support for it yet.

So I decided to go up-to-date and updated according to
Python 3.6 upgrade of a virtualenv

So I am currently running it on a raspberry pi with Debian Stretch and Python 3.6.3 Installed running HA 60.1 in an venv also at Python 3.6.3. Mosquitto is up-to-date
Everything is working just fine and like before when I start HA from console with:

sudo su -s /bin/bash homeassistant
cd /srv/homeassistant
source bin/activate
hass

MQTT Component properly initializes and works properly with my devices
However I followed up this guide to setup autostart Autostart using systemd
And now HA cant initalize the MQTT Components.
First I thought Mosquitto isn’t set up in time before HA, but in the mosquitto.service systemD I have
After=xdk-daemon.service
Before=home-assistant.service

And in the home-assistant.service I have:
After=network.target mosquitto.service

After fresh reboot
sudo systemctl status mosquitto.service
and
sudo systemctl status home-assistant.service

both return me “active” where mosquitto always has a lower PID than HA. So I assume Mosquitto IS starting before HA.

I have no clue why HA wont initalize the MQTT Component… I hope you guys know a solution.
I already tried reinstalling Mosquitto+Client and removing an setting up again both systemd for HA and Mosquitto.

Here is what is logged from HA:
[Errno 101] Network is unreachable

2018-01-19 12:17:13 ERROR (MainThread) [homeassistant.setup] Error during setup of component mqtt
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/setup.py", line 190, in _async_setup_component
    result = yield from component.async_setup(hass, processed_config)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/mqtt/__init__.py", line 387, in async_setup
    success = yield from hass.data[DATA_MQTT].async_connect()
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/mqtt/__init__.py", line 498, in async_connect
    self._mqttc.connect, self.broker, self.port, self.keepalive)
  File "/usr/local/lib/python3.6/asyncio/futures.py", line 332, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 250, in _wakeup
    future.result()
  File "/usr/local/lib/python3.6/asyncio/futures.py", line 245, in result
    raise self._exception
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.6/site-packages/paho/mqtt/client.py", line 768, in connect
    return self.reconnect()
  File "/srv/homeassistant/lib/python3.6/site-packages/paho/mqtt/client.py", line 895, in reconnect
    sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
  File "/usr/local/lib/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/usr/local/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
**OSError: [Errno 101] Network is unreachable**

This implies that HA is starting before the network is ready. You need to specify HA needs the network in your systemd file. Mine looks like

$ cat /etc/systemd/system/[email protected] 
# This is a simple service file for systems with systemd to tun HA as user.
#
# For details please check https://home-assistant.io/getting-started/autostart/
#
[Unit]
Description=Home Assistant for %i
After=network.target mosquitto.service influxdb.service

[Service]
Type=simple
User=%i
ExecStart=/srv/homeassistant/bin/hass
SendSIGKILL=no
Restart=on-failure

[Install]
WantedBy=multi-user.target

1 Like

Thank you @gpbenton!
Although I had the
After=network.target mosquitto.service
in my home-assistant.service
I was missing the “influxdb.service”
Could you explain what it does?
Also I changed my config to localhost:

mqtt:
broker: 127.0.0.1 (before I had 192.168.0.11)
port: 1883
client_id: xxx
username: xxxx
password: xxxx

And enabled "Slow Boot Wait for network " in the raspi-config. So Any of these might did the fix for my problem.

influxdb shouldn’t be necessary if you don’t have influxdb installed. I just forgot to take that out.

If you already had the network.service, I suspect your other changes are more likely to have solved the issue, especially the change to the boot config ( I have that also).

1 Like