How to Set Up Spotify on Home Assistant

In this tutorial, I walk through the steps on how to set up the Spotify integration in Home Assistant.

Spotify on Home Assistant

Spotify is one of the largest streaming music platforms available today. It has a wide range of music and podcasts from different musicians, artists, and podcasters around the world. Spotify offers a free and paid premium version of its offerings.

Integrating Spotify into Home Assistant is a great way to control your music remotely or automate it based on a series of events. For example, you can have Home Assistant start playing a certain playlist once you are detected walking through the front door. Home Assistant opens you up to endless ways you can play music around the house.

To start this tutorial, you will need to have an active Spotify account and Home Assistant set up on a Raspberry Pi, or something similar.

This tutorial will cover setting up your Spotify developer account, installing the integration to Home Assistant, and setting up a basic script.

Setting Up Spotify

We will first need to configure Spotify for use within Home Assistant. This process is relatively easy and involves creating a Spotify application using the developer website.

1. To begin, you will first need to go to the Spotify Developer Website and login to your Spotify account.

Spotify Developers Website - Log In

2. Once logged in, click the “profile” in the top right and click “Dashboard“.

Go to the Spotify Developers Dashboard

Before proceeding to the next step, you must agree to the terms and conditions of the developer account.

3. On your Spotify developer account dashboard, click “Create app“.

Spotify Create App

4. Next, enter the “name” (1) and “description” (2) of the app. It can be anything, but it will be best to make it as descriptive as possible.

You must also enter the following URL as the “redirect URI” (3).

https://my.home-assistant.io/redirect/oauth

For the API/SDK question, you will want to select “Web API” (4).

Lastly, you will need to “agree” with Spotify’s terms of service and guidelines (5).

Below is an example of the settings I entered into each field.

Spotify Developers Create App Options Page

Once you are happy with everything entered, click on “Save” (6).

5. You should be redirected to your new app’s dashboard. Click on “Settings“.

Spotify Custom App Dashboard

6. On this page, you will see details for your application. We will need both the “Client ID” and the “Client Secret“. By default, the client secret is hidden, so you will need to click View client secret.

Spotify App View Client Secret

7. You will need to copy both the “Client ID” and the “Client secret” into a safe place, as we will need these within Home Assistant.

If you ever need to reset your client secret, click “Rotate Client Secret“. Do not forget to update it within Home Assistant if you reset it.

Spotify App Client Secret and Client ID

Installing the Spotify Integration

In this section, we will install the Spotify integration. To proceed, you will need to ensure that you have both the client ID and client secret from your Spotify developers account to proceed.

1. In your Home Assistant, go to the “Settings” page and click on “Devices and services“.

Home Assistant Settings Page - Click Devices and Services

2. On the next page, click the “Add Integration” button in the bottom right corner of the screen.

Home Assistant - Add Integration

3. Search (1) or find “Spotify” in the list of different brands. Once you have found the “Spotify” (2) integration, click it.

Integrations - Select Brand - Spotify

4. You will now need to enter the details of the Spotify developer account. The name can be anything you want, but for simplicity’s sake, I called mine “Spotify” (1). I also entered the “oath client id” (2) and the “oath client secret” (3) I saved from earlier in this tutorial.

Once everything is entered, click “ADD” (4).

Spotify Integration - Add Credentials - Filled

5. Next, you will see a screen asking you to allow Spotify to connect to Home Assistant. Read through the information carefully. Once you are happy, click on “Agree” to proceed.

Allow Spotify to Connect to Home Assistant

6. You should now see another screen asking you to link your account to Home Assistant. Ensure the instance URL is pointing to the correct location; if not, you will need to update it by clicking the pencil.

Click “Link Account” to proceed.

Link Account to Home Assistant Screen

7. Finally, you should see a success message. You can assign your Spotify account to an area or simply click “Finish“.

Spotify Setup Success in Home Assistant

8. To open the Spotify integration, find it on the integrations page and click “1 Service“.

Find Spotify within Integrations

9. On this page, you can see all the details of the connected Spotify account, such as a log of when the status changes from playing to idle. If music is currently playing, you can see the details of it.

Home Assistant Spotify Device Information

10. Clicking on the “control panel” will bring up more information.

Home Assistant Spotify Open the Control Panel

As you can see below, the expanded control panel has more information and controls for the media. By clicking browse media, you can view and change the music being played.

Below is an example of the menu shown when you click to browse media.

Home Assistant Spotify Controls

Example Spotify Script

You can now control Spotify using automations within Home Assistant. In this section, I will show you a basic script you can set up to control your Spotify.

There are a few bits of information you will need to make life easier when writing the script. I will go through them below.

How to Find the Source Name

You must know the name of the source (device) on which you wish to play the Spotify playlist. The easiest way to view this is to open the Spotify media control panel within Home Assistant. If you play music on the device you wish to use, you should see it as the selected source.

The example below shows the source id for the media player that is currently in use. If multiple sources are available, they will be viewable by clicking on the dropdown.

Source List in Spotify Media Control on Home Assistant

How to get a Playlist URL

To complete our script, we will need a reference to the playlist we wish to play. The easiest way to do this is to open the Spotify app or go to their website.

Once in Spotify, locate the playlist you wish to use and click on the “three dots” next to the play button and plus button.

Next, hover over the share item menu and click “Copy link to playlist“. Save this link, as we will need it in the next part.

Copy Link to Spotify Playlist


Writing the Script

In this step, I go through the steps of setting up the script in Home Assistant so that we can get our Spotify player to play on request.

1. In Home Assistant, go to the “settings” page and click “Automations & scenes“.

Home Assistant Settings - Automations and Scenes

2. Next, go to the “Scripts” tab and click “Add Script” in the bottom right-hand corner of the screen.

Create your Script in Home Assistant

In the pop-up, click “Create new script“.

Create a new Script

3. On this page, click on the “three dots” in the top right corner of the screen and swap to “Edit in YAML“.

Edit Script in YAML

4. We will now write the YAML for our Spotify script. I will briefly explain the script below. Check out the official documentation if you want more information on scripting in Home Assistant.

To begin, we specify sequence:. This entry specifies a series of actions that you wish to run.

After sequence, we specify service: media_player.select_source.

This service will select the source for where we want our music to play. We need to set target to our Spotify media player entity. Lastly, we want to set the source to our Spotify player, which, in my case, is Angus-PC.

Our second service call is to play media. To do this, we first specify service: media_player.play_media.

We will set the media_content_id to the Spotify playlist URL we wish to play. We also set the content_type to playlist. Lastly, we set the entity to our Spotify media player entity.

Lastly, we specify some admin options to help identify the script easier. Alias is the name of the script. Description is a short description of what the script is trying to achieve. Lastly, we set an icon for our script so it can easily be identified.

sequence:
  - service: media_player.select_source
    target:
      entity_id: media_player.spotify_angus
    data:
      source: "ANGUS-PC"

  - service: media_player.play_media
    data:
      media_content_id: https://open.spotify.com/playlist/37i9dQZF1DZ06evO2VafrW?si=808a023b81354608
      media_content_type: playlist
    target:
      entity_id: media_player.spotify_angus
alias: Play Spotify
description: "Plays a Spotify playlist to a device."
icon: mdi:account-music

5. Once done, click the “Save Script” button in the bottom right corner of the screen.

Save new Script for Spotify

6. Test the script by clicking on the “three dots” at the top right corner of the screen and click “Run script“. The script should run, and music should start to play on your specified music player.

Run Spotify Script to Test

7. You can now use this script in an automation or trigger it manually whenever you need it to run.

Conclusion

I hope you now have the Spotify integration set up and working within your Home Assistant installation. It is a great way to control your music and opens up opportunities to automate the playing of music.

There are a huge range of different things that you can do with Home Assistant. If you are looking for ideas, be sure to check out our many different Home Assistant tutorials.

If you have any feedback regarding this tutorial, please be sure to leave a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *