Adding comments tags to automations, scripts, scene etc

There is a potential workaround though, since templates can contain comments, and these are not removed by the UI. Add a template condition at the relevant point. Make it always return true so it can’t affect subsequent command execution, then put comments in there, like so: -

condition: template
value_template: |-
  {# COMMENT BLOCK FOLLOWS #}
  {# this is a comment #}
  {# and here is another #}
  {# as many as you like #}
  {{true}}
5 Likes

I would really like a simple solution to this.
I’m new here but I’ve managed to write some fairly complex automations to control my smart radiator valves.
I would very much like to be able to add comments to the yaml because:
I’m in the UK and we’re coming to the end of winter.
In six months time I expect to come back to these heating automations and I’d really like to have comments to remind me how they work :wink:

Young people [may] say, "Comments are for bad ‘coders’

But young people lack the wisdom to see their value.

I’ve been a programmer for over 50 years (yes, I started out as a “young person”:slight_smile: ) and I’ve learned to both treasure and insist on comments/remarks/descriptions in code. Not useless comments that describe what is being done - that should be self-evident from the code. Such comments just add noise. I mean comments that explain reasons and significant context that is not otherwise evident.

There’s a reason that all good development standards require comments (and often in a structured form). That’s because the code shows what is done (or is to be done) but it can’t explain with any clarity why it is done like that. Obviously, in large systems there is a heap of design & architecture documentation, but that is big picture stuff (and often can become detached from the source code when it’s distributed). Down at the detailed code level, the programmer needs to pass on to their successors the reason a particular approach is used and the constraints that that approach addresses (often outside the immediate context of the code in question).

In the context of HA Automations, I find it necessary to document not just the purpose of the automation – that can be done in a description – but also, for example, the reason a particular condition is there or the secondary event that a particular action may trigger.

It is indeed unfortunate that the migration to a “visual” interface in HA has downgraded the YAML to the extent that it has. It was particularly unfortunate that it was decided to “blow away” any comments in the YAML code on migration to the visual form without even so much as a warning (at event time, not in the release notes) that that would take place.

It seems that each generation of technologists has to re-learn the hard-learned lessons from the previous generation.

Brian

4 Likes

Pity that Scripts don’t have an UI for descriptions the same as Automations (thanks, Claudio!)

Nonetheless, the yaml for Scripts accepts the description field and doesn’t remove it.
Likewise:

alias: Random Lights
mode: single
description: >-
  See
  https://www.example.com/reference
sequence:
3 Likes

For reference, a similar FR which had been active for a while was closed, with a link to this FR as a duplicate. Fair enough, but I wanted to include the link here since that thread had been going for a while and may contain posts some readers here may find interesting.

Most of this and that other FR is implemented, just not in how you’d expect. There’s a description field as rudolff pointed out. Also, all actions, conditions, and triggers allow alias which is used to describe that “step” for the frontend. Variables allow jinja comments ({# ... #}) as well.

3 Likes

Good point, there are workarounds. This FR is still a good idea, but I suspect it would be hard to implement.

My own workaround (post #2 of the other thread) is not to use the UI for automations, except maybe just to try out something new.

Being that the YAML is converted to JSON when saved, then back again to YAML for editing, a compromised would be to store the original YAML file as-is in a text file, before converting it to JSON. When the user comes back to the YAML at a later date for editing, instead of converting the JSON back to YAML, simply retrieve the associated YAML text file and display it to the user. Nothing is going to change the JSON in the meantime, so the stored text file should always be up to date with the JSON. Readability of code is important. Dropping helpful comments which enhance the readability of config files should be avoided if possible.

4 Likes

Or simply adding fake JSON node for comment - something like 'comment':'comment from YAML'

4 Likes

The problem isn’t adding a comment or a description node to the json, it’s reading the comments in the yaml. Typical YAML parsers will not parse comments, they will just ignore them entirely, because that’s what the YAML specs say they should do:

Comments are a presentation detail and must not have any effect on the serialization tree or representation graph. In particular, comments are not associated with a particular node.

Of course that doesn’t mean a YAML parser couldn’t preserve them as an out-of-spec extension (so to facilitate format conversions or source formatting for example) and some probably do. But I guess the one HA uses doesn’t.

2 Likes

When developing automation that are related, or automation that relies on the execution of other automation or other external things (like nodered), it can be a mess to maintenance all of those things after some time or after feature changes.
A new comment textbox to be inserted in the automation, or a textbox added in existing automation’s blocks, could be useful to describe the why’s to the future yourself, looking at same automation/script in the future, of why you’ve done the things in that way.

A text string of some hundreds of characters I hope could be enough.

ps
I’m an HA user since 0.101 , so I have used also nodered to accomplish my ideas and now I’ve no time to migrate everything to HA.

Thank you!

I typically just rename blocks to be more descriptive. That way future me knows what current me is actually thinking. It’s not as good as a comment block, but it works for now.

1 Like

I put it all in the description block for paragraphs, otherwise you can add # or {# #} comments everywhere as long as you don’t save it in the ui editor, that strips all the things including spaces for efficiency in the storage when it parses the code to make sure it works then saves it in automaton.yaml

1 Like

I generally use the UI editor to create the automation, then move it to a my_automations.yaml file to tweak it and add comments. If future me really needs to use the UI for some reason, he can always copy and paste the code back to automations.yaml or right into the UI. Hasn’t happened yet.

Organising and annotating automations.

I do, but it’s not maintainable having a long name like “nodered flow X uses this value with event K that’s misaligned of N minutes”.

on next UI saving (that can happen, it’s easier changing via UI instead of doing SSH login to server and manually change automation), all the comments will be stripped out, so it’s a bit risky

Thanks, I guess I didn’t explain that well enough.

I have a second !include statement in my configuration.yaml which points to my_automations.yaml:

automation ui: !include automations.yaml
automation manual: !include my_automations.yaml

My “production” automations are in this second file, which HA can’t edit. It still loads and runs all the automations, but it can’t re-format them, erasing all the comments. No need to SSH in to edit, I use SMB.

I only use the UI to create new automations using logic I haven’t used before. Otherwise I just copy-and-paste from existing, working automations. Once I’ve got one working the way I like, it gets moved to the my_automations.yaml file. In a pinch, I can always move them back for more development work in the UI.

As I’ve said before, there are other ways to document your automations. This method was what was available when I started with automations, and it works for me. YMMV.

2 Likes

it’s the first time I see this smart use of include statement, very interesting, thanks!

Good enough for me for the moment, thank you for the idea.
But seriously though - I can’t see why this behavior exists in frontend files and not in backend files.