Pausing Media Players When Phone Rings

I’m trying to automate pausing my media players when I get or make a call and resume when I’m finished.

- id: '1636047543236'
  alias: OnCall
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.mi_note_10_phone_state
    to: offhook
  - platform: state
    entity_id: sensor.mi_note_10_phone_state
    to: ringing
  condition: []
  action:
    service_template: >
      {% if (is_state("media_player.nest_mini_2", "playing")) %}
        - service: media_player.media_pause
          target:
            device_id: 5cfe9d97f891190e65ecc2fe636dd295
        - wait_for_trigger:
          - platform: state
            entity_id: sensor.mi_note_10_phone_state
            to: idle
        - service: automation.turn_off
          target:
            entity_id: automation.soundbar_on_bluetooth
        - service: media_player.media_play
          target:
            device_id: 5cfe9d97f891190e65ecc2fe636dd295
        - delay:
            hours: 0
            minutes: 0
            seconds: 20
            milliseconds: 0
        - service: automation.turn_on
          target:
            entity_id: automation.soundbar_on_bluetooth
      {% elif (is_state("media_player.box", "playing")) %}
        - service: media_player.media_pause
          target:
            device_id: 56d4e5485b1b84e9f6aada3b43853573
        - wait_for_trigger:
          - platform: state
            entity_id: sensor.mi_note_10_phone_state
            to: idle
        - service: media_player.media_play
          target:
            device_id: 56d4e5485b1b84e9f6aada3b43853573
      {% endif %}
  mode: parallel
  max: 10

It was working when there was no if statemente and it was only controlling the Nest Mini. When trying to add the “box”, it returns Template rendered invalid service:

What am I doing wrong?

You are trying to template whole key: value pairs. Only the value side accepts templates.

Use a choose action instead of a template.

https://www.home-assistant.io/docs/scripts/#choose-a-group-of-actions

The problem with choose is that both players might be on and it’ll only choose the first on the list.

You can also use two choose actions as well:

action:
	- choose:
	   - conditions:
		   - condition: state
			 entity_id: media_player.playerA
			 state: 'playing'
		 sequence:
		   - service: media_player.media_pause
			 entity_id: media_player.playerA
	- choose:
	   - conditions:
		   - condition: state
		 	 entity_id: media_player.playerB
		 	 state: 'playing'
		 sequence:
		   - service: media_player.media_pause
		 	 entity_id: media_player.playerB			

Oh true. Thanks!