After some time doing a lot of stuff in configuration.yaml, I found that I needed to make a much more clean setup, where things were split up in multiple files instead of all automations being in automations.yaml etc. Basically my configuration.yaml and automations.yaml files contained thousands of lines making them confusing to look at or modify.
I read the documentation regarding splitting up the configuration.yaml file into multiple files (official documentation here), which seems both logic and easy. Before I did it I however sad down and made a list on how to do it, for example by the use of folders.
I split up my usage in the following to types:
- File Merge
This is used with the !include_dir_merge_list. It will find all files in the folder and merge them together to one big list, meaning that I can place multiple files in a folder and Home Assistant will find the files and merge them without me needing to change the configuration.yaml file. - File Reference
This is where I use a folder containing multiple files, but where I point to a specific file in the folder in my configuration.yaml.
This covers a lot of my integrations, like IHC, HomeKit etc.
Type | Name | Usage |
Folder | automations/ | File Merge |
Folder | groups/ | File Merge |
Folder | integrations/ | File Reference |
Folder | lights/ | File Merge |
Folder | scenes/ | File Merge |
Folder | scripts/ | File Merge |
Folder | sensors/ | File Merge |
Folder | setup/ | File Reference |
Folder | switches/ | File Merge |
Folder | variables/ | File Reference |
After this I created the folders through Visual Studio Code (one of my add-ons installed), took the content from my existing files and placed them in the different folders.
Below you can see some chosen examples on how my files look like afterwards:
configuration.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
################################### ## Home Assistant Core ################################### homeassistant: !include setup/homeassistant.yaml http: !include setup/http.yaml config: system_health: updater: ssdp: zeroconf: frontend: recorder: !include setup/recorder.yaml history: !include setup/history.yaml logbook: !include setup/logbook.yaml map: mobile_app: person: !include setup/person.yaml sun: tts: !include setup/tts.yaml ################################### ## Variables ################################### input_boolean: !include variables/input_boolean.yaml input_datetime: !include variables/input_datetime.yaml input_number: !include variables/input_number.yaml ################################### ## Components ################################### ihc: !include integrations/ihc.yaml vacuum: !include integrations/vacuum.yaml notify: !include integrations/notify.yaml homekit: !include integrations/homekit.yaml # hue: !include integrations/hue.yaml tplink: !include integrations/tplink.yaml garbage_collection: !include integrations/garbage_collection.yaml alarm_control_panel: !include integrations/alarm_control_panel.yaml unifi: !include integrations/unifi.yaml ################################### ## Includes ################################### sensor: !include_dir_merge_list sensors/ group: !include_dir_merge_list groups/ automation: !include_dir_merge_list automations/ script: !include_dir_merge_list scripts/ scene: !include_dir_merge_list scenes/ switch: !include_dir_merge_list switches/ light: !include_dir_merge_list lights/ # zone: !include_dir_merge_list zones/ |
lights/room-entrance.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
################################### ## ENTRANCE LIGHTS ################################### ################################### ## Switches ################################### - platform: switch name: 'entrance_light' entity_id: switch.entrance_light - platform: switch name: 'entrance_floor_star' entity_id: switch.entrance_floor_star |
lights/room-livingroom.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
################################### ## LIVINGROOM LIGHTS ################################### ################################### ## Switches ################################### - platform: switch name: 'livingroom_ph_lamp' entity_id: switch.tplink_ph_lampstick - platform: switch name: 'livingroom_floor_lamp' entity_id: switch.tplink_floorlamp |
integrations/tplink.yaml
1 2 3 4 5 6 7 8 |
################################### ## TP-LINK INTEGRATION ################################### discovery: false switch: - host: !secret tplink_host_livingroom_phlampstick - host: !secret tplink_host_livingroom_floorlamp - host: !secret tplink_host_garage_rackcabinet |
The examples shows two files that will be merged to one list (lights/room-entrance.yaml and lights/room-livingroom.yaml), but you will also see an example where I referer to a specific file (integrations/tplink.yaml).
You could say that if the files in a folder is for the same integration, then you should use file merge, and if the files in a folder is for different integrations, then you should use file reference. For example in my integrations-folder I have different files for different integrations like IHC, HomeKit, iRobot, while in my automations-folder I have files for the automation-integration.
Furthermore you should remember that in each file you should not start by defining the integration. It’s already defined in configuration.yaml, for example:
1 2 |
ihc: !include integrations/ihc.yaml automation: !include_dir_merge_list automations/ |
What I have done in my automations-folder is that I have created files per room, so it’s easier to have a file-based overview. All related automations for your livingroom is in the same file, while all related automations for our entrance is in another file. This makes a much more clear overview and easier to find the YAML code you are searching for, add extra code og modify existing code.
I didn’t do this from the beginning, and I regret that today. I took some time changing all of this from my old method to this new method, but I was worth the time. Everything is much easier now.