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:
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.
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