Service_template: how to select source in a mediaplayer

  - alias: "Toggle Yamaha-receiver on off when casting to livingroom"
    trigger: 
      - platform: state
        entity_id: media_player.livingroom
    action:
      - service_template: > 
          {% if is_state('media_player.livingroom' , 'off') %} 
            media_player.turn_off
          {% else %}
            media_player.turn_on
          {% endif %}
        entity_id: media_player.yamaha_receiver_livingroom
      - service_template: > 
          {% if not is_state('media_player.livingroom' , 'off') %} 
            media_player.select_source
            data:
              entity_id: media_player.yamaha_receiver_livingroom
              source: 'AUDIO1'
          {% endif %}

The script above is very useful, but one feature does not work quite the way I want it to.

I have a Yamaha-receiver with a chromecast connected to Audio1.

When that chromecast is turned on (i.e. it’s state is no longer “off”), then the Yamaha-receiver will turn on and switch to source “Audio1”.

When I turn of the chromecast (i.e. when nothing is casting to it any more), the Yamaha-receiver will turn off.

Everything is working except of the source-selecting part.

How shall I alter the script below media_player.select_source in order to select source?

add a condition instead of using a service_template:

  - alias: "Toggle Yamaha-receiver on off when casting to livingroom"
    trigger: 
      - platform: state
        entity_id: media_player.livingroom
    action:
      - service_template: > 
          {% if is_state('media_player.livingroom' , 'off') %} 
            media_player.turn_off
          {% else %}
            media_player.turn_on
          {% endif %}
        entity_id: media_player.yamaha_receiver_livingroom
      - condition: state
        entity_id: media_player.livingroom
        state: 'on'
      - service: media_player.select_source
        data:
          entity_id: media_player.yamaha_receiver_livingroom
          source: 'AUDIO1'

This will only move past the condition if the condition is met. Otherwise it will exit the action without executing anything else.

1 Like