How to read text file and add to input select

Hi everyone, is there anyone can help or give some hints on this?

I have a text file locate in www folder. The content was mp3 file name

I want to read them and automaticaly add to input select. So that it can helps me to get the mp3 url.
Maybe I need a sensor to read the text file then use a script to expand the state of the sensor and set vaile to input select.
But I’m not sure how to read the text file and if there is any other solution.

Thanks in advance

There is a MUCH easier way to do this if you just need a list of the file names and the files reside in a folder on your Home Assistant server. The problem I had with the solution @123 suggested is that I need to match a URL with a name in a CVS file.

In this case, to just get a list of file names you can use the folder sensor. The folder contents are listed in the attributes (you can set filters on the sensor if necessary).

Then use an automation to populate the input select with the file names. Note the folder path must be listed in allowlist_external_dirs if it doesn’t reside in /www (which is included automatically)

The input select will populate on a restart and then anytime the folder contents change (eg adding a file). Note it takes a moment or two for the file sensor to update.

  - platform: folder
    folder: '/config/www/alarm_clock_sounds'
    filter: '*'

    name: "MP3 Files"
      - None Available

  - id: load_mp3_select
    alias: "Load MP3 Select"
    description: "Load/Update MP3 select list."
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sensor.alarm_clock_sounds
      - service: input_select.set_options
          entity_id: input_select.mp3_files
          options: >
            {% set files = namespace(value=[]) %}
            {% for item in state_attr('sensor.alarm_clock_sounds','file_list') %}
            {% set files.value = files.value + [item] %}
            {% endfor %}
            {{ files.value }}



Hi jazz
Thanks a lot. This works great.
But is there a ways to show only the file name instead of /config/www/…

1 Like


{% set files.value = files.value + [item] %}


{% set files.value = files.value + [item|replace('/config/www/alarm_clock_sounds/','')] %}

Change alarm_clock_sounds to your folder name of course.

Thanks. Actually I also find the solution. But what I did was change your last line.
But thanks so much for your solution. Really apperaciated.

 {{ files.value  |replace('/config/www/music/', '')}}
1 Like

Cool beans! That is probably even slightly more efficient since replace is only called once instead of every iteration of the for loop.

Yes you are absolutely right.

Good work! :+1:

BTW, the file_list attribute already contains a list so there’s no need to use a for-loop to create a list.

All that’s need is just to slice off the file-path information for each item in the list and that’s easily done like this:

      - service: input_select.set_options
          entity_id: input_select.mp3_files
          options: >
            {{ state_attr('sensor.alarm_clock_sounds', 'file_list') | map('replace', '/config/www/alarm_clock_sounds/', '') | list }}

Here’s an example using some files on my test machine located in /config/data.


Hello guys,

Sorry if this off topic…

I followed the guide above to fill the dropdown with .m3u playlists I have in my /media folder. Now if I try to play it via nest mini by using media_play service. The Playlist is not played.

If I play the Playlist using vlc it plays just fine.

What am I doing wrong here?

Kindly guide