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”
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)
- 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
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:
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.
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:
- platform: command_line
switches:
luca_barbarossa:
friendly_name: Luca Barbarossa
command_on: “curl -X PUT http://192.168.178.41:8181/playlists/luca-barbarossa/play”
I’ll give it a try with the apple generated ones.
Tried it, works fine
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:
- platform: command_line
switches:
shuffle:
friendly_name: Shuffle Mode
command_on: “curl -X PUT http://192.168.178.41:8181/shuffle -d mode=on”
command_off: "curl -X PUT http://192.168.178.41:8181/shuffle -d mode=off "
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"]}}'
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…
There’s no good reason as the iTunes API supports it. You can vote on my feature request here if you like:
For sure, Voted!
BTW I tried the switch but it didn’t update status when I toggled on iTunes or remote. Apparently the standard switch component only updates when used by hass. So I don’t think the value template is doing much here as even without it, it would be in sync after 1 use anyway.
I’m trying to see if we can move over to a RESTful switch which apparently does poll the state with the value template. But I can’t even get it to appear in my switches or logs for some reason. Will have to mess with it some more
Alright, so you inspired me to finally get around to making a shuffle that properly reports the state of the shuffle on the media player even if it gets changed from outside of hass and turns it on/off when toggled. If you change the state from outside hass via iTunes or Remote, it takes 5-10 seconds to sync, that’s just how long it takes itunes-api to do it. A better way to do this might be with a RESTful switch, but I got tired of banging my head against that wall and did it this way.
Add the following to configuration.yaml:
the commands that turn shuffle off and on:
rest_command:
shuffle_on:
url: "http://[YOUR_ITUNES-API_SERVER_IP]:8181/shuffle"
method: put
content_type: "application/x-www-form-urlencoded"
payload: "mode=songs"
shuffle_off:
url: "http://[YOUR_ITUNES-API_SERVER_IP]:8181/shuffle"
method: put
content_type: "application/x-www-form-urlencoded"
payload: "mode=off"
the input boolean you will use as a switch in your ui:
input_boolean:
itunes_shuffle:
name: "Shuffle"
icon: mdi:shuffle-variant
The automations which make sure it displays the correct status if someone changes it in iTunes or Remote rather than via homeassistant and which make the service calls when the input boolean is toggled:
automation:
- id: turn_on_shuffle_from_input_boolean
alias: turn_on_shuffle_from_input_boolean
initial_state: 'true'
trigger:
platform: state
entity_id: input_boolean.itunes_shuffle
from: 'off'
to: 'on'
condition:
condition: template
value_template: '{{ state_attr("media_player.itunes", "shuffle") == false}}'
action:
service: rest_command.shuffle_on
- id: turn_off_shuffle_from_input_boolean
alias: turn_off_shuffle_from_input_boolean
initial_state: 'true'
trigger:
platform: state
entity_id: input_boolean.itunes_shuffle
from: 'on'
to: 'off'
condition:
condition: template
value_template: '{{ state_attr("media_player.itunes", "shuffle") == true}}'
action:
service: rest_command.shuffle_off
- id: sync_shuffle_input_boolean_to_external_on
alias: sync_shuffle_input_boolean_to_external_on
initial_state: 'true'
trigger:
- platform: state
entity_id: media_player.itunes
- platform: homeassistant
event: start
condition:
condition: and
conditions:
- condition: template
value_template: '{{ state_attr("media_player.itunes", "shuffle") == true}}'
- condition: state
entity_id: input_boolean.itunes_shuffle
state: 'off'
action:
service: input_boolean.turn_on
data:
entity_id: input_boolean.itunes_shuffle
- id: sync_shuffle_input_boolean_to_external_off
alias: sync_shuffle_input_boolean_to_external_off
initial_state: 'true'
trigger:
- platform: state
entity_id: media_player.itunes
- platform: homeassistant
event: start
condition:
condition: and
conditions:
- condition: template
value_template: '{{ state_attr("media_player.itunes", "shuffle") == false}}'
- condition: state
entity_id: input_boolean.itunes_shuffle
state: 'on'
action:
service: input_boolean.turn_off
data:
entity_id: input_boolean.itunes_shuffle
trying to get the server / api working and getting an ‘Internal Server Error’ on any test ‘curl’
MacOS 10.15.7
Error: Command failed: osascript -l JavaScript -e 'var old = console.log; console.log = function () { Array.prototype.unshift.call(arguments, "<brandonhorst:node-osa>"); Array.prototype.push.call(arguments, "</brandonhorst:node-osa>"); old.apply(console, arguments); }; JSON.stringify((function getPlaylistsFromItunes(){ itunes = Application('\''iTunes'\''); playlists = itunes.playlists(); playlistNames = []; for (var i = 0; i < playlists.length; i++) { playlist = playlists[i]; data = {}; data['\''id'\''] = playlist.id(); data['\''name'\''] = playlist.name(); data['\''loved'\''] = playlist.loved(); data['\''duration_in_seconds'\''] = playlist.duration(); data['\''time'\''] = playlist.time(); playlistNames.push(data); } return playlistNames;})());'
execution error: Error: Error: Application can't be found. (-2700)
at ChildProcess.exithandler (child_process.js:308:12)
at ChildProcess.emit (events.js:315:20)
at maybeClose (internal/child_process.js:1048:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5) {
killed: false,
code: 1,
signal: null,
cmd: `osascript -l JavaScript -e 'var old = console.log; console.log = function () { Array.prototype.unshift.call(arguments, "<brandonhorst:node-osa>"); Array.prototype.push.call(arguments, "</brandonhorst:node-osa>"); old.apply(console, arguments); }; JSON.stringify((function getPlaylistsFromItunes(){ itunes = Application('\\''iTunes'\\''); playlists = itunes.playlists(); playlistNames = []; for (var i = 0; i < playlists.length; i++) { playlist = playlists[i]; data = {}; data['\\''id'\\''] = playlist.id(); data['\\''name'\\''] = playlist.name(); data['\\''loved'\\''] = playlist.loved(); data['\\''duration_in_seconds'\\''] = playlist.duration(); data['\\''time'\\''] = playlist.time(); playlistNames.push(data); } return playlistNames;})());'`
}
'[2020-11-27T21:32:27.081Z] - ::ffff:10.10.100.60 - GET /playlists 500 77.727 ms - 21b'
Anythonghts ? I just having running with the script/server , tried it with SUDO also same results.
Might have found my answer…
is this true?
The API will only work with iTunes 12.x or lower.
if it is is there a solution for higher releases with Music not iTunes?
Seems to be TRUE. I found a way to INSTALL iTunes on Catalina.
worked on my Mac mini and now the API is responding to CURL
It is possible to use itunes-api with Catalina’s music app according to this thread, but I would recommend you read my guide on forked-daapd. I switched from iTunes to forked-daapd and will never go back. Read my guide and see why