HA keeps changing the formatting of my automation

Hello,

I am on Home Assistant 0.114.4 Running Ubuntu 19.10 with a python virtual environment and am having an issue where HA decides to reformat my automation making it less readable;

I’ve my automation like this

- id: '2'
  alias: WC light TimeBased
  trigger:
    platform: state
    entity_id: binary_sensor.shenzhen_neo_electronics_co_ltd_battery_powered_pir_sensor_v2_sensor_2
    to: 'on'
  action:
    service: light.turn_on
    data_template:
      entity_id: light.wclicht
      brightness_pct: >
        {% if now().hour in [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] %} 
          100
        {% elif now().hour in [22, 23] %} 
          70
        {% elif now().hour in [0, 1, 2, 3, 4, 5, 6, 7, 8] %} 
          30
        {% endif %}

After a while I look at my Automation.yaml again and see that the formatting has changed to this;

- id: '2'
  alias: WC light TimeBased
  trigger:
    platform: state
    entity_id: binary_sensor.shenzhen_neo_electronics_co_ltd_battery_powered_pir_sensor_v2_sensor_2
    to: 'on'
  action:
    service: light.turn_on
    data_template:
      entity_id: light.wclicht
      brightness_pct: "{% if now().hour in [9, 10, 11, 12, 13, 14, 15, 16, 17, 18,\
        \ 19, 20, 21] %}\n 75\n{% elif now().hour in [22, 23] %}\n 50\n{% elif now().hour\
        \ in [0, 1, 2, 3, 4, 5, 6, 7, 8] %}\n 30\n{% endif %}\n"

This kind of triggers me, as it less readable and I have to search for the light % if I want to change something.
How can I prevent this?

Did you edit the automation in the UI?

From the : -

I’m guessing - yes !

Yes and no. When I started working with HA I used the gui alot. But the GUI adds unnecessary attributes which can also make the automation work incorrectly from my experience. Since then I’m slowly recreating all my automations just using a text editor and only using neccesary attributes.

The ID attribute is probably one of them but I thought it was kinda handy so I kept it in. HA gives a long ID number and I changed the automation that I recreated to a short ID number. 1, 2, 3 etc.

I did not touch this automation via GUI or automations.yaml when HA change the formatting again.
Should I delete that one?

Okay,
For the HA GUI Automation editor to access your automations, they need to be in \config\automations.yaml
So if they are not in there they won’t be read/messed with (but you need an entry to point HA at them in order to be able to run them)
I use packages and only use the GUI to look up “device” info - then back out quickly

I think @finity uses both the GUI and packages (I really recommend packages) so you could get the best of both worlds if you desire. I can’t find his post where he tells you how to do this but I’m sure he’ll be happy to help out

So you could create your perfect automation in the GUI, run it for 6 months to ensure no bugs, cut it from automations.yaml, paste it into ‘a package’, then edit it to make it readable, add comments etc. and the only “editing” that will be done to it, will be done by you.
The same can be true with scripts

I was thinking about making a folder per room where the automations reside. But I still have to look into that. Yes all my automations are currently in /home/homeassistant/.homeassistant/automations.yaml

Dunno about a folder per room but whatever floats your boat
I have a \config\packages folder and then just 3 folders off that - \lights, \switches and \misc
The package files under those are room specific for me but I also have some general ones too like aa_ha_gen.yaml in the \config\packages folder which covers general global stuff like noise permissives, outside sun elevation periods, customisations, some general helpers (booleans, texts, numbers etc.) used in general testing

I think it’s best to read the docs about packages :slight_smile:

If all your automations are in automations.yaml, I think HA still applies some formatting etc. to them automatically, but I’m not sure on this one.

Here’s what I just tested (I don’t use UI automations) to have UI automations and YAML automations at the same time:

In configuration.yaml:

# Core configuration
homeassistant:
  packages: !include_dir_named packages/
automation: !include automations.yaml

Then in the packages folder I have files like office.yaml, multiroom_audio.yaml and whatever, where I configure the manual automations. All automations configured through the UI are automatically stored in automations.yaml. I can still edit them in the UI and the manually configured automations in the packages are untouched.

1 Like

I do use packages but stay as far from the GUI for other things as I can except when I’m forced to use it or for testing things while helping others.

Here is my config for using both GUI and for yaml:

homeassistant:
  packages: !include_dir_named packages

## only used for the testing of the GUI editors
automation: !include automations.yaml
script: !include scripts.yaml

## All of my regular automations go here
automation yaml: !include_dir_merge_list automations/

And of course all of the automations that are in packages are included under the “homeassistant:” key as shown above.

1 Like