Automation for configuration check

Is there a way to run automation to check the configuration?

The service call you are looking for is:

homeassistant.check_config

If you go to Developer Tools > Services, you can scroll through all the available services in the Service field:

2 Likes

Thanks @123

Hi,

I’m trying to do something like this:

  1. new version, automation fires and checks for errors using the example above.

  2. if there are no errors, trigger the update.
    2.a) once update is done, send a success push notification

  3. if check fails, send a failure push notification

I think I can make most of it, but the step to trigger an automation depending on the config check result is missing. Can this be done? I could not find any state or event from the config Check script.

Any suggestions?
Thanks

There is none. In general, service calls work like subroutines, not functions, because they return nothing.

Here is my automation for config check and proceed with HA backup and restart on condition config check has no errors.

#template sensors
      invalid_config_check:
        value_template: >-
          {%- if states.persistent_notification.invalid_config.state == 'notifying' -%}
            invalid
          {%- else -%}
            valid   
          {%- endif -%}
      config_check:
        value_template: >-
          {%- if states.persistent_notification.homeassistant_check_config.state == 'notifying' -%}
            invalid
          {%- else -%}
            valid   
          {%- endif -%}

#Automation:
- id: '123'
  alias: Home restart
  description: Restart HA on condition Config is valid
  trigger:
  - platform: time
    at: 03:00:00
  condition:
  - condition: time
    weekday:
    - sun
  action:
  - service: homeassistant.check_config
  - delay: 00:00:40
  - alias: Restart on condition
    choose:
    - alias: validate and restart
      conditions:
      - condition: state
        entity_id: sensor.config_check
        state: valid
      - condition: state
        entity_id: sensor.invalid_config_check
        state: valid
      sequence:
      - service: hassio.addon_stdin
        data:
          addon: 15d21743_samba_backup
          input: trigger
      - delay: 00:05:00
      - service: notify.mobile_phone
        data:
          title: HA Restart Notification
          message: Configuration is valid, HA is going to restart.
          data:
            ttl: 0
            priority: high
      - delay: 00:00:05
      - service: homeassistant.restart
    default:
    - service: notify.mobile_phone
      data:
        title: HA Restart Notification
        message: Home Assistant not restarted, invalid configuration identified. {{state_attr('persistent_notification.invalid_config',
          'message')}} {{state_attr('persistent_notification.homeassistant_check_config',
          'message')}}
        data:
          ttl: 0
          priority: high
  mode: single
4 Likes

That’s an interesting workaround for the service call not returning a value. It relies on checking the status of the persistent notification that (may have been) produced by the service call.

What are you using to clear the persistent notification’s ‘notifying’ status? Are you manually reading/deleting the notification to clear it?

1 Like

Thanks!

I changed the template to this in the hope it is correct. I never worked with templates before:

template:
  - sensor:
    - name: invalid_config_check
      state: >-
        {%- if states.persistent_notification.invalid_config.state == 'notifying' -%}
          invalid
        {%- else -%}
          valid
        {%- endif -%}
  - sensor:
    - name: config_check
      state: >-
        {%- if states.persistent_notification.homeassistant_check_config.state == 'notifying' -%}
          invalid
        {%- else -%}
          valid
        {%- endif -%}

I suggest creating a single Template Binary Sensor. It will be on if there’s either persistent_notification present (otherwise, off).

template:
  - binary_sensor:
    - name: invalid_config_check
      state: >-
        {{ states.persistent_notification.invalid_config.state == 'notifying' 
           or states.persistent_notification.homeassistant_check_config.state == 'notifying' }}
2 Likes

I’m trying to understand how the persistent_notification works… do I need to manually create states.persistent_notification.invalid_config or is this being created automatically by the config_check plugin? I’m using the official Check Home Assistant configuration Home Assistant Add-on, but I never see any persistent notification created by it.

I feel its more safer to clear the errors manually than using automation (hope there is one available) so I can troubleshoot and fix them before automation triggers for config check again.

This is how it should be in your config.yaml file

that’s a great suggestion.

Make an invalid entry in config file and save the file, then run the config check or run service call - ha-config_check - this will generate persistent_notification entity which you can see in developer tools - states.