Spotify is the renowned music streaming service that needs no introduction. Therefore you will not be surprised to learn that there is a Spotify integration within Home Assistant.
In this tutorial we will learn how to set up the Spotify integration in Home Assistant. The integration is very easy to implement with a few options accessible on the Spotify developer page.
You can use a free Spotify account but the control in Home Assistant will be very limited. Ideally you need a premium account.
Prerequisite
You should have Home Assistant installed and running and a basic understanding of YAML including how to edit configuration.yaml
. You should also understand how to use a service and entity within Home Assistant.
If you are new to Home Assistant or you are not yet familiar with editing YAML files then you should definitely check out my tutorials on automation and scripts first.
If you don’t yet have a Spotify account setup, head over to the site and setup a free account.
You can get things up and running with the free account, however you may want to consider upgrading to their premium offering that will allow Home Assistant full control.
How to Set up Spotify for Home Assistant
Firstly we need to set up Spotify so that we can connect Home Assistant. In order to do this, we need to create our own custom Spotify app on the Spotify developer page.
Don’t worry, it is very simple and completely free. This guide will walk you through the process step by step.
The first thing we need to do is head on over to the Spotify developer dashboard and log in. You should see the login button in the top right-hand corner of the screen.
Create Spotify developer application
Once you have logged in to the developer portal click Create app to create a new app.
Next, enter a name for the app such as Home Assistant and give your app a short description.
If you have not manually disabled My Home Assistant (it is enabled by default), enter the following address in the URI field:
https://my.home-assistant.io/redirect/oauth
If you have manually disabled My Home Assistant, enter the following in the URI field, replacing <IP-address>
with the IP address that you use to access Home Assistant:
http://<IP-address>:8123/auth/external/callback
Tick the box to accept the terms and conditions and then click the Save button.
Fetch the Spotify app client ID and client secret
A new app will be created and you will be taken to the dashboard. Click the settings button to open the app settings.
In the settings menu, click View client secret to reveal the client secret.
Copy and paste the Client ID and the Client Secret into a text editor, we will need these later.
Configuring the Home Assistant Spotify Integration
The first thing that we need to do is add the Spotify integration to Home Assistant. You can either add the integration using My Home Assistant, or you can add it manually from the settings menu.
Add the Spotify Integration with My Home Assistant
The quickest way to add integrations to Home Assistant is by using My Home Assistant. Simply click the button below to add the Spotify integration to your Home Assistant configuration.
Add the Spotify Integration Manually
If you have disabled My Home Assistant, you can add the integration manually. First, Click Settings > Devices & Services.
Next, click the Add integration button in the bottom right-hand corner and choose the Spotify integration.
Configuring the Spotify Integration in Home Assistant
Next, we will configure the Spotify integration using the credentials that we created earlier. Enter the app name, client ID and client secret that you configured in the previous step and then click the Add button.
When prompted, click the Open website button and then click Agree at the bottom of the list of terms and conditions. Lastly, click the Finish button to complete the setup.
Testing the Spotify Integration in Home Assistant
Now that you have sucesfully added and configured the Spotify integration, you will find a Spotify card on the Home Assistant dashboard.
Note that to get the full functionality of Spotify, including the ability to control play, pause and skipping to the next track, you will need a Spotify Premium account.
Setting a Spotify Output Source in Home Assistant
Once you have the Spotify integration up and running, you can specify which of your audio sources you wish to direct Spotify audio to.
The list of available audio devices is dependent on which devices are known to Spotify, so you first need to configure the device you wish to use in the Spotify app.
Once you have the output configured and selected in Spotify, you can check the reference for the particular output in Developer tools > States.
In the example above, Spotify is currently outputting to the speakers of my MacBook Pro. If I select a differenc source, the value here will change.
Once you know the source names for the different devices that you have configured in Spotify, you can configure Home Assistant to select the outputs using the following YAML.
# Select MacBook Pro speakers for Spotify audio output service: media_player.select_source entity_id: media_player.spotify data: source: "Simon's MacBook Pro"
You can combine this YAML with scripts and automations as you prefer, which will give Home Assistant the ability to switch the audio output as you prefer.
Controlling Spotify with Home Assistant Automations
In order to get the most out of the Spotify integration we are going to want to automate it. It allows wonderful things to occur, such as Home Assistant automatically playing your favorite playlist when you arrive home.
In order to demonstrate the basic functionality, we can create a script that will automatically load and play your favorite playlist.
First we need to create a new script in the configuration.yaml
file. If you already have the script integration, simply add this code beneath the existing code and omit the instance of the script integration.
We will create a script called spotify_play_playlist
, but you can name however you prefer.
spotify_play_playlist: alias: Spotify Play description: 'Play playlist in Spotify'
Next we will add the media_player.play_media
service into the sequence. Note that in order for it work work we need to use the “medial_player.play_media” and not the “medial_player.media_play” service.
spotify_play_playlist: alias: Spotify Play description: 'Play playlist in Spotify' sequence: - alias: Spotify Play service: media_player.play_media
Finally we need to add the target and data. We need to specify the entity_id as our Spotify media player. The content type needs to be set as playlist and we need to fetch the Spotify user and playlist code for media_content_id.
spotify_play: alias: Spotify Play description: 'Play playlist in Spotify' sequence: - alias: Spotify Play service: media_player.play_media target: entity_id: media_player.spotify_siytek data: media_content_type: playlist media_content_id: '<spotify-URI>'
We can replace <spotify-URI>
with the URI for the playlist that we wish to play. Simply navigate to the playlist and click the three dots. There you will find an option to copy the URI so that you can paste it into the script. Don’t forget to add the ‘quotes.’
Once you have finished entering the code, save the file and restart Home Assistant. We can then give the script a test run!
Simply follow the steps in my script tutorial to add an entity button using the script we have just created for Spotify as the entity, script.spotify_play
.
Conclusion
Streaming media has revolutionized the way we consume media content and Spotify is at the forefront of streaming music services. In this tutorial we have learnt how to link a Spotify account to Home Assistant and use a basic script to control it.
Setting up Home Assistant to work with Spotify is a straightforward process that allows you to enhance your home automation experience and enjoy seamless control of your music streaming.
By following this easy step-by-step guide, you can integrate Spotify into your smart home ecosystem, enabling your Spotify music library to be controlled by voice commands, automation, and remote control through the Home Assistant platform.
The combination of these two powerful tools opens up a world of possibilities, from creating personalized playlists for different occasions to synchronizing your music with other smart devices in your home.
Whether you’re a music enthusiast or seeking to streamline your home automation, the integration of Home Assistant and Spotify is a winning combination that brings convenience and entertainment together in perfect harmony. Happy automating!
Article Updates
July 27th 2023 : Spotify setup and Home Assistant Integration process completely overhauled. Featured image updated. TOC added.
Article first published March 10th 2020.
Thanks so much for visiting my site! If this article helped you achieve your goal and you want to say thanks, you can now support my work by buying me a coffee. I promise I won't spend it on beer instead... 😏
Hi Siytek,
I got this message after following your code above:
“extra keys not allowed @ data[‘media_content_type’}”
Translation Error: The intl string context variable ‘Level’ was not provided to the string ‘Detalhes do log ({Level})’
Logger: homeassistant.components.websocket_api.http.connection.548215457680
Source: core.py:1212
Integration: websocket_api (documentation, issues)
First occurred: 16:31:38 (1 occurrences)
Last logged: 16:31:38
What am I missing here?
Thanks in advance!
Hey Nacho, Thanks for visiting! Are you using ‘media_play’ instead of ‘play_media’ ?
Hi Siytek. It was that!
It is very easy to learn the way you explain in all posts.
Thanks for sharing Knowledge!
Keep safe!
Really pleased to hear it fixed your issue Nacho, I also made the mistake of using ‘media_play’ the first time I used the Spotify integration! I have added a note about it in the tutorial to help others avoid the issue. Thanks again for the feedback too, really appreciate it 🙂
Hi,
Is there any way that I can automate my chromecast starting at 06:30 am and playing a preset playlist?
I have followed the abouve instructions and as of right now i get the automation to start my chromecast but it doesnt seem to do anything. I can control spotify from my HA-Lovelace UI but only if i have spotify running on that same device.
Maby there is no way to make this work!
Thanks in advance!
Hey Poodlepaag, thanks for visiting! Did you manage to write a script or an automation that can start a playlist in Spotify, like in the example? Could you share your YAML?
I followed all the instructions but nothing happens when I start the script. I am not understanding how it knows which speaker to use. Is this set as the entity id?
I am trying to have a playlist play with a button.