The YAML Preprocessor is a tool designed to enhance the YAML configuration experience in Home Assistant. It allows you to preprocess your YAML files, enabling features like variable substitution, making your configurations cleaner and more maintainable.
This is an experimental project and should be used with caution. It is not officially supported by Home Assistant, and there may be bugs or issues that arise during use. Always back up your configuration files before using the preprocessor.
Pre-processing the core configuration file is not handled by this preprocessor. It is recommended to use the preprocessor only for included files, such as packages or custom components. The preprocessor will not work with the main configuration file (configuration.yaml) directly.
You can install the YAML Preprocessor in two ways:
- Using HACS (Home Assistant Community Store):
- Open HACS in your Home Assistant instance.
- Go to the "Integrations" section.
- Search for "YAML Preprocessor".
- Click on "Install".
- Restart Home Assistant to apply the changes.
- Manual Installation:
- Download the latest release from the GitHub repository.
- Extract the contents of the downloaded file.
- Copy the
custom_components/yaml_preprocessorfolder into your Home Assistantcustom_componentsdirectory. - Restart Home Assistant to apply the changes.
To configure the YAML Preprocessor, you need to add the following lines to your configuration.yaml file:
yaml_preprocessor:
# Dot-files in the input directory will be ignored but can be used as reusable snippets
input_dir: packages/input
# be careful, this directory will be deleted each time you run the preprocessor
output_dir: packages/output
# Example for using the preprocessor in your configuration
homeassistant:
packages: !include_dir_named packages/outputThe YAML Preprocessor allows you to create reusable YAML snippets and include them in your main configuration. Here is an example of how to use it following the previous configuration:
- Create a file named
packages/input/.light.yamlwith the following content:unique_id: light_${id}_has_red name: ${name} Light has some red state: > {% if is_state('light.light_${id}', 'on') %} {% set rgb = state_attr('light.light_${id}', 'rgb_color') %} {{ rgb and rgb[0] > 0 }} {% else %} false {% endif %}
- Create a file named
packages/input/lights.yamlwith the following content:template: - binary_sensor: - !include file: ./.light.yaml vars: id: living_room name: Living Room - !include file: ./.light.yaml vars: id: kitchen name: Kitchen
- Call the preprocessor to generate the output: in Home Assistant, go to Developer Tools > Services and call the
yaml_preprocessor.preprocessservice. This will generatepackages/output/lights.yamlwith the following content:template: - binary_sensor: - unique_id: light_living_room_has_red name: Living Room Light has some red state: > {% if is_state('light.light_living_room', 'on') %} {% set rgb = state_attr('light.light_living_room', 'rgb_color') %} {{ rgb and rgb[0] > 0 }} {% else %} false {% endif %} - unique_id: light_kitchen_has_red name: Kitchen Light has some red state: > {% if is_state('light.light_kitchen', 'on') %} {% set rgb = state_attr('light.light_kitchen', 'rgb_color') %} {{ rgb and rgb[0] > 0 }} {% else %} false {% endif %}
We welcome contributions to the YAML Preprocessor project! If you have suggestions, bug reports, or would like to contribute code, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them with clear messages.
- Push your changes to your forked repository.
- Create a pull request to the main repository.
- Ensure your code adheres to the project's coding standards and passes all tests.
This project is licensed under the MIT License. See the LICENSE file for details.
This project is maintained by huafu and contributors. Special thanks to the Home Assistant community for their support and feedback.