Playing Music Randomly

Hi guys, I want to play my mp3’s randomly without changing their names to numbers, i.e. 1.mp3, 2.mp3, etc, I am currenty using :

media_content_id: http://myi.pad.dr.ess:8123/local/music/{{ range(1,14) |random}}.mp3

but I have to change my song to numbers.

I tried the folder integration and template,

'{{ state_attr(''sensor.music'', ''file_list'')|random }}'

but it gives the file as:

'/config/www/music/mysong.mp3'

how can i change

'/config/www/'

to

' http://myi.pad.dr.ess:8123/local/'

using a template, so I can use the result in:

'media_content_id: result'

I am not on duckdns and everything is rendered locally through the local IP (although I am on nabucasa as well), my music folder listed as a homeassistant available folder in the config and it is also a sensor.

I hope my question is understandable :slight_smile:

Did a quick dev tools experiment so you have something to work with:

{% set t = '/config/www/music/my0music.file.mp3' %}
{% set b = 'https://my-hostname:port' %}
{{ b }}{{ t|replace('config/www/','local/') }}

Outputs: https://my-hostname:port/local/music/my0music.file.mp3

I tried to set

{% set b ='{{ state_attr(''sensor.music'', ''file list'') |random}} ` %}

, but it gives an error in the templates test section under dev tools, Although the right hand side of the equation

`{{ state_attr(''sensor.music'', ''file list'') |random}} `

on its own results in a file folder and location like

'/config/www/music/song.mp3'

for some reason it does not see this as text when I use the “set b=” expression

In the example I posted - b was short for base - eg http:// blah

Taking your original code it would be:

'http://my-host:port{{ state_attr(''sensor.music'', ''file_list'')|random|replace('config/www/','local/') }}'

Thanks, it looks like a good approach, will try it

Thanks, it works :slight_smile:

1 Like