XBMC sleep mode playlist

XBMC offers a lot of cool features, most of them I still don’t really understand and don’t use currently. But one of them I just learned to love: smart playlists and python scripts.

I was looking for a plugin that would allow me to setup a playlist with random episodes of tv shows that runs for X minutes and then shuts down the media center (or in my case as I use openELEC just stop playback). To make this part short: I did not find any.

But I found smart playlists and python scripting. So I finally got what I wanted:

A one button solution (well, I extended it to two buttons for more flexibility) that starts a playlist and stops after a specified time. To achieve this, I needed three steps:

  1. Setup a smart playlist that randomly selects episodes from a given set of tv shows
  2. A python script that tells xbmc to play the playlist and starts a timer (asking you for the amount of time) that will stop playback or shutdown xbmc
  3. Mapping the python script to a button on my remote

Smart playlist

This one is quite easy. You can use the GUI version to create a smart playlist or just create a xml file inside Userdata/playlists/video/playlistname.xsp with the following content:

This creates a smart playlist with the name “playlistname” (change this!) with two rules. The first is to select the tv shows and the second one defines that only episodes that have a playcount greater than 0 should be selected (this will only add watched episodes to the playlist – just remove the rule block to disable this). It only adds 50 elements (which is enough for me) and orders them randomly.

You can test the playlist from inside XBMC under the video -> playlists menu.

Python script

This is easier than it sounds. Just create a file named “playplaylist.py” in our userdata folder:

The last three lines are important: The last one sets the timer (named “sleepmode”) and defines the action to perform at the end (Stop the playback – you can also use other XBMC functions here, like XBMC.ShutDown). The previous one starts playback of the playlist (check your playlist name here!) and the first one just cancels a previously started alarm, if available.

Remote control button

So the last step is to link the python script to a remote control button. I’ve added a remote.xml file to the keymaps directory (in your Userdata directory). This file will override the default mappings:

This tells XMBC to start run the python script when the blue button is pressed on the remote. Voila!