iTunes - current track informations as oH-string items

Since my first installation of openHAB1 i was looking for a solution to display some informations like current track title, artist and album played with iTunes (under MacOS) not only on Apple TV but also within oH.

After waisting a lot of time (and money for useless tools) i found A simple server providing a RESTful service for controlling iTunes

By installing this simple server on the same place as my oH-Server as described on the Github-Page some things started to work.

Sample of .items-file (iTunes.items):

String Artist "Artist [%s]"  { http="<[http://192.XXX.XXX.XX:8181/now_playing:6000:JS(Artist.js)]" }
String Title "Titel [%s]"  { http="<[http://192.XXX.XXX.XX:8181/now_playing:6000:JS(Title.js)]" }
String Album "Album [%s]"  { http="<[http://192.XXX.XXX.XX:8181/now_playing:6000:JS(Album.js)]" }
String Playlist "Playlist [%s]"  { http="<[http://192.XXX.XXX.53:8181/now_playing:6000:JS(Playlist.js)]" }

Sample of title-transformation (Title.js):

(function(i) {
    var json = JSON.parse(i);
    return json['name'];
})(input)

Log-entries:

20:40:56.956 [INFO ] [marthome.event.ItemStateChangedEvent] - Playlist changed from null to Musik
20:40:58.124 [INFO ] [marthome.event.ItemStateChangedEvent] - Album changed from null to Die Ärzte (1986)
20:40:58.276 [INFO ] [marthome.event.ItemStateChangedEvent] - Title changed from null to Mysteryland
20:40:58.478 [INFO ] [marthome.event.ItemStateChangedEvent] - Artist changed from null to Die Ärzte

oH-GUI-adaption with the great HABPanel:

Next “easy” step is to control iTunes not only via itself, the remotes, the ios-devices but also with oH.

After reading some postings about Sonos, Plex, XBMC and so on today it seems not so easy to let oH display the cover of current song like the A simple server providing a RESTful service for controlling iTunes provides:

Maybe someone have a solution to refresh this image-item like the text-items above inside oH, especially version 2?

5 Likes

There is a recent PR that was submitted that lets one use the state of a String Item to define the path to an Image or Video. With that you can query your RESTful service for the path to the currently playing Image and put that value into a String Item and put use Image item=NowPlayingCoverItem

1 Like

Thanks, i follow the discussion on Github and hope to use this for my idea. If it works as expected i can add the information how it works here :slight_smile:

1 Like

Hey,

I play a bit with the iTunes API but i can’t figure out where you put your script Title.js

Thanks

for me the path is /oH2/conf/transform

Please note that Javascript-Transformation must be installed.

1 Like

@Heiko_Fanieng

Newbie here, still getting to grips and this and is probably way above what I should be attempting at this early stage, I think i’ve completed the install correctly.

What kind of widget on Panel do you use to get this information back?

None, cause the widget-function of HAB-Panel was released after i did the tests.

Maybe i create one if i solve my pending problem of displaying current artwork.

1 Like

@Heiko_Fanieng

Please excuse my idiocy! I have just realised that my previous questions rely on me creating .js files for the other items.

:blush:

hello ,
can I install the itunes-api on Raspbian GNU/Linux ? or it should be in mac os?

I have it installed on mac osx, but i believe that as long you install the dependencies you should be allright on Raspi or Linux.

1 Like

thanks for your replay ,
after a lot of searching , the itunes-api cannot be installed on other than OSX because its uses AppleScript.

1 Like

hi guys, please help me out:

itunes-api is running ( 10.0.0.2:8181/now_playing )

{"player_state":"playing","id":"C7A5F6029F393868","name":"Rat Race","artist":"Bob Marley and The Wailers","album":"Babylon By Bus [Live] (1978)","playlist":"Meine Lieblingstitel","volume":44,"muted":false,"repeat":"off","shuffle":"songs"}

itunes-api is installed on my mac
openhabian is installed on a raspberry

i created the title.js under etc/openhab2/transform and installed the JavaScript-Transformation

(function(i) {
    var json = JSON.parse(i);
    return json['name'];
})(input)

my item looks like this:

String Title "Titel [%s]"   { http="<[http://10.0.0.2:8181/now_playing:6000:JS(title.js)]" }

sitemap:

Frame label="iTunes Musik"
  {
      Text item=Title
      Text item=Album
      Text item=Artist
      Text item=Playlist
  }

why is it not working ? :frowning: thank you

EDIT:

Got it! - i missed to install the HTTP-Binding

How can i control itunes ? Thanks!

Take a look at iTunes-api, it’s a rest api to iTunes. I am moving over to home assistant, but my openhab setup has itunes/Airplay for 10 zones thru openhab. Works very well. Hit me up if you have questions.

i can only use the GET’s but not the PUT’s, why !?

http://10.0.0.2:8181/playpause

Cannot GET /playpause
Cannot GET /playpause/now_playing
Cannot GET /now_playing/playpause

EDIT:

Got it :slight_smile:

i created itunes.rules

rule "iTunes Play"
		when
			Item iTunesPlay received command ON
		then
    var String command = String::format("curl -X PUT http://xx.xx.xx.xx:8181/play")
executeCommandLine(command)
end

rule "iTunes Pause"
		when
			Item iTunesPlay received command OFF
		then
    var String command = String::format("curl -X PUT http://xx.xx.xx.xx:8181/pause")
executeCommandLine(command)
end

item:

Switch iTunesPlay "Play/Pause"       <soundvolume>

sitemap:

Switch item=iTunesPlay mappings=[OFF="Pause",ON="Play"]

Now it works :slight_smile:

My Example of Controlling the Volume with a OpenHAB Dimmer :slight_smile:

This is only a first Example … I must add if Device is ON than execute Script, else turn Dimmer 0…and perhaps send Error via Telegram :wink:

rule"MAIN Volume"
when
  //DIMMER ITEM
  Item Volume_Main received update
then
  executeCommandLine("/etc/openhab2/scripts/iTunes_Volume.sh " + Volume_Main.state + " Computer")
end

rule"Volume Büro"
when
//DIMMER ITEM
  Item Volume_Buero received update
then
  executeCommandLine("/etc/openhab2/scripts/iTunes_Volume.sh " + Volume_Buero.state + " ID_FROM_AIRPLAY_DEVICE")
end
#!/bin/bash
curl -X PUT -d "level=$1" http://XXX.XXX.XXX.XXX:8181/airplay_devices/$2/volume
1 Like

Here the Example with only 1 Bash Script :slight_smile:

rule"MAIN Volume"
when
  //DIMMER ITEM
  Item Volume_Main received update
then
  executeCommandLine("/etc/openhab2/scripts/iTunes.sh " + "volume " + Volume_Main.state + " Computer")
end

rule"Volume Büro"
when
//DIMMER ITEM
  Item Volume_Buero received update
then
  executeCommandLine("/etc/openhab2/scripts/iTunes.sh " + "volume "+ Volume_Buero.state + " ID_FROM_AIRPLAY_DEVICE")
end

rule"Volume Mute"
when
//SWITCH ITEM
  Item Mute_Item received update
then
  if (Mute_Item.state == ON){
    executeCommandLine("/etc/openhab2/scripts/iTunes.sh " + "mute "+ "true")
  }
  else{
    executeCommandLine("/etc/openhab2/scripts/iTunes.sh " + "mute "+ "false")
  }
end

rule"Shuffle"
when
//NUMBER ITEM
  Item Shuffle_Item received update
then
  if (Shuffle_Item.state == 0) executeCommandLine("/etc/openhab2/scripts/iTunes.sh " + "shuffle "+ "off")
  if (Shuffle_Item.state == 1) executeCommandLine("/etc/openhab2/scripts/iTunes.sh " + "shuffle "+ "songs")
  if (Shuffle_Item.state == 2) executeCommandLine("/etc/openhab2/scripts/iTunes.sh " + "shuffle "+ "albums")
  if (Shuffle_Item.state == 3) executeCommandLine("/etc/openhab2/scripts/iTunes.sh " + "shuffle "+ "groupings")
end

rule"Repeat"
when
//NUMBER ITEM
  Item Repeat_Item received update
then
  if (Repeat_Item.state == 0) executeCommandLine("/etc/openhab2/scripts/iTunes.sh " + "repeat "+ "off")
  if (Repeat_Item.state == 1) executeCommandLine("/etc/openhab2/scripts/iTunes.sh " + "repeat "+ "one")
  if (Repeat_Item.state == 2) executeCommandLine("/etc/openhab2/scripts/iTunes.sh " + "repeat "+ "all")
end
#!/bin/bash

itunesAPI_URL="10.10.80.214"
itunesAPI_Port="8181"

#Ab hier sind keine Änderungen mehr nötig!
case $1 in
    volume)
        curl -X PUT -d "level=$2" http://$itunesAPI_URL:$itunesAPI_Port/airplay_devices/$3/volume
        ;;
    play)
        curl -X PUT http://$itunesAPI_URL:$itunesAPI_Port/play
        ;;
    playpause)
        curl -X PUT http://$itunesAPI_URL:$itunesAPI_Port/playpause
        ;;
    pause)
        curl -X PUT http://$itunesAPI_URL:$itunesAPI_Port/pause
        ;;
    stop)
        curl -X PUT http://$itunesAPI_URL:$itunesAPI_Port/stop
        ;;
    previous)
        curl -X PUT http://$itunesAPI_URL:$itunesAPI_Port/previous
        ;;
    next)
        curl -X PUT http://$itunesAPI_URL:$itunesAPI_Port/next
        ;;
    mute)
        curl -X PUT -d "muted=$2" http://$itunesAPI_URL:$itunesAPI_Port/mute
        ;;
    shuffle)
        curl -X PUT -d "mode=$2" http://$itunesAPI_URL:$itunesAPI_Port/shuffle
        ;;
    repeat)
        curl -X PUT -d "mode=$2" http://$itunesAPI_URL:$itunesAPI_Port/repeat
        ;;
     *)
          echo "Kommando nicht vorhanden!"
          ;;
esac

My current integration of itunes-api
You can read the playlists from iTunes and they are immediately written in a selection of the sitemap :slight_smile:

3 Likes