Save currently-playing song to Spotify playlist

I often listen to Spotify-generated playlist. Discover Weekly, Daily Mix, Release Radar, etc. Often, I’ll hear a song that I want to earmark to listen to later or add to a playlist. Here’s a method to do that.

While I keep my fingers crossed for an add_to_playlist service for the Spotify component, I’ve used IFTTT as the glue to make this work.

So, you’ll need to set up the IFTTT component so that you can trigger events. Follow the instructions on the component page.

On the IFTTT site, create a new applet. Select “Webhooks >> Receive a web request” for your “this”. Enter “add_to_playlist” as your event name.

Choose “Spotify >> Add track to a playlist” for your “that”. Choose a playlist name. I use “Remember”. Enter “Value1” for the search query and “Value2” for “Artist name.”

In my setup, I use the Universal Media Player component to keep track of what music is playing. This way, it doesn’t matter if I’m listening to Spotify or SqueezePlay or any supported media player. Since the IFTTT applet uses title/artist to search, you can “remember” songs from any media player and save it to Spotify.

Now, create a script that looks like this:

add_to_playlist:
  alias: Add Spotify Track to Playlist
  sequence:
    - service: ifttt.trigger
      data_template: 
        event: "add_to_playlist"
        value1: "{{ states.media_player.mandark.attributes.media_title }}"
        value2: "{{ states.media_player.mandark.attributes.media_artist }}"

Replace “media_player.mandark” (my universal media player) with “media_player.spotify”, etc, based on the media_player you’re using. Then set up an automation to call the script from a zwave remote or whatever. If you’d like to use this as Alexa intent, you can call the script from there:

RememberSongIntent:
  action:
    - service: script.add_to_playlist
  speech:
    type: plain
    text: "Remembering {{ states.media_player.mandark.attributes.media_title }} by {{ states.media_player.mandark.attributes.media_artist }}"
11 Likes

Thank you a lot for sharing this! Can’t wait to try it out!

This worked great!!
Just sharing my experience while going through the guide: I had trouble firing up the IFTTT event and wondered why. Then I realized I had to type {{Value1}} and {{Value2}} inside of the IFTTT event and not just Value1 and Value2. Probably a newbie-error by me but I thought I’d share it here before anyone else is having problems.

Thank you so much for making this. I combine it with emulated_hue and Alexa routines. So cool to be able to add features to Alexa manually through Home Assistant :slight_smile:

4 Likes

Yeah - it’s probably easiest to use the “ingredient” button rather than type. That’ll input the correct format for passed values.

Hey! I am wanting to do something very similar to this, yet have been struggling the entire day. Im using a trigger on my google assistant (“Save this song”) to initiate the process. I then would like to save the currently playing song to the specified playlist. This is the closest thing i’ve found to my problem on the internet:P
So my question:
What would i need to type into my filter code to simply return the value1 and value2 variables? i have tried using the code provided above but i am not very good with coding. I hope you can help me out!

I’m not really sure what you’re asking. Filter?

Take a look at your media player in the states dev tool to figure out the correct values for “value1/value2”. In my system, I have a media_player called “mandark”, so I can use:

states.media_player.mandark.attributes.media_title
states.media_player.mandark.attributes.media_asrtist

In IFTTT, “Search query” is “Value1” (use the “add ingredient” button"). “Artist name” is “Value2”.

Apologies, the filter code is just were you input your script.
I am using the Spotify player to play my music for me. When i input the code you used above, i get a ton of errors. My question is this:
var value1 = “{{ states.media_player.spotify.attributes.media_title }}”
How do i link this to my media player (if this is even possible?) to return the title and the artist?
Sorry for the questions, i really appreciate your response!

I have no idea what you’re describing here. You’re using the IFTTT component, correct?

Why don’t you post your configuration YAML and the error from your home-assistant.log.

a simple video tutorial would help , am trying this for weeks now can’t get it right . please , please help !

1 Like

how to exactly place this:?

RememberSongIntent:
  action:
    - service: script.add_to_playlist
  speech:
    type: plain
    text: "Remembering {{ states.media_player.mandark.attributes.media_title }} by {{ states.media_player.mandark.attributes.media_artist }}"

You’ll need to set up the Alexa skill or the Google Home skill.

This details using intents with Alexa:

i do have home assistant working but im not sure what automation to use.

That intent is only necessary if you want to be able to ask Alexa to remember the song. You could run script.add_to_playlist from any triggered automation.

1 Like

Just wanted to say thanks for this awesome post, worked great for me!

Since I only use Spotify I went with " Add track to a playlist by TrackID" as the then in IFTTT: In my script I then used this for my value1:

         value1: "{{ state_attr('media_player.spotify', 'media_content_id') | replace('spotify:track:', '') }}"

However, I do have one question. Has anyone figured out if theres any name I can enter as the “playlist name” that makes the track appear in “Liked Songs” and basically putting a :heart: on it? I’ve tried “Liked Songs” but that didn’t work…