How to create a modular automations.yaml file

I’m trying to have well organized automation files. For example in the /config directory I would like to have config/automations/light_automations.yaml and include that in /config/automations.yaml.

Here is an example of the automations.yaml I want but does not work:

# Main automations file

# Include other automation files from a subdirectory
!include_dir_list automations/

# Include light automations
!include automations/light_automations.yaml

# Other categories can be included similarly
# !include  notify.yaml
# !include climate.yaml

I get an error at the !include line.

could not find expected ':' 

Use a different file…

From: Automation YAML - Home Assistant

The following links to an example that shows how to set up configuration.yaml to properly merge both automations.yaml and a directory you can manually manage: Splitting up the configuration - Home Assistant

1 Like

Here’s an example which works for me:

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

The first one allows the automation editor to work. I’ll sometimes use that to start building an automation.

Once I have that, I’ll move it to my_automations.yaml. Now I can format it the way I want, make changes and add comments without having the editor change it all back.

Presumably, you could add other lines and organize your automations into multiple files:

automation lights: !include automations_lights.yaml

etc.

Or if you want different yaml files for different automations all included from an automations directory you do this:

# only used for GUI automations
automation: !include automations.yaml

# All of my yaml automations go here in this directory
automation yaml: !include_dir_merge_list automations/

then under the automations folder I have:

automations_misc.yaml
automations_lighting.yaml
etc

I also use packages a lot. And I do mean a lot.

2 Likes

Taken a step further, here is a list of how I am set up:

# ******************************************************************************
#    Included Files
# ******************************************************************************
alarm_control_panel: !include includes/alarm_cp.yaml
anniversaries: !include includes/anniversary.yaml
automation manual: !include_dir_merge_list automations
automation ui: !include automations.yaml
# ^^ uses both the UI editor and YAML mode
binary_sensor: !include_dir_merge_list binary_sensors
camera: !include_dir_merge_list cameras
calendar: !include includes/calendars.yaml
command_line: !include includes/command_lines.yaml
#  - binary_sensor: !include_dir_merge_list includes/command_line/binary_sensor/
#  - sensor: !include_dir_merge_named includes/command_line/sensor/
counter: !include includes/counters.yaml
cover: !include includes/covers.yaml
# fan: !include includes/fans.yaml
group: !include_dir_merge_named groups
# influxdb: !include includes/influxdbs.yaml
input_boolean: !include includes/input_booleans.yaml
input_button: !include includes/input_buttons.yaml
input_datetime: !include includes/input_datetimes.yaml
input_number: !include includes/input_numbers.yaml
input_select: !include includes/input_selects.yaml
input_text: !include includes/input_texts.yaml
# light: !include includes/lights.yaml
# media_player: !include includes/media_players.yaml
multiscrape: !include includes/multi_scrape.yaml
mqtt: !include includes/mqtt_sensors.yaml
notify: !include_dir_merge_list notify
# proximity: !include includes/proximity.yaml
recorder: !include includes/recorders.yaml
# scene: !include_dir_list scenes > moved to scripts for simplicity
script: !include_dir_merge_named scripts
sensor: !include_dir_merge_list sensors
shell_command: !include includes/shell_commands.yaml
# siren: !include includes/sirens.yaml
# sql: !include includes/sql_queries.yaml
switch: !include includes/switches.yaml
template: !include_dir_merge_list templates
timer: !include includes/timers.yaml
tts: !include includes/txt2spk.yaml
utility_meter: !include includes/utility_meters.yaml
var: !include includes/vars.yaml
# webostv: !include includes/webostvs.yaml
weather: !include includes/weather_cfg.yaml
yahoofinance: !include includes/yahoo_finance.yaml
#  ---------------------------------------------------------------------------
# !include_dir_merge_list will return the content of a directory as a list by merging all files (which should contain a list) into 1 big list. !include_dir_merge_list (must start with a hyphen)
#    automation, notify, sensor
#
# !include_dir_merge_named will return the content of a directory as a dictionary by loading each file and merging it into 1 big dictionary. !include_dir_merge_named (does not start with a hyphen)
#    group, script
#  ---------------------------------------------------------------------------

Thanks a bunch! I see now “include_dir_merge_list” allows us to include all .yaml files in a directory.

1 Like