How to play Plex playlist?

I am using Plex Home (with multiple users) and I have few music playlists. However, I don’t know how to use HA to play a playlist from my Plex account. I have tried using Developer Tools > Services and then select…

Domain: media_player
Service: play_media
Service Data: {“entity_id”:“media_player.plex_web_firefox”, “media_content_id”:“360771”, “media_content_type”:“PLAYLIST”}

When I call the service, nothing happened. I suspect media_content_id value I used is wrong. May I know how to fix this? Thanks.

Hi masterkenobi!

[redacted] I totally misunderstood your question! Instead, try using the full URI for the playlist in the content_id field.

I can’t seem to get the service to work, even when specifying these details. I can only get it to work when casting directly from Spotify to media_player.bedroom. But the full track URI is specified in media_content_id. Granted, this is an individual track, not a playlist. This is a bit frustrating.

When play a song in Plex, I can see this Developer Tools > States…

Then, I go to Developer Tools > Services and enter this…

Nothing happens after I press “Call Service” button.

Is this a bug?

The same happens when you try playing a live stream. For example, I can play a TuneIn Radio station on my GoogleHome, and do not get a media_content_type. Or I can play a playlist from Spotify and the media_content_id is the individual track itself.

It seems there is a fair amount of inconsistency with media_player as a service, still.

a jear later and im at the same spot.
Actually I’m only looking for a way to play music while im in the shower.
I’m at

  sequence:
  • service: media_player.play_media
    data:
    entity_id: media_player.bad
    media_content_id: {“playlist_name” : “plex8”, “shuffle”: “0”}
    media_content_type: ‘PLAYLIST’

I think HA doesnt know where this playlist is…

@masterkenobi @SupahNoob @Underknowledge

I am trying to play a movie through a script on the chromecast in my livingroom (woonkamer in Dutch)

plexplay:
  alias: plex play test
  sequence:
    - service: media_player.play_media
      data:
        entity_id: media_player.woonkamer
        media_content_id: "/library/metadata/12323"
        media_content_type: video

For media_content_type I tried video and movie.
Where “video” is given in the example on the component page.
and movie was indicated at the state overview at media_player.woonkamer.

For media_content_id I have tried:

  • /library/metadata/12323
  • { \“library_name\” : \“Movies\”, \“video_name\” : \“Awesomemovie\”, \“shuffle\”: \“0\” }

Did you guys manage to play something from your plex library through the PLAY_MEDIA service?
How did you do it?

Hey guys,
I’m still searching for a better way to cast Plex music playlist to chromecast audio but in the meantime, I’ve started working on a workaround

script

cast:
  alias: Cast
  sequence:
    - service: media_player.play_media
      entity_id: media_player.audio_funk
      data_template:
        media_content_id: "{{states.input_boolean.song.state}}"
        media_content_type: audio/flac

input_boolean

song:
  initial: off
play:
  initial: off

automation

- id: cast
  alias: Cast
  initial_state: True
  trigger:
    - platform: state
      entity_id: media_player.audio_funk
      to: 'idle'
    - platform: state
      entity_id: input_boolean.play
      to: 'on'
  condition:
    - condition: state
      entity_id: input_boolean.play
      state: 'on'
  action:
    - service: shell_command.song
    - service: script.cast
    - delay: '00:00:03'

shell_command

song: /bin/bash -c "/config/song.sh"

bash script

#!/bin/bash
playlist=7516 #Get playlist id: curl -s  http://plex:32400/playlists/all

song=$(curl -s http://PLEX_IP:32400/playlists/$playlist/items \
 |grep "parts"|awk -F '"' '{print "http://PLEX_IP:32400" $4}'|shuf |head -1)

curl -s -X POST -H "Content-Type: application/json" -d '{"attributes": {"friendly_name":  "Song"}, "entity_id": "input_boolean.song", "state": "'"$song"'"}'\
 http://localhost:8123/api/states/input_boolean.song >/dev/null 2>&1

Good day, been trying to get your script to work but it’s incomplete. Basically you need to add the plex-token and HA password. this is where I am:

#!/bin/bash

playlist=21937 #Get playlist id: curl -s http://192.168.1.157:32400/playlists/all?X-Plex-Token=xxxxxx

song=$(curl -s http://192.168.1.157:32400/playlists/$playlist/items?X-Plex-Token=xxxxx |grep "parts"|awk -F '"' '{print "http://192.168.1.157:32400" $4}'|shuf |head -1)

curl -s -X POST -H "x-ha-access: XXXXXX" -H "Content-Type: application/json" -d '{"attributes": {"friendly_name": "Juan Playlist"}, "entity_id": "input_boolean.plist01", "state": "'"$song"'"}'\

 http://192.168.1.157:8123/api/states/input_boolean.plist01 >/dev/null 2>&1

when I run the curl request I get the location string as below example:

``http://192.168.1.157:32400/library/parts/8709/1532892435/file.m4a.`

this then needs to be posted with your 3rd curl command. but the string also needs to be sent with the token like so:

http://192.168.1.157:32400/library/parts/8712/1532892458/file.m4a?X-Plex-Token=XXXXXX
typing this in my browser plays the song :slight_smile:

The modified second command is this:

song=$(curl -s http://192.168.1.157:32400/playlists/$playlist/items?X-Plex-Token=xxxxx |grep "parts"|awk -F '"' '{print "http://192.168.1.157:32400" $4 echo "?X-Plex-Token=xxxxxxxxx"}'|shuf |head -1)

this output is ready to play in any browser.

one question I have is shouldn’t this be posted as an input_text, which would carry the entire location string?

thank you again for sharing this.

I have plex, hass and a few other services running in docker on the same machine, so I can get away without using any token/authentification
You can post the song url into almost any sensor state, I’m using boolean because it looks cleaner and it’s easier to reset

It’s cool to see improvements added to it :slight_smile: that’s the beauty of open source and so maybe we can make it to display the song info in hass

Thank you. Im also running several docker containers in the same machine, but perhaps as Im using Nginx it doesnt let me access without the token. Not sure why, but didn’t work without it.

Another issue is that your automaton triggers again when the player goes to idle. I guess it works on Chromecast, any suggestion for other players? I use Bose Soundtouch and the state remains playing after the song is finished. So no re-triggering.

Did some digging and it turns out that you can disable auth

  • Plex has an option under settings > network > “List of IP addresses and networks that are allowed without auth”
  • Hass has http > trusted_networks

If your player state doesn’t change to idle maybe you can check media_duration vs media_position or do some math based on when the automation was triggered vs media_duration

off topic…what other goodies do you have in docker?I’m always looking for new ideas

Thank you. this is what Im using:

- portainer
- mqtt
- motioneye
- appdaemon
- cloud9
- hadockermon
- glances
- postgres
- facebox
- influxdb
- grafana
- tasmoadmin
- letsencrypt
- duckdns
- filebrowser
- organizr
- heimdall
- onlyoffice
- nextcloud
- plex
- booksonic
- guacamole
- telegraf
- transmission
- radarr  
- sonarr
- jackett
- syncthing
- timemachine

Also tried pi-hole but turned it off because it was restricting everything.

EDIT: I managed to fix the error code 52. Found the right position for the script file. Now there’s something wrong somewhere else, because I hear the Google Home speaker activating but no song is played. I already added the token as suggested by @juan11perez (thanks!) and I can listen to the song in the browser. @zanerv, is your solution working fine? Or did you change something else after this?
Does anyone know how to troubleshoot something like this?
Thanks.

OLD MESSAGE: Where did you put the song.sh file? I tried putting it in /usr/share/hassio/homeassistant/ but I get error code 52 when I try to execute the shell command /bin/bash -c “/config/song.sh”

In scripts.yaml:

run_playlist_on_plex_test2:
  sequence:
  - service: media_player.play_media
    data:
      media_content_id: '{ "playlist_name": "Playlist1", "shuffle": "1" }'
      media_content_type: PLAYLIST
    target:
      entity_id: media_player.plex_windows_hp455
  mode: single
  alias: Запустить плейлист plex test2
  icon: mdi:playlist-play

2 Likes