Itunes Playlist Can my config be simplified?

Hi,
I have managed to get the the integration to Itunes and can now select airplay speakers etc.
I want to select a playlist and have managed this with the following code.
To select playlist in configuration.yaml:
CODE

itunesplaylist:
name: Itunes Playlist
options:
- ABreakfast
- Sugar Man
initial: ABreakfast

and in automations.yaml

######## Itunes Play Lists Automations #################

  • alias: Play Playlist ABreakfast
    trigger:
    platform: state
    entity_id: input_select.itunesplaylist
    to: “ABreakfast”
    action:
    • service: media_player.play_media
      data:
      entity_id: media_player.itunes
      media_content_id: “ABreakfast”
      media_content_type: playlist
  • alias: Play Playlist Sugar Man
    trigger:
    platform: state
    entity_id: input_select.itunesplaylist
    to: “Sugar Man”
    action:
    • service: media_player.play_media
      data:
      entity_id: media_player.itunes
      media_content_id: “Sugar Man”
      media_content_type: playlist

This works but it seems very longwinded.
Is there a way to take the selection from options and pass it to the

media_content_id: as a variable?

Hope some experts here have a simpler solution?
Clive

Hi there, it’s 3 months later, I know, but I’ve also been noodling around with iTunes lately. I cobbled this together, might interest you.

The 2nd version is a bit shorter, but requires that your input_select has ugly state names in it.

######## Test Input Select with variable #################
input_select:
  itunesplaylist_variable:
    name: Itunes Playlist variable
    options:
      - Block Party
      - Radio 4
      - TalkSport
    initial: Radio 4
    icon: mdi:music
automation:
  - alias: Start radio station variable
    trigger:
      - platform: state
        entity_id: input_select.itunesplaylist_variable
    action:
      service: media_player.play_media
      data_template:
        entity_id: media_player.itunes
        media_content_id: >
          {% if is_state("input_select.itunesplaylist_variable", "Block Party") %}
            block-party
          {%-elif is_state("input_select.itunesplaylist_variable", "Radio 4") %}
            radio4
          {%-elif is_state("input_select.itunesplaylist_variable", "TalkSport") %}
            talksport
          {% else %}
            none
          {% endif %}
        media_content_type: playlist
      
      
######## Test Input Select with variable test 2 - use variable as media_content_id #################

input_select:
  itunesplaylist_variable_test2:
    name: Itunes Playlist variable 2
    options:
      - block-party
      - radio4
      - talksport
    initial: radio4
    icon: mdi:music
automation:
  - alias: Start radio station variable test 2
    trigger:
      - platform: state
        entity_id: input_select.itunesplaylist_variable_test2
    action:
      service: media_player.play_media
      data_template:
        entity_id: media_player.itunes
        media_content_id: > 
          {{ states('input_select.itunesplaylist_variable_test2') }}
        media_content_type: playlist

Hi, can you help me to configure itunes. It looks very simple but my icon in HA remains offline.
I wrote in configuration yaml

  • platform: itunes
    name: iTunes
    host: 192.168.178.41
    port: 8181

If you visit 192.168.178.41:8181 do you see a webpage about the iTunes API?

Solved. Now i see a webpage itunes server but in HA, itunes is always offline

Hmm, strange, the media player should just work. Is there anything about iTunes in your log?

Fantastic! Hi, I solved my problem. Now itunes works great.
Thank you for sharing the way to recall playlists in HA.
I added the shuffle button with the following configuration:

config.yaml


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

Can you tell me how can I get the status update? If I enable SHUFFLE from itunes, I would like it to update also in HA.
Thnak’s!

Sorry I don’t think it is possible to get the shuffle status.

You might be able to work around it by creating a smart playlist which has the songs shuffled e.g. ‘Album = xxx, Limit to 25 selected randomly’.

I do something similar with a script named ‘Block Party’, which chooses ‘x number of songs rated over y stars from each artist in z playlist’. It’s kind of like shuffle but for many albums at once. https://dougscripts.com/itunes/scripts/ss.php?sp=blockparty

But my shuffle command works great without having to create a mixed playlist. The problem is that if I activate shuffle from HA it updates iTunes. If instead I activate or turn off shuffle from itunes it does not update HA. Do you have any idea how to change my command?

Someone else may have an idea but as far as I know you can’t find out from iTunes if it is shuffling or not, so you’ll never be able to update HA correctly.

Actually I was fiddling with something else, and I noticed you can actually get the shuffle status via the ‘NowPlaying’ resource.

If you do curl -X GET http://192.168.0.1:8181/now_playing it will return a json with “shuffle”: false or “shuffle”: true

This is an image while itunes is in shuffle mode

Here’s what it looks like on mine:

So how could you write in HA a command that updates the status of itunes?

You’d use a rest sensor: https://www.home-assistant.io/components/sensor.rest/

The format to get the correct json data is quite fiddly IMO, there might be some trial and error.

I was fiddling around with this recently. Just in case you haven’t figured it out yet, these go in your sensor.yaml:

  - platform: template
    sensors:
      itunes_shuffle_status:
        friendly_name: "iTunes shuffle status"
        value_template: >-
          {% if is_state('sensor.itunes_shuffle_status_raw', 'False') %}
            Off
          {% elif is_state('sensor.itunes_shuffle_status_raw', 'songs') %}
            On
          {% else %}
            Unknown
          {% endif %}
  - platform: rest 
    name: iTunes shuffle status raw
    resource: http://192.168.0.2:8181/now_playing
    value_template: '{{ value_json.shuffle }}'

I’ve tried, but it doesn’t work. My sensor appears to be off
54

Hmm that’s odd. It’s working for me. It correctly displays shuffle status, and defaults to ‘Unknown’ if iTunes doesn’t have a song or playlist selected (e.g. when you first launch iTunes, and there is only an Apple and no text in the info window).

You’ll have to see what values are being captured by your rest sensor (the value_json.shuffle part). Is that part working?

How do you know if it works?

Look in your ‘states’ page and find sensor.itunes_shuffle_status_raw (or whatever yours is called). You should be abel to see the state changing as you click the shuffle icon in iTunes.