Internet

How to organize Home Assistant configuration files

The general configuration file for Home Assistant is configuration.yaml, this is where we can generally configure the behavior and also tell the system where the configuration files for sensors, automations, scripts and much more are.

Organizing files is essential

When we install a home automation system like Home Assistant, organization is very important. From the name of the devices that we register with their entities, as well as the name of the automations and, of course, the organization of said configuration files. Keep in mind that we can put all the sensors, templates and MQTT configuration directly in the general configuration file, but it is not recommended because we would have an extremely long configuration file that is very difficult to manage.

For this reason, it is best to create separate configuration files, and join them all in the main configuration.yaml, because otherwise this file would become too large to be maintained correctly. In our case, we have the following configuration file:

#Configuracion de todas las carpetas para los archivos de configuracion.
group: !include AA_groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include AA_scenes.yaml
sensor: !include AA_sensors.yaml
cover: !include AA_covers.yaml
template: !include AA_templates.yaml
mqtt: !include AA_todomqtt.yaml
binary_sensor: !include AA_binarysensors.yaml

It is very important that Let’s not change the name of “automations.yaml” and “scripts.yaml” from the originalbecause if we do, when we go to the «Settings / Automations» We will get a notice with an error, indicating that we should do all the configurations in the configuration file manually, and not using the graphical user interface. In case you want to maintain two different files, one that is manual and another through the GUI, then you will have to put two tags to differentiate them in the configuration.yaml.

automation manual: !include_dir_merge_list automatizaciones/
automation ui: !include automations.yaml

With this configuration, all the automations that you want to add manually must be in the automations folder. The ones that we create through the graphical user interface will be in “automations.yaml” and you should not change the name, otherwise it will give an error when opening the “Automations” section, although they will continue to work without any problem if the syntax is correct, it is simply a warning indicating that we cannot do both. In fact, we could also configure it in the following way:

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

And in the automations_manual.yaml file, put all our automations in the same configuration file, without having to create one for each automation we want.

Once we have seen the code to use, below you can see our configuration file that we have in production right now:

The configuration files with all the “group”, “automation”, “script”, “scene” and the rest, come with the following directive:

!include archivo_configuracion.yaml

This means that all the configuration related to “group”, “automation”, “script”, “scene” and the rest are in the file you have with that name. In this way, we can configure each file individually to have better organization.

If we go to the «File Editor«, we can see the complete list of configuration files that have already been created, some of them are empty because we do not use them, and others have virtual sensors and different templates.

For example, we have a virtual sensor that tells us if the garage door is open or closed, depending on the state of two different sensors. We would have to put this code in yaml in the configuration.yaml if we did not have the organization of the configuration files.

We also have the possibility that in «configuration.yaml» point to a specific folder, and import all configuration files, instead of just using one configuration file. In the event that we have “sensors” configuration files that are too large, we may create several individual configuration files. The options we have available are the following:

  • !include_dir_list– This option will return the contents of the directory as a list and the content of each file is an entry in the list.
  • !include_dir_named– This option returns the contents of a directory as a dictionary that maps file name and file content.
  • !include_dir_merge_list– Returns the contents of the directory as a list merging all the files into one larger list.
  • !include_dir_merge_named– Returns the contents of a directory as a dictionary by loading each file and merging it into a larger one.

Depending on what your configuration files are like, you can configure one option or another.

In the previous example where we have all the sensors in the file «AA_sensors.yaml«, we have the possibility of creating a folder called «sensors» where we have all the sensors. The configuration.yaml configuration file would look like this:

!include_dir_merge_named sensores/

And now each individual configuration file would look like this:

#Sensor virtual con el estado de la puerta del garaje
- platform: template
sensors:
sensor_puerta_estado_garaje:
friendly_name: "Estado Puerta Garaje"
value_template: >-
{% if is_state('binary_sensor.shellydw2_puerta_garaje_cerrado', 'off') %}
Cerrada
{% elif is_state('binary_sensor.shellydw2_puerta_garaje_abierto', 'off') %}
Abierta
{% else %}
Entre-Abierta
{% endif %}
icon_template: >-
{% if is_state('binary_sensor.shellydw2_puerta_garaje_cerrado', 'off') %}
mdi:garage-variant-lock
{% elif is_state('binary_sensor.shellydw2_puerta_garaje_abierto', 'on') %}
mdi:garage-alert-variant
{% else %}
mdi:garage-open-variant
{% endif %}

Another sensor would be the following:

#Sensor virtual con la actualización de datos del edata.
- platform: template
sensors:
last_registered_kwh_date:
friendly_name: "Edata actualizacion datos"
value_template: >-
{{ state_attr('sensor.edata_99nk', 'last_registered_kWh_date') }}

And the last sensor would be this:

- platform: edata
debug: true

In this way, we can divide all the sensors we have in the same specific file, into several different files and all of them in the same folder, to further improve the organization of all Home Assistant files.

Related Articles