Startup Sequence of Home Assistant and Mosquitto on OSX

I run Home Assistant on OSX. I also run mosquitto on the same machine.
If the machine restarts, Home Assistant appears to start before mosquitto, so my mqtt sensors and covers come up in “unknown” state even though the last state is retained on the MQTT server.

It doesn’t appear that dependencies are supported with launchd like they are with systemd.
Any thoughts about how I can delay the Home Assistant start until after MQTT is available?
Alternately, would it be a good idea for H.A. to populate it’s MQTT sensors when MQTT becomes available?

You could create a simple Bash script to depend on one another, and you would launch that instead of mosquito and HA separately.

It would look something like this:
I don’t use MAC, and completely spitballing here, you may need to test

#!/bin/bash

# Start mosquito here. It will block the script from starting
systemctl start mosquito

# Sleep in seconds if needed
sleep 60

# Start HA
systemctl start [email protected]

I do have the same issue on a PI3. HA will start before mosquitto and I get configuration error in UI. If I restart HA, all is fine.

Tested my script above on my Pi and it works, WYSIWYG

If you really wanted to, you could point Systemd to that script too

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

[Service]
Type=simple
User=%i
ExecStart=/bin/bash /location/to/my/script/start_both.sh

[Install]
WantedBy=multi-user.target

We are getting away from the original poster’s mac here, but the normal method of ensuring the order using systemd is After=

For instance, my service file starts HA after influxdb and mosquitto

# 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

But that doesn’t help on a Mac.

1 Like

Shouldn’t HA be able to restore the connection when Mosquitto becomes available?
@treno what do the HA log says? If you leave it alone for a couple of minutes, do you see other connection retries or after the first failed one there are no more attempts?

Yes, it does restore the connection, but all the MQTT devices end up in an “unknown” state until the next time the source devices publish a new state, or until the next time Home Assistant is restarted.

Have you configured the Last Will for your devices?

This is my configuration for Sonoffs` with Tasmota, see the availability topic and payload available/not available:

  - platform: mqtt
    name: "Sonoff 2"
    state_topic: "stat/sonoff-2/POWER"
    command_topic: "cmnd/sonoff-2/POWER"
    availability_topic: "tele/sonoff-2/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

The last will is a retained message. This way HA should recognize that the devices are available when it connects to the broker, because it gets the “Online” message.

I’ll give this a try. Thanks

Please note that this only ensures that this service is started after the listed services have been started, but does not guarantee that any of them is in any useful state. Mosquitto may have been started already, but may not accept connections from HA or devices yet.

Thanks. Not using systemd. This is OSX.

I’m running Home Assistant, mosquitto, influxdb, grafana and a few other random related daemons on my Mac mini running macOS. Oh, and just for fun, it also has OpenZFS for OSX installed on it, too.

I had two problems trying to use launchd and sequencing the startup of daemons. The first was real annoying; launchd would start infuxdb before ZFS finished importing/mounting all the ZFS pools. Influxdb then saw no databases yet because the ZFS dataset wasn’t mounting in time… feh.

And then to a lesser extent the sequencing all the rest…

What I eventually ended up doing it using http://supervisord.org/ to manage all of my various home automation tasks. It was adequate to specify a starting order for each of the various daemons. I did, however, hack in a script to wait until all my ZFS pools finished importing upon boot, however.

You’d think there would be an easier way to solve this problem, but apparently none that I could find. Now, the only thing that launchd starts is supervisord and you can monitor/start/stop/restart from there. As an added bonus, supervisord can capture stdout/stderr and save the last number of lines for you to easily examine.

1 Like