Hi all,
As a newbie in HA I am enjoying discovering all the possible features (and probably will never master it!). However, I got a little bit frustrated not be be able to decently control local MP3 casting (while Spotify integration works perfectly for instance - but I don’t want to get on account), as I can only play local media one by one (maybe I missed something, but most of the posts I saw was about playing a particular music once). I tried the Plex server solution in docker, in an optimistic way (especially since the last release of HA) but get disapointed as I failed in properly casting my music to my google Nest. Hence, I came up with some solution based on scripts and automation.
The first step is to put the music in a folder inside config/www
(/config/www/media
in my case) so that it can be accessed through local
shortcut (but needs to go through DuckDNS forwarding). Then, to extract the file list (and ensuring that only mp3’s are in the folder), I set a folder sensor
in configuration.yaml
:
sensor:
- platform: folder
folder: /config/www/media
which also requires
homeassistant:
allowlist_external_dirs:
- /config/www/media
After this first step, I set 4 scripts. The first one aims at initiating the process:
lancement_musique:
alias: Lancement musique
sequence:
- service: script.lecture_musique
data:
piste: '{{ liste[18:] }}'
mode: single
icon: mdi:music
variables:
liste: '{{ state_attr(''sensor.media'', ''file_list'')|random }}'
You can note that the music is randomized. Also, I remove the first 18 characters that correspond actually to the folder (as the “file_list” attribute returns the full path).
So this script controls the “lecture_musique” script that actually casts the music to my nest in the living room:
lecture_musique:
alias: Lecture Musique
sequence:
- service: media_player.play_media
data:
media_content_id: https://<HA URL>.duckdns.org:<PORT>/local/media/{{ piste
}}
media_content_type: audio/mp3
entity_id: '{{ lecteur }}'
mode: single
icon: mdi:music
variables:
piste: <DEFAULT FILE>
lecteur: media_player.nest_salon
The last two scripts are the controls for play/pause and stop:
arret_musique:
alias: Arrêt musique
sequence:
- service: media_player.turn_off
data:
entity_id: '{{ lecteur }}'
mode: single
icon: mdi:stop
variables:
lecteur: media_player.nest_salon
basculer_musique:
alias: Basculer Musique
sequence:
- service: media_player.media_play_pause
data:
entity_id: '{{ lecteur }}'
mode: single
icon: mdi:play-pause
variables:
lecteur: media_player.nest_salon
OK… But wait, what happen when a track has finished? It will stop! And this is where an automation detects when the player changes its state and recall the first script:
- id: '<AUTOMATION ID>'
alias: Musique_suivante
description: ''
trigger:
- platform: state
entity_id: media_player.nest_salon
from: playing
to: idle
condition:
- condition: template
value_template: '{{ ''https://<HA URL>.duckdns.org:<PORT>/local/media/'' in state_attr(''media_player.nest_salon'',
''media_content_id'') }}'
action:
- service: script.lancement_musique
data: {}
mode: single
In order not to trigger this if another type of media is cast, it detects through media_content_id
that the file comes from our local folder.
And Lovelace integration:
Like this, I can have a shuffled, infinitely playing playlist (which I initially wanted), without defining a hard-coded playlist. There are of courses many possible enhancements (no shuffling, track selection etc.), but I think it solves stuff for which I have spent long time in trying to find a solution, including digging into the web…
Please do not hesitate to give your thoughts or idea. As I said, I’m really new in HA and I surely missed something!
Mick
Edit (02/11/2020): corrected a mistake regarding the correct folder to store music