Play spotify on source from multiple accounts

Hi, I have recently started using home-assistant and I have set up a configuration to play music from spotify on my MusicCast devices. Now, both, my wife and me have a spotify account and use it to play music. My hass automation works perfectly fine if the MusicCast device was last used by the spotify account that is used in the automation. However, it doesn’t work if the device was used last by the other account. In this case, the device does not appear in the spotify media players source list.

Any ideas how this could be solved?

To give some more details:

  • I have set up spotify in home assistant with support for 2 accounts. This gives me 2 spotify entities:
    • media_player.spotify_account1
    • media_player.spotify_account2
  • I have set up the following scene (scene.my_test_scene)
- id: '1662888589307'
  name: Test Spotify Scene
  entities:
    media_player.spotify_account1:
      volume_level: 0.35
      source: 'MyMusicCastDevice'
      shuffle: true
      repeat: 'off'
      friendly_name: Spotify
      supported_features: 444983
      state: paused
  metadata: {}
  • I also created the script
spotify_play:
    alias: Spotify Play
    description: 'Play playlist in Spotify'
    sequence:
        - alias: Set Scene
          service: scene.turn_on
          target:
            entity_id: scene.my_test_scene
        - alias: Spotify Play
          service: media_player.play_media
          data:
            entity_id: media_player.spotify_account1
            media_content_type: playlist
            media_content_id: spotify:playlist:4QNXIVL4Zzm5JSsS4kgv4M

Running this script works fine and starts playing the desired playlist from MyMusicCastDevice if account1 was previously playing spotify on that device. However, if account2 was recently playing on that device, I first need to start playing music on MyMusicCastDevice with account1 from the mobile app before running the script.

MyMusicCastDevice is always listed in the source_list of the spotify entity of the account that was last used to play on this device.

Now, ideally, it would be possible to switch the account not only via the app on the mobile phone via the spotify app but also via home-assistant. If that is not possible, a workaround could be to set up 2 scenes, one for each spotify account and then select the scene and the media player for playing the playlist based on the source list of MyMusicCastDevice. Is any of this possible?

1 Like

I have not yet found a way to change the account - maybe that’s a limitation by spotify. However, it is possible to check which spotify account was last used on the device by checking the source_list and then use it accordingly:

spotify_play:
    alias: Spotify Play
    description: 'Play playlist in Spotify'
    sequence:
        - variables:
            # Get the target_device_name from the input select. These names have
            # to match the names that are shown in the spotify accounts
            # source_list.
            target_device_name: "{{ states('input_select.musicbox_target_device') }}"
            # Check which device is selected for playing (target_device):
            target_device: >
              {% if target_device_name == "Esszimmer" %} media_player.esszimmer
              {% elif target_device_name == "Wohnzimmer" %} media_player.wohnzimmer
              {% elif target_device_name == "Küche" %} media_player.kuche
              {% elif target_device_name == "Tragbar" %} media_player.tragbar
              {% elif target_device_name == "Bewegungsraum" %} media_player.bewegungsraum
              {% else %} error in the automation
              {% endif %}
            # Check which account was playing last on the target device as only
            # this account can be used:
            spotify_account: >
              {% if target_device_name in (state_attr('media_player.spotify_thomas', 'source_list'))|trim %} media_player.spotify_thomas
              {% elif target_device_name in (state_attr('media_player.spotify_anna', 'source_list'))|trim %} media_player.spotify_anna
              {% else %} error in the automation
              {% endif %}
        # Now notify
        - service: notify.notify
          data:
            title: Spotify Play
            message: >
              Start playing spotify on '{{ target_device }}' with account '{{ spotify_account }}' media '{{ media }}'

        # Prepare media players for playback.
        - service: media_player.turn_on
          target:
            entity_id:
              - '{{ target_device }}'

        - service: media_player.select_source
          data:
            source: '{{ target_device_name }}'
          target:
            entity_id: '{{ spotify_account }}'

        - service: media_player.volume_set
          data_template:
            entity_id:
              # - '{{ spotify_account }}'
              - '{{ target_device }}'
            volume_level: 0.5

        - service: media_player.unjoin
          target:
            entity_id:
              - '{{ target_device }}'

        - service: media_player.repeat_set
          data:
            repeat: "off"
          target:
            entity_id:
              - '{{ spotify_account }}'
              - '{{ target_device }}'

        - service: media_player.shuffle_set
          data_template:
            entity_id:
              - '{{ spotify_account }}'
              - '{{ target_device }}'
            shuffle: false

        # Start playing the given media. This script is called from the mqtt
        # trigger which sends the content of media as message. Expected is a
        # spotify URI.
        - alias: Spotify Play
          service: media_player.play_media
          data_template:
            entity_id: '{{ spotify_account }}'
            media_content_type: playlist
            media_content_id: '{{ media }}'
1 Like