Preserve YAML formatting and comments when editing automations via UI

Dear Home Assistant developers,

I prefer working with YAML files for my automations due to the flexibility and readability they offer. However, I’m experiencing a significant issue when modifying automations through the UI:

The Problem:
When I edit an automation that was originally written in YAML (with careful formatting, blank lines, and comments) via the UI and save it, all formatting is lost. The automation gets rewritten as a long, unreadable blob without:

Blank lines between different sections

Comments that explain what certain parts do

Proper indentation and structure

Example:

yaml

BEFORE (readable):

alias: “Evening living room lighting”
description: “Turn on lights at sunset”

Sunset trigger

trigger:

  • platform: sun
    event: sunset

Lighting actions

action:

  • service: light.turn_on
    target:
    entity_id: light.living_room_table
    data:
    brightness: 200

Slow dimming over 30 minutes

  • service: light.turn_on
    target:
    entity_id: light.living_room_ambient
    data:
    brightness: 150
    transition: 1800

AFTER UI edit (unreadable):

alias: Evening living room lighting
description: Turn on lights at sunset
trigger:

  • platform: sun
    event: sunset
    action:
  • service: light.turn_on
    target:
    entity_id: light.living_room_table
    data:
    brightness: 200
  • service: light.turn_on
    target:
    entity_id: light.living_room_ambient
    data:
    brightness: 150
    transition: 1800
    Consequences:

Automations become difficult to maintain

Comments (and thus context) are lost

Collaboration becomes more challenging

Finding specific components takes more time

Desired Solution:
I would like to propose that the UI respects the existing YAML structure when automations are saved. Ideally:

Preserve blank lines between logical sections

Maintain comments in their original positions

Keep the original indentation where possible

Alternative Solution:
A setting option to choose between “compact YAML” (current behavior) and “readable YAML” (formatted with comments).

This would significantly improve the workflow for many users who utilize both YAML and the UI!

This has been debated forever in the past.
As far as I know it’s not possible, I believe it’s because the yaml is converted to json.

But if you want to make a point about formatting then maybe you should at least format your post correctly using the code block button </b> .

2 Likes

this one has been discussed many times since HA started storing yaml into json-based storage. I believe the OP can find requests in WTH section.

It’s technically possible while not in current version of HA. Requires yaml parser that treats comment blocks as data, instead of reaping them off. Even Petro started mentioning such an option as possible one some months ago.

It’s also when blueprints are imported and any time the system parses the YAML from an outside source.

Annoying but you just have to use work-arounds.

For instance use a lot of aliases in your Automations & scripts, those are saved.

The forum is not the place for feature requests. This is: home-assistant · Discussions · GitHub

However as previously noted this request will not be implemented for technical reasons. So you have two options:

  1. Write in YAML only where you can comment to your heart’s content.
  2. Use the alias option to store comments for every block in the UI editor.
1 Like