Integrating External Variable Files into Home Automation Automations

Hello everyone,

I’ve been working on several automations and have found myself using a consistent set of variables for speaker mappings. Currently, these mappings are defined within each automation like so:

variables:
  speaker_mapping: 
    master bedroom: media_player.master_bedroom_speaker_ma
    living room: media_player.living_room_display_ma
    kitchen: media_player.kitchen_speaker_ma
    floor up: media_player.floor_up_ma
    floor below: media_player.floor_below_ma
    children room: media_player.children_s_speaker_ma
    office: media_player.office_room_speaker_ma
    zvi room: media_player.office_room_speaker_ma
    yael room: media_player.children_room_speaker_ma
    all speakers: media_player.all_speakers_ma

I’m exploring ways to streamline my configuration and make it more maintainable. Is there a method to store this mapping in a separate file and include it in each automation where it’s needed? I’ve considered something like this:

variables:
    speaker_mapping: !include speaker_mapping.yaml

and creating a file named speaker_mapping.yaml in the config path but this does not work

Would appreciate any insights or suggestions on how to achieve this efficiently.

Maybe a script to do the repeated part of the action?

I believe it’s possible to have reusable templates:

1 Like

There are a couple methods depending on how you handle your configuration and/or build your automations and scripts.

For static (and “relatively” static) data like your mapping example, I use a custom Jinja file. You can declare variables in a file in your custom_templates folder.

#/config/custom_templates/constants.jinja

{% set speaker_mapping = { 
'master bedroom': 'media_player.master_bedroom_speaker_ma',
'living room': 'media_player.living_room_display_ma',
'kitchen': 'media_player.kitchen_speaker_ma',
'floor up': 'media_player.floor_up_ma',
'floor below': 'media_player.floor_below_ma',
'children room': 'media_player.children_s_speaker_ma',
'office': 'media_player.office_room_speaker_ma',
'zvi room': 'media_player.office_room_speaker_ma',
'yael room': 'media_player.children_room_speaker_ma',
'all speakers': 'media_player.all_speakers_ma'} %}

Then import them into templates anywhere you need to.

{% from 'constants.jinja' import speaker_mapping %}
{{ speaker_mapping.get('kitchen') }}
1 Like

so as @Stiltjack and @Didgeridrew mentioned I managed it using the Jinja file

I defined the following method in the Jinja file

{% macro generate_speaker_value(speaker_name) %}
    {% set speaker_mapping = {
        "master bedroom": "media_player.master_bedroom_speaker_ma",
        "living room": "media_player.living_room_display_ma",
        "kitchen": "media_player.kitchen_speaker_ma",
        "floor up": "media_player.floor_up_ma",
        "floor below": "media_player.floor_below_ma",
        "children speakers": "media_player.children_s_speaker_ma",
        "office": "media_player.office_room_speaker_ma",
        "all speakers": "media_player.all_speakers_ma"
    } %}
    
    {% if speaker_name in speaker_mapping %}
        {{ speaker_mapping[speaker_name] }}
    {% else %}
        {% set default_speaker = "media_player.master_bedroom_speaker_ma" %}
        {{ default_speaker }}
    {% endif %}
{% endmacro %}

and in the automation

alias: Assist Next Song On The Specified Speaker - English Language
description: Assist Play Playlist On The Specified Speaker - English Language
trigger:
  - platform: conversation
    command:
      - next song in the {speaker}
action:
  - service: media_player.media_next_track
    target:
      entity_id: >-
        {% from 'tools.jinja' import generate_speaker_value %}  {{
        generate_speaker_value(trigger.slots.speaker.lower()) }}
mode: single

It’s works as expected