iTunes Switches for AirPlay or Playlists

@Maddox Hi Jon, I was wondering if there’s a way to use your iTunes API and the Rest Switch to control both AirPlay devices and/or Playlists via a switch. Using your examples, shouldn’t these 2 work? Or is there another way to do this?

switch:

1 Like

The restful switch won’t work, because it uses POST for it’s requests, without an option to change it. iTunes API uses PUT for commanding things like playlist switching and toggling airplay device state.

You could instead accomplish this with the command line switch:

switch:
  - platform: command_line
    switches:
      bedroom_airplay:
        friendly_name: Bedroom Airplay
        command_on: "curl -X PUT http://192.168.1.10:8181/airplay_devices/63-22-fa-1f-f5-d4/on"
        command_off: "curl -X PUT http://192.168.1.10:8181/airplay_devices/63-22-fa-1f-f5-d4/off"
      outkast:
        friendly_name: Outkast the 90s Playlist
        command_on: "curl -X PUT http://192.168.1.10:8181/playlists/outkast-the-90s/play"

But, you do realize there’s a component inside HA for iTunes API right? When configured, it will expose your AirPlay devices as speakers with the ability to just turn them on and off. You can also send an event to the main iTunes accessory to set it’s playlist with media_player.play_media

Watch your URLs, you have and extra slash in the paths in your example that would cause issues. Also, you’re pointing at the GET URLs. Using the body with on/off and play wouldn’t do anything, the URLs need the full path as written in the docs.

Hope this helps!

1 Like

Thanks Jon! I didn’t realize I could set a playlist that way. I’ll look into it tonight.

The reason I was looking into using switches is two-fold. First, I was wanting to create a playlist group that contains a page of playlists I can toggle on and off… Second, since 0.29 broke the iTunes Media Player (not your API, but the iTunes component is always offline now) I was thinking I could build this as an interim solution until it gets fixed as I just picked up an Amazon Echo and wanted to integrate some voice commands into my current airplay setup :slight_smile:

Thanks again for your help!

Ah, I didn’t know it was broken. A mention of that is more helpful. Thanks.

Just to follow-up. It turns out it wasn’t broken, it’s that I had http://192.168.1.10 when I should just use the IP address ie 192.168.1.10

Also I managed to get the switches to also update their states and thought I’d post for others. This example supposes I have a an airplay device and a playlist called “The A List: Rock”

platform: command_line
   switches:
    bathroom_airplay:
      friendly_name: Bathroom Airplay
      command_on: "curl -X PUT http://192.168.1.10:8181/airplay_devices/90-84-0d-f1-04-74/on"
      command_off: "curl -X PUT http://192.168.1.10:8181/airplay_devices/90-84-0d-f1-04-74/off"
      command_state: "curl -X GET http://192.168.1.10:8181/airplay_devices/90-84-0d-f1-04-74"
      value_template: '{{ value_json["selected"] }}'    
    a_list_rock:
      friendly_name: Rock
      command_on: "curl -X PUT http://192.168.1.10:8181/playlists/the-a-list-rock/play"
      command_off: "curl -X PUT http://192.168.1.10:8181/pause"
      command_state: "curl -X GET http://192.168.1.10:8181/now_playing"
      value_template: '{{ value_json["playlist"] == "The A List: Rock" }}'

It’s not a switch but I found rest_command very useful to control iTunes Playlists from scripts or automations

rest_command:
  mozart225_playlist:
      url: 'http://192.168.0.13:8181/playlists/mozart-225/play'
      method: PUT
      payload: '{"shuffle":true}'

script: 
  mozart225_shuffle:
    alias: "Mozart 225 Shuffle"
    sequence:
      - service: rest_command.mozart225_playlist

A group playlists let’s me turn on any of these scripts to start the music:

  playlists:
    name: Playlists
    entities:
      - script.karljenkins_shuffle
      - script.klassikradio_live
      - script.chillout_shuffle
      - script.mozart225_shuffle

Cheers,
Florian

Do you have any examples on how to select a playlist with the HA iTunes component?

When I try the rest_command mentioned by macmurph42 the payload is settings are completely ignored.

Any Idea what the url is like when you want to toggle shuffle mode ?

Documentations of iTunes-api reports only the following:

PUT /shuffle [mode=songs] => NowPlayingResource
PUT /shuffle [mode=off] => NowPlayingResource

I couldn’t figure out how to add the mode parameter in the url.

1 Like

Works like this:
command_on: “curl -X PUT http://192.168.X.XX:8181/shuffle -d mode=on”
command_off: “curl -X PUT http://192.168.X.XX:8181/shuffle -d mode=off”

1 Like

Hi all, if anyone is interested, I have made some little buttons in lovelace using the custom button card that change the playlist to the one listed on the button. Maybe it will save some of you some time to do the same. There’s probably a cleaner way to do this with the rest_command, but this worked.

So first I defined the switches in my configuration.yaml pretty much as described above, except I made both toggle on and toggle off assigned to turn the playlist on. If I want a different playlist, I will just click that button, so the playlist button should always assign the chosen playlist regardless of state.

switch:
  - platform: command_line
    switches:
      mix:
        friendly_name: Mix
        command_on: "curl -X PUT http://x.x.x.x:8181/playlists/a-45-star/play"
        command_off: "curl -X PUT http://x.x.x.x:8181/playlists/a-45-star/play"
  - platform: command_line
    switches:
      jazz:
        friendly_name: Jazz
        command_on: "curl -X PUT http://x.x.x.x:8181/playlists/best-jazz/play"
        command_off: "curl -X PUT http://x.x.x.x:8181/playlists/best-jazz/play"
  - platform: command_line
    switches:
      rock:
        friendly_name: Rock
        command_on: "curl -X PUT http://x.x.x.x:8181/playlists/best-rock/play"
        command_off: "curl -X PUT http://x.x.x.x:8181/playlists/best-rock/play"
  - platform: command_line
    switches:
      pop:
        friendly_name: Pop
        command_on: "curl -X PUT http://x.x.x.x:8181/playlists/best-pop/play"
        command_off: "curl -X PUT http://x.x.x.x:8181/playlists/best-pop/play"

Then in ui-lovelace.yaml I defined the buttons as below (4 simple small text buttons in a row, but much more fancy ones are possible. I will probably make mine nicer someday)

buttons

  - type: horizontal-stack
    cards:
    - type: "custom:button-card"
      name: Mix
      action: service
      service:
        domain: switch
        action: toggle
        data:
          entity_id: switch.mix
    - type: "custom:button-card"
      name: Jazz
      action: service
      service:
        domain: switch
        action: toggle
        data:
          entity_id: switch.jazz
    - type: "custom:button-card"
      name: Rock
      action: service
      service:
        domain: switch
        action: toggle
        data:
          entity_id: switch.rock
    - type: "custom:button-card"
      name: Pop
      action: service
      service:
        domain: switch
        action: toggle
        data:
          entity_id: switch.pop
1 Like

Hi all,

I was playing around with this some more and it turns out that there’s actually nothing wrong with calling the service for play_media on media_player_itunes directly to change the playlist. No curl or shell commands needed. The following works fine:

image

And so you can create a button with the lovelace custom button card mentioned in my previous post as below (tested and working).

    - type: "custom:button-card"
      name: Jazz
      action: service
      service:
        domain: media_player
        action: play_media 
        data:
          entity_id: "media_player.itunes"
          media_content_id: "best-jazz"
          media_content_type: "playlist"

Next I’m going to have it show which playlist is active using input_boolean and a script which you pass the playlist name in with a variable…
Then I’ll try an input_select for the subgenre of the selected genre… Will report back when I have something working.

1 Like

Thanks scstraus, that is really useful!

I’m also trying to play from Apple generated playlists (e.g. “Favourites mix”) but can’t get it to work.
Also I can’t find a way to access playlists that contain spaces (standard fixes like adding a forward slash or %20 don’t work).
Does anyone have an idea?

You can see all the ones I defined in my config which I published to github… Some of them maybe had spaces but I generally avoid spaces exactly for reasons like this.

If you copy paste the name it says at http://YOUR_SERVER_IP:8181/playlists
it should work, at least it always has for me.

Hi, if you want to play a playlist that contains spaces, just type the - between one name and another.
Example:

1 Like

I’ll give it a try with the apple generated ones.

Tried it, works fine :slight_smile:

Can you help me?
So I added the shuffle function to my itunes.
How to get the status update? If I activate the shuffle function from itunes I would like it to activate also in HA.
My configuration is:

switch:

customize:
switch.shuffle:
icon: mdi:shuffle
assumed_state: false
retain: false

I haven’t figured this out yet unfortunately. I have opened a feature request to get it supported officially, you can go vote for that.

Not sure if you still need it but here is what I have and it works.

switch:
  - platform: command_line
      shuffle:
        friendly_name: Shuffle Mode
        command_on: "curl -d 'mode=songs' -H 'Content-Type: application/x-www-form-urlencoded' -X PUT http://192.168.1.24:8181/shuffle"
        command_off: "curl -d 'mode=off' -H 'Content-Type: application/x-www-form-urlencoded' -X PUT http://192.168.1.24:8181/shuffle"
        value_template: '{{ media_player.itunes.attributes["shuffle"]}}'

1 Like

Wow well done, I moved on to other things and never came back to it, but it will be very nice to have ! Surprising that there’s an attribute for something there’s no service call for, I’m not sure I ever would have noticed that.

Yeah not sure why this is not built into the media player by default…