Set a delay for HASS to Start After Server Restart (systemd)

Does anyone run a UniFi controller inside an LXC container? I want Home Assistant to be the last to start after my server is fully booted and online.

The first script is what I use:

With that said, is it possible for Home Assistant to be executed the last after all services are started? I want to set a delay for 10 seconds so that presence detection can work properly; otherwise, I get “Not home” for both of my Nexus 6P and Nexus 7 despite both of them being connected to the WiFi network.

This might be of help. You can use a Unit Directive to control the order or even base it on a dependency.

1 Like

I used the browser’s find feature to look for “delay” and I found OnStartupSec under [Timer] section. So now this is what I have so far.

[Unit]
Description=Home Assistant
After=network.target
 
[Service]
Type=simple
User=%i
ExecStart=/usr/local/bin/hass
 
[Timer]
OnStartupSec=15
 
[Install]
WantedBy=multi-user.target

I will now do a system restart to see how this works. Thanks.

Update after a couple of minutes later: It seems the OnStartupSec and OnBootSec are the same regardless of whether I set it to 15 or 30 seconds. Changing to lxc.service in the After= directive to see how it goes…

After a couple of minutes later…

Well, I may simply restart Home Assistant manually since the presence detection will simply behave as showing “not home.”

Thanks for your help.

Hey,

Bit of a late reply but I have a different solution I just came up with today that makes use of oneshot mechanics and simple curl to check if unifi service is available

Create the following /etc/systemd/system/hass-pre.service file (or similar)

[Unit]
Description=Home Assistant helper for UniFi
After=network-online.target unifi.service
Wants=unifi.service

[Service]
Type=oneshot
User=%i
# Enable the following line if you get network-related HA errors during boot
#ExecStartPre=/usr/bin/sleep 60
# Use `whereis hass` to determine the path of hass
ExecStart=/bin/bash -c 'while true; do curl -ksI -m 5 https://0:8443/manage/account/login?redirect=%2Fmanage | grep "200 OK" > /dev/null; if [ $$? == 0 ]; then break; fi; sleep 5; done;'
RemainAfterExit=yes

Then add hass-pre.service to your After= and Wants= inside the main homeassistant systemd file.

Oneshot makes this call block dependent services (hass) until it’s done with the script and RemainAfterExit makes it remain active after the script is done.

So the fake “service” will appear as activating as long as the script is in the loop and will be “activated” when it exits.

Adjust values accordingly. That one-liner simply queries a known URL that responds with “200 OK” header every 5s until it gets the correct response. I’ve tested and it works fine :slight_smile:

Let me know!

EDIT: If you’re not running unifi.service in the same environment, you can remove it from Wants= and After= and just rely on curl result

EDIT2: For good measure also add -m 5 to curl parameters so it doesn’t wait too long for each response

1 Like

@dark_skeleton, can you please explain this? I do not understand it. I am new to this.

In case anyone comes across this thread, if you need to delay starting HA you can create a ‘timer’ file.

The hass service file should be the first 2 sections

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

[Service] Type=simple
User=%i
ExecStart=/usr/local/bin/hass

Then create a second .timer file in the same directory with

[Timer]
OnStartupSec=15

[Install]
WantedBy=multi-user.target

You then need to enable the .timer control file (as opposed to the .service file) and the main service file will not now start for 15 seconds.

4 Likes