# Spotify autoplay with mopidy on Raspbian

**URL:** <https://discourse.mopidy.com/t/spotify-autoplay-with-mopidy-on-raspbian/4890>\
**Category:** Help and support\
**Created:** [April 17, 2021, 12:21am UTC](https://discourse.mopidy.com/t/spotify-autoplay-with-mopidy-on-raspbian/4890 "2021-04-17T00:21:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![croustibat91](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@croustibat91](https://discourse.mopidy.com/u/croustibat91)\
**Post date:** [April 17, 2021, 12:21am UTC](https://discourse.mopidy.com/t/spotify-autoplay-with-mopidy-on-raspbian/4890/1 "2021-04-17T00:21:56Z")

</div>

Hello ,

I followed the topic [4270](https://discourse.mopidy.com/t/spotify-stopped-working/4270) to install Mopidy with the MusicBox webclient on my Raspberry.  
My goal is too autoplay my playlist when my Pi is powered on.  
So, I installed mopidy-spotify and launched mopidy as a service, all is OK, I can play my playlist from the webclient.

But, I need your help for the last step.  
I have my URI : spotify:user:dxkzsx0zyohh4digj26tyuzps:playlist:2HeB0wsI7jPikTAJ7XVCzl but I don’t know to use it to autoplay it.  
I saw also the topic [2791](https://discourse.mopidy.com/t/spotify-autoplay-playlist-with-mopidy-running-on-raspbian-desktop/2791) but I don’t understand.  
I must create a bash (called in rc.local I supposed) but I don’t know what to write in this file.

Sorry for my poor level, I really try to understand and I think I’m not so far of my goal now.

Thank you very much for reading me and I hope someone can help me 😊

Have a good day !

Yannick

---

<div class="post-metadata">

**Author:** ![Steve\_Lambert](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/steve_lambert/32/1149_2.png) [@Steve\_Lambert](https://discourse.mopidy.com/u/Steve_Lambert)\
**Post date:** [April 17, 2021, 8:40pm UTC](https://discourse.mopidy.com/t/spotify-autoplay-with-mopidy-on-raspbian/4890/2 "2021-04-17T20:40:45Z")

</div>

Hi, easiest way is to add it to cron, this probably isn’t the best way as it relies on time rather than mopidy actually being up and running - anyway here goes.  
This uses [mpc](https://linux.die.net/man/1/mpc) which you will probably need to install.

assuming you are working in a terminal or ssh

sudo apt update  
sudo apt install mopidy-mpd  
sudo apt install mpc

Once that’s done, type mpc at a prompt and it should list what is playing like this;

pi@mopidy:~ $ mpc  
Luke Wareham;Rachel Mason - Shadows  
[playing] #1/5 0:08/4:30 (2%)  
volume:100% repeat: off random: off single: off consume: off

You can also use it to play your existing playlist above, type into the terminal;

mpc add spotify:user:dxkzsx0zyohh4digj26tyuzps:playlist:2HeB0wsI7jPikTAJ7XVCzl;mpc play

if that’s working you can now add it to cron to start when you reboot.

Open the crontab file in your preferred editor;  
sudo crontab -e  
scroll down to the bottom and add the following.

@reboot sleep 90 ; mpc add spotify:user:dxkzsx0zyohh4digj26tyuzps:playlist:2HeB0wsI7jPikTAJ7XVCzl;mpc play

This waits 90 seconds from reboot before it starts playing, if it doesn’t work make the figure higher, try lowering it to get a quicker time. Save and close it then reboot and listen if it works.

---

<div class="post-metadata">

**Author:** ![Steve\_Lambert](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/steve_lambert/32/1149_2.png) [@Steve\_Lambert](https://discourse.mopidy.com/u/Steve_Lambert)\
**Post date:** [April 17, 2021, 9:19pm UTC](https://discourse.mopidy.com/t/spotify-autoplay-with-mopidy-on-raspbian/4890/3 "2021-04-17T21:19:17Z")

</div>

Alternatively you can try this to add to /etc/rc.local  
Use your choice of editor to open a new file and save it as startup.sh somewhere you can find it, I used /home/pi/startup.sh  
Copy and paste the following into the document - **you will need to put your own playlist details in near the bottom of the script.**

#!/bin/bash  
INI\_\_musicbox\_\_autoplaymaxwait=120  
echo “Waiting for Mopidy to accept connections…”  
waittime=0  
while ! nc -q 1 localhost 6600 \</dev/null;  
do  
sleep 1;  
waittime=$((waittime+1));  
if [$waittime -gt $INI\_\_musicbox\_\_autoplaymaxwait]  
then  
echo “WARNING: Timeout waiting for Mopidy to start, aborting”  
break;  
fi  
done  
if [$waittime -le $INI\_\_musicbox\_\_autoplaymaxwait]  
then  
echo “Mopidy startup complete, playing $INI\_\_musicbox\_\_autoplay”  
mpc add spotify:playlist:put your playlist name here  
mpc play  
fi

Save and exit  
Give it the correct permission

sudo chmod 755 /home/pi/startup.sh

Try and run it in a terminal  
sudo /home/pi/startup.sh  
You should get a line saying something like  
“Waiting for Mopidy to accept connections…  
OK MPD 0.19.0  
Mopidy startup complete, playing”

If thats working then  
sudo nano /etc/rc.local  
and at the bottom, but before exit 0 add  
sudo /home/pi/startup.sh  
then exit, save and reboot to see if it works.

Tried both varieties on mine this evening and both seem to work, hope it does for you.

---

<div class="post-metadata">

**Author:** ![croustibat91](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@croustibat91](https://discourse.mopidy.com/u/croustibat91)\
**Post date:** [April 18, 2021, 5:58pm UTC](https://discourse.mopidy.com/t/spotify-autoplay-with-mopidy-on-raspbian/4890/4 "2021-04-18T17:58:53Z")

</div>

Hello,

Thank you so much for your help. All is perfect (I used the second method with rc.local).  
Thanks again for your perfect answer. It was very clear.

Have a good day ! 🙂

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.mopidy.com/system/32/1_2.png) [@system](https://discourse.mopidy.com/u/system)\
**Post date:** [April 21, 2021, 5:59pm UTC](https://discourse.mopidy.com/t/spotify-autoplay-with-mopidy-on-raspbian/4890/5 "2021-04-21T17:59:01Z")

</div>

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
