Working solution to stream Spotify Playlists to Google Home or Alexa (using Mopidy)



I had the problem that I could not stream Spotify Playlists to my Google Home with Home-Assistant. After some investigations I found out that Spotify doesnt support the automated streaming of playlists to Google Home or Alexa (although single musictracks work fine).

It is possible to send an audio stream to Google Home (like a webradio), so I thought about the following workaround:

  • Mopidy receives Spotify
  • Mopidy passes the audio signal to LiquidSoap
  • LiquidSoap Streams the audio signal into the network
  • Google Home plays the network stream

There are already many docker images that cover the above points, but sadly they use the official version of the mopidy-spotify package. Unfortunately this one has a malfunction and can't play playlists. Therefore I created my own Docker Image which uses a bugfix version of mopidy-spoptify (kudos to kingosticks and Blacklight).

Docker Image

docker pull galdan/mopidy-stream
docker run -e SPOTIFY_USERNAME=spotifyUser
           -e SPOTIFY_PASSWORD=spotifyPassword
           -e SPOTIFY_ID=spotifyId
           -e SPOTIFY_SECRET=spotifySecret
           -p 6600:6600
           -p 6680:6680
           -p 8800:8800
           -t galdan/mopidy-stream:latest

You can easily retrieve the Spotify ID and Secret on this page.

The Docker image is based on the work of schinken.

Verify functionality of Mopidy

You can connect now to the running Docker image using following command:

sudo docker exec -i -t <containername> /bin/bash

In the container the program mpc is available. MPC is used to remote controle Mopidy.

You can try now to add a spotify playlist with the command mpc add spotify:user:spotify:playlist:37i9dQZF1DWWM6GBnxtToT
Then start playing it: mpc play

When you open the URL http://192.168.0.15:8800/mopidy in a browser, you should hear the playlist (after some delay).

Home-Assistant

Spotify Control in Home-Assistant

packages/spotify.yaml

media_player:
  - platform: mpd
    name: mpd
    host: 192.168.0.15
    port: 6600

input_number:
  spotify_volume_bath:
    name: 'Volume bath'
    icon: mdi:volume-high
    min: 0
    max: 1
    step: 0.05
    initial: 0.2

input_select:
  spotify_playlist:
    name: 'Playlist'
    options:
      - Dein Mix der Woche
      - Classic Acoustic
      - Fallout 4 Soundtrack
      - Mafia Soundtrack
      - Electronic Rising
      - Techno Bunker
      - Truly Deep House
      - Deep Listening
      - Feelgood Indie
      - Totally Stressfree
      - Songs die dir bis jetzt entgangen sind
      - Billboard Top 1000
      - Release Radar
      - Peaceful Piano
      - Sunday Morning
      - Oldies but Goldies
      - Baby Lieder Baby Schlaf
      - B-Seite

automation old:
  - alias: Change volume in bathroom if changing slider
    trigger:
      platform: state
      entity_id: input_number.spotify_volume_bath
    action:
      - service: media_player.volume_set
        data_template:
          entity_id: media_player.googlehome_bath
          volume_level: '{{ states.input_number.spotify_volume_bath.state }}'

  - alias: "Motion: Bathroom Radio Timer ON"
    trigger:
    - entity_id: binary_sensor.bathroom_motion
      platform: state
      to: 'on'
    condition:
      - condition: time
        after: 07:30:00
        before: '20:00:00'
      - condition: state
        entity_id: media_player.mpd
        state: 'off'
    action:
    - service: script.turn_on
      entity_id: script.spotify_bath_on

  - alias: "Timer: Bathroom Radio OFF"
    trigger:
    - entity_id: binary_sensor.bathroom_motion
      for:
        minutes: 15
      platform: state
      to: 'off'
    action:
    - service: script.turn_on
      entity_id: script.spotify_bath_off


script:
  spotify_bath_on:
    alias: 'Start music in bathroom'
    sequence:
      
      - service: media_player.volume_set
        data_template:
          entity_id: media_player.googlehome_bath
          volume_level: '{{ states.input_number.spotify_volume_bath.state }}'

      - service: media_player.play_media
        data_template:
          entity_id: media_player.googlehome_bath
          media_content_type: music
          media_content_id: http://192.168.0.15:8800/mopidy

      - service: media_player.shuffle_set
        data_template:
          entity_id: media_player.mpd
          shuffle: true
      
      - service: media_player.play_media
        data_template:
          entity_id: media_player.mpd
          media_content_type: music
          media_content_id: >
            {% if is_state("input_select.spotify_playlist", "Dein Mix der Woche") %} spotify:user:spotify:playlist:37i9dQZEVXcUVB4pJAf8kT
            {% elif is_state("input_select.spotify_playlist", "Classic Acoustic") %} spotify:user:spotify:playlist:37i9dQZF1DX504r1DvyvxG
            {% elif is_state("input_select.spotify_playlist", "Fallout 4 Soundtrack") %} spotify:user:spotify:playlist:6zlDvW7Ab10r0nOfDPtcfR
            {% elif is_state("input_select.spotify_playlist", "Mafia Soundtrack") %} spotify:user:spotify:playlist:4046T63qpz3BG6B4kIrZeF
            {% elif is_state("input_select.spotify_playlist", "Electronic Rising") %} spotify:user:spotify:playlist:37i9dQZF1DX8AliSIsGeKd
            {% elif is_state("input_select.spotify_playlist", "Techno Bunker") %} spotify:user:spotify:playlist:37i9dQZF1DX6J5NfMJS675
            {% elif is_state("input_select.spotify_playlist", "Truly Deep House") %} spotify:user:spotify:playlist:37i9dQZF1DX5xiztvBdlUf
            {% elif is_state("input_select.spotify_playlist", "Deep Listening") %} spotify:user:spotify:playlist:37i9dQZF1DX5y2yPmGqrG8
            {% elif is_state("input_select.spotify_playlist", "Feelgood Indie") %} spotify:user:spotify:playlist:37i9dQZF1DWWM6GBnxtToT
            {% elif is_state("input_select.spotify_playlist", "Totally Stressfree") %} spotify:user:spotify:playlist:37i9dQZF1DWT7XSlwvR1ar
            {% elif is_state("input_select.spotify_playlist", "Songs die dir bis jetzt entgangen sind") %} spotify:user:spotify:playlist:37i9dQZF1Ea2HT3zueQX7e
            {% elif is_state("input_select.spotify_playlist", "Billboard Top 1000") %} spotify:user:spotify:playlist:24P5rHoP6MhEiEaB25l1wI
            {% elif is_state("input_select.spotify_playlist", "Release Radar") %} spotify:user:spotify:playlist:37i9dQZEVXbqtQ8kWVHMCH
            {% elif is_state("input_select.spotify_playlist", "Peaceful Piano") %} spotify:user:spotify:playlist:37i9dQZF1DX4sWSpwq3LiO
            {% elif is_state("input_select.spotify_playlist", "Sunday Morning") %} spotify:user:sonymusicnl:playlist:7aRkIB2UvssMbJyzOH8nST
            {% elif is_state("input_select.spotify_playlist", "Oldies but Goldies") %} spotify:user:spotify:playlist:37i9dQZF1DXdmXczhgY3oW
            {% elif is_state("input_select.spotify_playlist", "Baby Lieder Baby Schlaf") %} spotify:album:0h0pR1Sl1Elg1oZzUwjbvg
            {% elif is_state("input_select.spotify_playlist", "B-Seite") %} spotify:user:spotify:playlist:37i9dQZF1EiY1J9jyBCSi7
            {% endif %}

  spotify_bath_off:
    alias: 'Stop music in bathroom'
    sequence:
      - service: media_player.media_stop
        data:
          entity_id: media_player.googlehome_bath

      - service: media_player.media_stop
        data:
          entity_id: media_player.mpd

ui-lovelace.yaml

  - icon: mdi:volume-high
    title: Multimedia
    id: multimedia
    cards:
      - type: entities
        title: Spotify
        entities:
          - input_select.spotify_playlist
          - media_player.mpd

      - type: entities
        title: Bathroom
        entities:
          - input_number.spotify_volume_bath

      - type: entity-button
        name: Play Music in Bathroom
        entity: script.spotify_bath_on
        tap_action:
          action: call-service
          service: script.spotify_bath_on

      - type: entity-button
        name: Stop Music in Bathroom
        entity: script.spotify_bath_off
        tap_action:
          action: call-service
          service: script.spotify_bath_off

Comments