Home Assistant systemctl service startup issue mqtt

Hi,

When I start Home Assistant from the command prompt, after the system loads-
“sudo systemctl start [email protected]
… everything works as expected

But when the service is set to autostart at bootup and I reboot-
The following components and platforms could not be set up:
mqtt
light.mqtt_json
sensor.mqtt
Please check your config.

Home Assistant loads properly except for the mqtt components / platforms. I think that something that MQTT depends on is not running yet but I cant figure out what it is.

My service file [email protected]

[Unit]
Description=Home Assistant
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=homeassistant
ExecStartPre=source /home/homeassistant/.homeassistant/bin/activate
ExecStart=/home/homeassistant/homeassistant/bin/hass -c “/home/homeassistant/.homeassistant”

[Install]
WantedBy=multi-user.target

Home Assistant log…

Blockquote
Traceback (most recent call last):
File “/usr/lib/python3.5/asyncio/tasks.py”, line 241, in _step
result = coro.throw(exc)
File “/home/homeassistant/homeassistant/lib/python3.5/site-packages/homeassistant/components/discovery.py”, line 134, in scan_devices
results = yield from hass.async_add_job(_discover, netdisco)
File “/usr/lib/python3.5/asyncio/futures.py”, line 380, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.5/asyncio/tasks.py”, line 304, in _wakeup
future.result()
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/concurrent/futures/thread.py”, line 55, in run
result = self.fn(*self.args, **self.kwargs)
File “/home/homeassistant/homeassistant/lib/python3.5/site-packages/homeassistant/components/discovery.py”, line 160, in _discover
netdisco.scan()
File “/home/homeassistant/homeassistant/lib/python3.5/site-packages/netdisco/discovery.py”, line 60, in scan
self.gdm.scan()
File “/home/homeassistant/homeassistant/lib/python3.5/site-packages/netdisco/gdm.py”, line 22, in scan
self.update()
File “/home/homeassistant/homeassistant/lib/python3.5/site-packages/netdisco/gdm.py”, line 77, in update
sock.sendto(gdm_msg, (gdm_ip, gdm_port))
OSError: [Errno 101] Network is unreachable

Looks like the service is loading before the network is ready? But I don’t know what else to do…

You can add multiple services to the After property, so the line could read

After=network-online.target mosquitto.service

Alternatively, depending on mosquitto.service could be sufficient, as mosquitto for sure depends on network.

After=mosquitto.service

Hi,
Thanks for responding.

I made the changes…

Blockquote
[Unit]
Description=Home Assistant
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User=homeassistant
ExecStartPre=source /home/homeassistant/.homeassistant/bin/activate
ExecStart=/home/homeassistant/homeassistant/bin/hass -c “/home/homeassistant/.homeassistant”
[Install]
WantedBy=multi-user.target

But still the same issue. Also, my mqtt broker is on another system in the subnet, not on the home assistant server.

Your configuration looks normal, but it could be that network-online.target isn’t working the way it should be.

What is your OS and hardware?

Hi,

Thanks for the response…

Debian 9
Python 3.5.3
Core Duo on a digital engine

I don’t have any experience of plain Debian, so I can’t help much. The defintion of what network-online.service is supposed to mean is here

network-online.target is a target that actively waits until the nework is “up”, where the definition of “up” is defined by the network management software.

Debian may have a different definition of this to Raspbian.

If nobody has any suggestions here, you might have more response from the debian user forums. It looks more like an OS problem than an HA problem.

Thanks! I will do as you suggested, seems like the best path.

I am having the exact same issue. Did you find a solution?

Hi @txwindsurfer,
Here is my working home assistant systemd service config. I am running HA on debian 9, and my mqtt server is on another debian server. If your system does not match then this may or may not work for you.

[Unit]
Description=Home Assistant
After=network-online.target

[Service]
Type=simple
User=homeassistant
ExecStart=/home/homeassistant/homeassistant/bin/hass -c “/home/homeassistant/.homeassistant”

[Install]
WantedBy=multi-user.target

Thank you. I also have MQTT on a separate server on the same subnet and was experiencing the exact issue your described in the first post. I bumped things around a bit and it seems, for me as least, the operative statement to get home assistant and MQTT work together after a boot was adding the line:
Wants=network-online.target
to the systemd service config.