Run script file before startup

Is there a way to run a python script before core container start with HAOS?

The problem is with a specific integration that causes the HA container to freeze during restart due to specific entries added to the integration.

Disabling the entries (in core.config_entries) before restart works, but I need to do this automatically for unscheduled restarts (e.g. power interruption, OS restart, etc.) so it completes the restart.

I’ve written a simple python script and tested it that will disable the specific entries, but don’t know how to automatically execute it before a core restart.

I have posed a similar question, i.e. if one could de/prioritise during startup but this was just for the custom_application…got no positive answer. One ‘solution’ for me was to add dependencies so that it would only kick off when (in my case) ‘person’ and ‘zone’ were started.
I would not tinker with config_entries tbh.
Another option (not tried yet) is to deactivate things when HA goes down and activate it few minutes after start. Both can possibly (?not sure for your situation) via automation when you gracefully restart HA

Per definition, no.

Solve the root cause rather than scratching your head trying to fix the symptoms :wink:

If the integration is builtin (I guess not), open a ticket.
Assuming this is a 3rd party, what does the author have to say about that issue?

Edit: Oh, I forgot the mandatory meme :joy:


Solve the root cause rather than scratching your head trying to fix the symptoms

I assure you that an ongoing and exhaustive effort has been made with the author of the integration to solve the root cause over the past 4 months with no luck.

I’m just looking for a work-around from waking up (or worse, being remote) to find HA in a suspended state and the only way to recover is to use SSH to manually edit core.config_entries, then stop/restart the container.

My only other option is to just not use the integration.

If you are not the only one with this issue then maybe this would indeed be the solution. Maybe you can explain a but more details … e.g. what this integration exactly is (name?) and when/why it hangs.
Also why would the proposed ‘disable integration on shutdown’ and ‘enable after start’ not work ?

1 Like

A bit complicated, but here it goes.

It’s the Google Nest integration. After adding one entry (i.e., one home with one or multiple thermostats), the integration works fine and unscheduled restarts work fine.

Note: unscheduled restarts means that it is an unexpected restart due to a power disruption, OS restart, HA automated restart (e.g. when an update is available), etc. So, many times there is no way to predict this event, thus no way to take any action before it like ‘disable integration on shutdown’.

After adding additional entries to the integration (which requires a unique Google Cloud account/project for each home with its own thermostat(s)), the integration still works fine… until a restart.

At that point, the restart fails during integration loading with only some of the integrations fully loaded and essential parts of the UI unavailable/unaccessible (like devices/services). That means even the restart option (e.g. safe mode) from the UI is unavailable.

The only way to recover is to modify core.config_entries (I use Samba), then using SSH to restart the container (i.e., ha core restart).

I appreciate the interest in the root problem, but my original request was how to help with the work-around. Of course, would be grateful for any root-cause solution suggestions also :wink:.

Root-cause is always where to search first but I agree this may not always see a solution.
With power interruptions… yes, the graceful shutdown will not work unless you add a UPS (more budget needed). OS should not restart uncontroled though.
A not helping comment: I use docker so have easier access to any of those config files, not sure if you would have access to HAOS when it ‘hangs’ like above but I guess not seieng your question.
So…does not seem that this will lead to a workaround, maybe someone else shows up with a great idea

My deployment is Windows 11 using VirtualBox. The HA installation for this uses HAOS in Docker containers, so I am using docker.

Uncontrolled restart of the OS in this case could be a Windows 11 OS shutdown/restart (which is the most often scenario). Windows often deploys updates to the OS with automatic shutdown/restarts. When unattended, this happens without warning (I have since disabled Windows updates, but this comes at a risk).

Since I am using Docker and SSH, yes I have access to all files during a suspended state.

To clear the suspended state from an SSH terminal (using PuTTY since the HA UI is not available), I use either ha su repair, ha su restart then ha core restart or docker stop [container ID] then docker start [container ID]. This requires attendance, but I desire an automated solution to recover, thus my original question.

Putty comes with plink.exe. It is a command line SSH utility.

I think you can automate this process using plink.

1 Like

@jeffcrum How is a plink automation triggered from an HA restart process? Seems that PuTTY/plink would need to be running and manually (attended) triggered.

Plink is command line from your windows computer just like Putty is from your windows computer. You do the Putty commands in a windows cmd file or from batch Powershell.

You would invoke it with the commands you want to run over SSH to the HA instance.

I have not done it in a long time. But I am sure you can find more info with a Google search.

1 Like

@jeffcrum Thanks for the suggestion (I love learning something new that has real value)!

Based on it, I successfully created a powershell script that calls a plink script to call the python script and modify core.config_entries to disable the offending Nest entries (but not delete them). The plink script then restarts the HA docker container for a successful restart. A node red flow re-enables the Nest entries after startup then everything is back online (including the offending Nest entries).

The powershell script checks if HA is running. If its not, it means a restart was attempted but failed due to the Nest entries. It then calls the plink script, etc. I have the powershell script triggered every 5 minutes in a scheduled task.

Now I can finally maintain a reasonable uptime of HA automatic restarts while I wait for the integration author to fix the root problem (if that ever happens).


1 Like

That is awesome!

I really prefer to send people in the right direction and have them take off and run like you have. Very impressive!

Thanks for coming back and reporting that it was a good path and worked.