{% set rooms = ['den', 'family', 'master', 'guest', 'bathroom', 'living', 'garage', 'workshop'] %}
{% set ns = namespace(speakers = 'media_player.kitchen') %}
{% for i in range(rooms | length) %}
{% if states('input_boolean.' ~ rooms[i]) == 'on' %}
{% set ns.speakers = ns.speakers ~ ', media_player.' ~ rooms[i] %}
{% endif %}
{% endfor %}
{{ ns.speakers }}
The assumption made in the template is that each room in the rooms
list has a corresponding media.player
and input_boolean
bearing the same name. Example:
media_player.den
input_boolean.den
media player.family
input_boolean.family
etc
-
The template creates a global string variable called
speakers
and initializes it tomedia_player.kitchen
. This is the default media_player (and needs no corresponding input_boolean). -
It proceeds to iterate through each item in the
rooms
list. -
If a room’s input_boolean is
on
, it appends the room’s media_player tospeakers
. -
After iterating through the list, it reports the contents of
speakers
.
Here it is in action. I created several input_booleans and set den, master, and bathroom to on
.
If the name of each room’s input_boolean was not identical to the room’s name (like input_boolean.den_speaker) then the template would need a small modification. The input_boolean names can be stored in a separate list or even in a different data structure, such as a dictionary. All this to say, the input_boolean’s name doesn’t have to be identical to the room’s name … but it makes the template simpler.
EDIT
Typo. Replaced ', media.player.'
with ', media_player.'