Template Switch for Media Player Source

I have a media player entity with sources.
I want to be able to control those sources with switches so that I can expose it to my Google Assistants and control the source by voice. I believe the best way to do this is with a switch template but being my first attempt with Home Assistant I have no clue how to do this.

It should be a switch that is on if the media player is on and the source is set to the one I define.

When the switch is turned off it will turn off the media player.
When the switch is turned on it will turn the media player on (if off) and then change the source.

Can someone please direct me on the best method of doing this?

switch:
  - platform: template
    switches:
      kitchen_tuner:
        value_template: "{{ is_state('media_player.kitchen', 'on') && is_state('media_player.kitchen.source', 'Tuner') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.skylight_open
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.skylight_close

What relationship is there, if any, between the media_players listed in value_template and the skylight entities seen in turn_on and turn_off?

My guess is the answer is ‘None’ and therefore this Template Switch won’t work properly.

The value_template should report the state of whatever turn_on and turn_off just acted on (skylight).

Are you sure a Template Switch is best for what you want to do? Or is it a requirement in order to be able to work via Google Assistant?

@123 I just grabbed this example, it is not an attempt to get it working as I figured someone else would have done something similar in the past.

I don’t know if this is the best method. I simply want to be able to expose a switch to google assistant so i can say ‘Hey google, turn on the radio in the kitchen’ and it will turn on my Russound media player and switch to the proper source input.
Short answer is yes that is my goal to expose source switching to google assistant.

As this is my first attempt at creating something custom, other than following setup instructions (have ssl, reverse proxy, google assistant, google maps device tracking, russound, ecobee all setup).

Thanks for any help

I don’t have media_players to experiment with so I can’t test anything. There are community members here who are more experienced with media_players who can easily steer you in the right direction. Until they show up, you can tinker with this (untested) Template Switch. Obviously you’ll need to modify the entity names to match whatever represents your Russound system.

switch:
  - platform: template
    switches:
      kitchen_tuner:
        value_template: "{{ is_state('media_player.kitchen', 'on') and is_state_attr('media_player.kitchen', 'source', 'tuner') }}"
        turn_on:
          - service: media_player.turn_on
            entity_id: media_player.kitchen
          - delay: '00:00:02'
          - service: media_player.select_source
            data:
              entity_id: media_player.kitchen
              source: 'tuner'
        turn_off:
          - service: media_player.turn_off
            entity_id: media_player.kitchen

I added a delay of 2 seconds between turning the system on and changing its source. This may not be necessary; you’ll have to experiment with it.

Look at the source names as they’re presented in Developer Tools > States for media_player.kitchen. I used lowercase ‘tuner’ but it may actually be reported in propercase. As a general rule, refer to the States page when checking these names and not how they are presented in the Lovelace UI. The source of truth is the States page.

@123

Quick question, being new to Home Assistant from OpenHAB is there a page that lists all the attributes of the components? The Media Player component page lists the services but not the attributes.

Thanks,

For a media_player, the list of all possible attributes is in its source code. However, the media_player attributes available to the specific platform one is using are shown in the States page.

Go to ‘developer tools’ and use the filter to bring up media players. The media player will need to be ‘on’ to show you it’s attributes

1 Like

Solution:

switch:
  - platform: template
    switches:
      deck_tuner:
        friendly_name: "Deck Radio"
        value_template: "{{ is_state('media_player.deck', 'on') and is_state_attr('media_player.deck', 'source', 'Tuner') }}"
        turn_on:
          - service: media_player.turn_on
            entity_id: media_player.deck
          - delay: '00:00:01'
          - service: media_player.select_source
            data:
              entity_id: media_player.deck
              source: 'Tuner'
        turn_off:
          - service: media_player.turn_off
            entity_id: media_player.deck
#Google Assistant
google_assistant:
    project_id: !Secret
    api_key: !Secret
    exposed_domains:
      - light
      - switch

After rebooting and syncing devices on Google Assistant the switch showed up to be assigned and worked as intended

Thanks all for helping me with my first bit of configuration in yaml.

1 Like

You’re welcome.

For future reference, it is customary in this community that when someone provides you with information answering your question or solving your problem, that is considered to be the ‘Solution’. The source of the solution, is the Solution, not the copy of it.

Nevertheless, it’s a custom, not a rule, and you’re free to mark whichever post you want as being the Solution.

Good luck and welcome to the Home Assistant community.