Note: This integration is strongly inspired by media_player.template.
Unfortunately that integration is no longer maintained and missing some features, so I decided to create a new and improved one.
With Template Media Player you can create media player entities in Home Assistant using templates and scripts.
You can define any attribute you want and create custom behaviour for all services supported by the media_player
domain.
This allows you to combine your existing media players into a single entity for improved usability and control or to create completely new media players from scratch.
You can install this integration using the custom repository option in HACS.
- Add the repository URL to the list of custom repositories in HACS
- Select and install the integration in HACS
- Restart Home Assistant
- Configure your entities
To create the entities you need to define them in your configuration.yaml
file.
For a full example of all available options see examples.
media_player:
- platform: template_media_player
media_players:
my_media_player:
unique_id: my_media_player
friendly_name: My Media Player
device_class: tv
icon_template: mdi:television
state_template: "on"
Options that are marked as _template
or elements of the attribute_templates
object can be defined using Jinja2 templates:
# ...
state_template: >
{% if states('media_player.something") == "on" %}
idle
{% else %}
off
{% endif %}
To define state attributes for your entity use the attribute_templates
option.
You can use the variable attribute
in your templates to get the current attributes name as a string.
For a full list of attributes commonly used by media player entities see examples.
media_player:
- platform: template_media_player
media_players:
my_media_player:
#...
attribute_templates:
media_title: >
# attribute contains the value "media_title"
{{ state_attr("media_player.something", attribute) }}
To reduce code duplication you can define a template using the global_template
option.
This template will be executed before each of the other templates and can be used to define common variables.
media_player:
- platform: template_media_player
media_players:
my_media_player:
#...
global_template: >
{% set tv = "media_player.tv" %}
state_template: >
{{ states(tv) }}
Elements of the service_scripts
, source_scripts
or sound_mode_scripts
options are action sequences like in Home Assistant scripts.
media_player:
- platform: template_media_player
media_players:
my_media_player:
#...
source_scripts:
Plex:
- service: media_player.turn_on
data_template:
entity_id: media_player.tv
- delay:
seconds: 3
- service: media_player.select_source
data_template:
entity_id: media_player.tv
source: Plex
Use the service_scripts
option to define services that are supported by the media_player
domain.
For a full list of services and their respective variables supported by media player entities see examples.
media_player:
- platform: template_media_player
media_players:
my_media_player:
#...
service_scripts:
turn_on:
- service: media_player.turn_on
data_template:
entity_id: media_player.tv
Use the source_scripts
option to define sources for your media_player.
media_player:
- platform: template_media_player
media_players:
my_media_player:
#...
source_scripts:
Plex:
- service: media_player.select_source
data_template:
entity_id: media_player.tv
source: Plex
Use the sound_mode_scripts
option to define sound modes for your media_player.
media_player:
- platform: template_media_player
media_players:
my_media_player:
#...
sound_mode_scripts:
Bass Boost:
- service: media_player.select_sound_mode
data_template:
entity_id: media_player.soundbar
source: Bass Boost
You can specify an entity using the base_media_player_entity_id
option to inherit all supported behaviour and attributes from, when the behaviour or attribute is not implemented by the template media player.
You can specify an entity to use for the browse media functionality using the browse_media_entity_id
option.
Make sure you also define the play_media
service for this to work.