I tried to build a script that would allow me to rate (stars) a song in Plex while it’s playing on a Home Assistant media player (eg chromecast etc)
I came up with below script using a rest command to the Plex Server. This is working but requires me to configure my Plex host and token separately. And I need to go into Plex to see what’s the current song rating, I didn’t found the song rating value yet within Home Assistant.
All ideas for improvement are welcome.
To understand below script, I have these helper variables defined:
input_select.media_player
: a drop down to select the name of any of my media players (without the prefix media_player to make it readable in the frontend)input_text.plex_token
: token of my Plex media server (can be found in the url when using Plex website)input_text.plex_host
: hostname of my Plex media serverinput_select.plex_rating
: dropdown with possible rating 1 - 5 stars, or rating can be passed as aservice_data: rating:
element (see dashboard example below)
alias: Rate Plex song
sequence:
- condition: template
value_template: >-
{{state_attr('media_player.' ~
states('input_select.media_player'),'media_content_id') != None}}
- variables:
rating: "{{rating | default('')}}"
rating_to_set: >-
{% if rating == '' %}{{states('input_select.plex_rating')|int * 2}}{%
else %}{{rating|int * 2}}{% endif %}
media_player_entity: media_player.{{states('input_select.media_player') }}
media_player_content_id: "{{state_attr(media_player_entity,'media_content_id')}}"
media_player_content_id_part: "{{media_player_content_id.split('/')[-1]}}"
- service: notify.persistent_notification
continue_on_error: true
data:
title: Rating Plex song
message: >-
Rating Plex song: {{state_attr(media_player_entity,'media_artist')}}:
{{state_attr(media_player_entity,'media_title')}}
({{media_player_content_id_part}}) with
{{rating_to_set}} stars while playing on
{{states('input_select.media_player') }}
- service: rest_command.plex_rating
continue_on_error: true
data:
plex_host: "{{states('input_text.plex_host') }}"
item_id: "{{media_player_content_id_part }}"
rating: "{{rating_to_set}}"
plex_token: "{{states('input_text.plex_token') }}"
enabled: true
mode: single
icon: mdi:plex
Configuration.yaml:
input_text:
plex_host:
initial: !secret plex_host
mode: password
plex_token:
initial: !secret plex_token
mode: password
rest:
plex_rating:
url: 'https://{{plex_host}}:32400/:/rate?key={{item_id}}&identifier=com.plexapp.plugins.library&rating={{rating}}&X-Plex-Token={{plex_token}}&X-Plex-Language=en'
method: PUT
content_type: "application/x-www-form-urlencoded"
Lovelace dashboard buttons to apply rating:
- type: horizontal-stack
cards:
- show_name: true
show_icon: false
type: button
tap_action:
action: call-service
service: script.rate_plex_song
service_data:
rating: 1
entity: script.rate_plex_song
name: ⭐
- show_name: true
show_icon: false
type: button
tap_action:
action: call-service
service: script.rate_plex_song
service_data:
rating: 2
entity: script.rate_plex_song
name: ⭐⭐
- show_name: true
show_icon: false
type: button
tap_action:
action: call-service
service: script.rate_plex_song
service_data:
rating: 3
entity: script.rate_plex_song
name: ⭐⭐⭐
- show_name: true
show_icon: false
type: button
tap_action:
action: call-service
service: script.rate_plex_song
service_data:
rating: 4
entity: script.rate_plex_song
name: ⭐⭐ ⭐⭐
- show_name: true
show_icon: false
type: button
tap_action:
action: call-service
service: script.rate_plex_song
service_data:
rating: 5
entity: script.rate_plex_song
name: ⭐⭐ ⭐⭐⭐