Skip to content

Instantly share code, notes, and snippets.

@joe248
Last active December 23, 2022 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joe248/14f4b2619fa5fa61d9d05944434de7cf to your computer and use it in GitHub Desktop.
Save joe248/14f4b2619fa5fa61d9d05944434de7cf to your computer and use it in GitHub Desktop.
Home Assistant Playlist Package
# Media is stored in .homeassistant/media/
# To use this in your own installation:
# 1. Replace all 'media_player.43_tcl_roku_tv' entities with your media player
# 2. Create a script for each playlist with the tracks you want
# 3. Edit the roku_finished_playing_track automation to conform to your media player, triggering whenever it finishes playing a track
# 4. Start playing a playlist by calling the script for that playlist
input_select:
current_playlist:
options:
- 'none'
initial: 'none'
script:
play_frozen_soundtrack:
alias: Play Frozen Soundtrack
sequence:
- service: script.play_playlist
data:
tracks:
- "Frozen Soundtrack/01. Frozen Heart.mp3"
- "Frozen Soundtrack/02. Do You Want To Build A Snowman.mp3"
- "Frozen Soundtrack/03. For The First Time In Forever.mp3"
- "Frozen Soundtrack/04. Love Is An Open Door.mp3"
- "Frozen Soundtrack/05. Let It Go.mp3"
- "Frozen Soundtrack/06. Reindeer(S) Are Better Than People.mp3"
- "Frozen Soundtrack/07. In Summer.mp3"
- "Frozen Soundtrack/08. For The First Time In Forever (Reprise).mp3"
- "Frozen Soundtrack/09. Fixer Upper.mp3"
- "Frozen Soundtrack/10. Let It Go.mp3"
play_playlist:
alias: Play playlist
sequence:
# update playlist input_select with new tracks
- service: input_select.set_options
target:
entity_id: input_select.current_playlist
data:
# Prepend the base media URL in front of each track
options: "{{ tracks | map('regex_replace', '^(.*)$', base + '\\\\1') | list }}"
# select and play the first track
- service: input_select.select_first
target:
entity_id: input_select.current_playlist
- service: media_player.play_media
data_template:
media_content_id: "{{ states('input_select.current_playlist') }}"
media_content_type: url
target:
entity_id: media_player.43_tcl_roku_tv
variables:
base: "media-source://media_source/local/"
tracks: []
play_previous_track_in_playlist:
alias: Play Previous Track in Playlist
sequence:
# If we're on the first playlist item, do nothing
- condition: template
value_template: >-
{{ state_attr("input_select.current_playlist","options").index(states('input_select.current_playlist')) > 0 }}
# Select the previous track and play it
- service: input_select.select_previous
target:
entity_id: input_select.current_playlist
- service: media_player.play_media
data_template:
media_content_id: "{{ states('input_select.current_playlist') }}"
media_content_type: url
target:
entity_id: media_player.43_tcl_roku_tv
play_next_track_in_playlist:
alias: Play Next Track in Playlist
sequence:
# If we're on the last playlist item, do nothing
- condition: template
value_template: >-
{{ state_attr("input_select.current_playlist","options").index(states('input_select.current_playlist'))
< state_attr("input_select.current_playlist","options") | count - 1 }}
# Select the next track and play it
- service: input_select.select_next
target:
entity_id: input_select.current_playlist
- service: media_player.play_media
data_template:
media_content_id: "{{ states('input_select.current_playlist') }}"
media_content_type: url
target:
entity_id: media_player.43_tcl_roku_tv
automation:
- id: roku_finished_playing_track
alias: Roku Finished Playing Track
trigger:
- platform: state
entity_id: media_player.43_tcl_roku_tv
from: playing
to: 'on'
# HA 2022.3 seems to have broken the above trigger. The Roku media player doesn't seem to consistently change state when a track is over. It just stays on 'playing'. So add another trigger based on when the track gets near the end
- platform: template
value_template: >-
{{ (state_attr("media_player.43_tcl_roku_tv", "media_duration") | int(default=1000) - state_attr("media_player.43_tcl_roku_tv", "media_position") | int(default=0)) < 10 }}
for: 00:00:10
condition:
# Only proceed if we're on the PlayOnRoku app
- condition: state
entity_id: media_player.43_tcl_roku_tv
attribute: app_id
state: '15985'
# This should filter out the case of the media player immediately jumping to the next track after going to a new track, by making sure that a track is loaded and (just finished) playing
- condition: template
value_template: >-
{{ state_attr("media_player.43_tcl_roku_tv", "media_duration") | int(default=0) > 0 }}
action:
- service: script.play_next_track_in_playlist
recorder:
exclude:
entities:
- input_select.current_playlist
@vjmaz
Copy link

vjmaz commented Dec 23, 2022

So to better understand above for simple people :
the first section : input_select total 4 lines is added in Config file
then you create a script
then create and automation

Does this sound right

can you elaborate a little more !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment