Python Script to validate your automations.yaml

I’ve wanted this for a long time, and thanks to Claude and a few nights back and forth iterating and refining, I’m pretty much there.

Link to Gist

I’d really welcome improvements and feedback on this.

  1. Basic Structure Checks:
  • Ensures each automation has a trigger and action section
  • Checks that the YAML is properly formatted
  • Validates that triggers/conditions/actions are either single items or lists
  1. Trigger Validations:
  • Checks for required fields based on trigger type (e.g., platform, entity_id)
  • Validates trigger platforms (state, mqtt, event, numeric_state, etc.)
  • For state triggers: checks entity IDs exist
  • For time triggers: validates time formats
  • For MQTT triggers: ensures topic is present
  • For sun triggers: validates event types (sunrise/sunset)
  • For numeric triggers: checks for valid above/below values
  • For template triggers: checks for Jinja2 delimiters
  1. Condition Validations:
  • Validates condition types (state, numeric, template, etc.)
  • Checks required fields for each condition type
  • Validates template syntax in template conditions
  • Checks numeric ranges in numeric conditions
  • Validates time formats in time conditions
  1. Action Validations:
  • Validates service calls have proper domain.service format
  • Checks for required fields in different action types
  • Validates data and data_template structures
  • Checks for proper formatting of service templates
  • Validates complex actions like repeat and choose
  • Checks for proper MQTT topic/payload structure
  1. Toggle Pattern Detection:
  • For certain device types (covers, media players, etc.)
  • Checks if there are matching on/off state triggers
  • Warns about missing toggle counterparts
  1. Entity Reference Tracking:
  • Tracks all referenced entities
  • Checks for undefined automations that are referenced
  • Validates entity ID formats
  1. Special Case Validations:
  • Checks for self-references in templates
  • Validates time patterns and formats
  • Checks for impossible numeric conditions
  • Validates event types for home assistant events
  1. Service Call Validations:
  • Validates MQTT publish actions have topics
  • Checks for transition times in light brightness changes
  • Warns about generic notify services
  1. Domain-specific Checks:
  • Light service validations
  • MQTT specific validations
  • Home Assistant event validations
  • Device trigger validations
  1. Warnings are generated for:
  • Unknown fields in triggers/conditions/actions
  • Missing optional but recommended fields
  • Non-standard usage patterns
  • Potential improvements (like adding transitions)
  1. Critical errors are flagged for:
  • Missing required fields
  • Invalid formats
  • Impossible conditions
  • Malformed structures

The script also categorizes automations based on their content, primarily looking at:

  • Service domains
  • Entity types
  • Action types
  • Common patterns in naming and usage

It’s not perfect…

Known issues:

  • Doesn’t handle folded YAML at the moment - it converts it to a single line
  • Strips single quotes from MQTT payloads - not a problem, I don’t think, but not ideal.
  • Probably other thing. Please test and report back.

To run, download it, then call python sorter.py where automations.yaml is in the same directory as the script. Do this locally rather than on the server!!!

2 Likes