Prevent invalid configuration or notify about it?

Hi there,

I’m currently looking for a way to prevent an invalid configuration as I just experienced an unfortunate problem that prevented my HA from starting up.

It started with me doing a change to my config and pushing it (I’ve set it up as a Git repository) but not checking anything as I thought it was fine (spoiler: it was not). A rather new automation of mine shuts down my server and also the power strip for my PC during the night for electricity saving purposes. As you can imagine, the next morning I couldn’t start my PC because my HA was not starting.

So I’m looking for a way to either include a configuration check as a pre-commit hook for the repository or to notify me when the check detects an invalid configuration. Is there any way I can do that?

That would be useful. Here’s a link that may help. If you get it figured out please post back.

How are you restarting HA after you push your changes?

Do you use the built-in restart service or some other way?

I guess triggering the check via automation would be possible and I could write the result in some file but I’d have to know in the automation whether it succeeded or not. Not sure how I’d find that out

I’m using the “git-pull” addon. It has a restart feature and also checks the config before restarting. The restart that got me was my own from my electricity saving automation because I don’t have a way to check the config yet. That’s why I made this post :smiley:

So you restarted manually using the built-in restart service from the dev tools->yaml page and didn’t use the git-pull add-on that time?

If that’s the case then the system should do an automatic config check before it restarts and won’t let you restart if it finds an error.

The reason I’m trying to find out exactly what happened is because your issue may be related to what I and other users have seen recently that the config checker isn’t finding errors before restarting/reloading automations.

Ah sorry I should’ve been more precise. What my automation does is triggering my server to shutdown to save electricity. I don’t really have anything preventing me from doing that. The addon didn’t restart due to the invalid config but my server basically forced it to shutdown :smiley:

Oh, OK. I think I understand now.

the config check found an issue and prevented the restart but later the automation killed the power to the server and because of the aforementioned unfixed error after the power was restored HA didn’t restart.

is that about correct?

That’s correct ye

1 Like

Okay I think I found a solution (haven’t tested it in my automation though). So I haven’t found an easy way to check the config and use the result of that check in an automation. My idea was to use the shell_command feature because the HA CLI can be used for the config check. The command looks like this

  check_core_config: 'curl -sSL -H "Authorization: Bearer $SUPERVISOR_TOKEN" -X POST http://supervisor/core/check -o "/config/logs/core-config-check.json"'

The reason I didn’t use the command ha core check directly is that apparently it doesn’t work, at least not with HA OS. Luckily the supervisor has an API we can use. The result will be a JSON that I write to a file.

The next step was to parse the file and create a sensor I can check in my automation:

  - platform: file
    name: Core Config Validity
    file_path: "/config/logs/core-config-check.json"
    value_template: >-
      {% if "\"ok\"" in value %}
      {% elif "\"error\"" in value %}
      {% else %}
      {% endif %}

With this setup I can trigger the check with my shell_command in the automation and wait a bit before I check on the sensor that will get updated after the check is done.

Maybe I’ll find a way to run the check after the git pull (or alternatively every x minutes with an automation) just to keep the sensor up2date

Maybe a bit over the top but if this allows me to always safely start my HA it’s fine :smiley:

fyi @PeteRage

1 Like