Creating a new automation with the visual editor really helps me. Starting from that base, when I need more logic in my automation I select “edit in YAML” from the top right menu and add it, and then click “save”. All of this works as expected, and I can use “Run” to check my code.
The problem I’m having is that the code sometimes reverts to what the visual editor had originally produced. This is very disconcerting. Am I doing something to cause this, or is this a bug? Does “save” rewrite the automations.yaml file on disk, or just update the in-memory copy of the automation, or both? Do I need to stop using the automations editor altogether once I write anything that won’t fit?
IMPORTANT NOTE: While I got some useful information here, most of my problem was simple operator error. I was trying to use template code like you would use to create a sensor in an automation where you can only code in YAML. “- if:” works in an automation and “{% if” does not.
When you click the Save button, whatever modifications you created (whether in YAML or Visual mode) are validated and, if there are no syntax errors, formatted according to the Visual Editor’s rules, saved to automations.yaml and reloaded (to activate the latest version of the automation).
To avoid having the Visual Editor change the format of your automation, you can create the automation using a text editor (like Visual Studio Code). It allows you to do things in YAML that the Visual Editor doesn’t support (like comments). However, you will have to keep these automations in a separate file (or files) from the default file used by the Visual Editor (automations.yaml).
The following documentation link explains how to split the configuration of automations that are created “manually” (with a text editor) and those created using the Visual Editor.