Folder/subfolder names to input select

I am trying to dynamically update an input_select list with a folder/subfolder name list.

ex:

D:\Dropbox\MUSIC\ALL MUSIC\FOLDER A
D:\Dropbox\MUSIC\ALL MUSIC\FOLDER B
D:\Dropbox\MUSIC\ALL MUSIC\FOLDER C
D:\Dropbox\MUSIC\ALL MUSIC\FOLDER D

I did find some information on how to populate the list with filenames under a specific folder :

but not the folder/subfolder names.

If it’s not possible to accomplish this directly, I have a dynamic text file containing all the folders and subfolders names in the same format :

D:\Dropbox\MUSIC\ALL MUSIC\FOLDER A
D:\Dropbox\MUSIC\ALL MUSIC\FOLDER B
D:\Dropbox\MUSIC\ALL MUSIC\FOLDER C
D:\Dropbox\MUSIC\ALL MUSIC\FOLDER D
...

and was hoping to use this method to read the data from a sensor

so enabled a file sensor

- platform: file
  name: 'Winamp folder list'
  file_path: /config/www/winamp/folderlist.txt  

but it just reads the last line.

looking for guidance or expert tips to close the loop on this one.
thanks for your help!

As per the docs, file sensor only uses the last line, so that’s expected behavior.

If you use */ instead of * in the filter for folder platform, you can list only folders. However, you can’t get subfolders (with **/), because glob does not have the recursive flag in the component’s code. See screenshots below.

You also can create the select in the UI if you prefer, no problem.
See this, if you want to change the sorting order when the automation updates the select.

Restart HA after config.

sensor:
  - platform: folder
    folder: "/config/www/winamp/"
    filter: "*/"

input_select:
  winamp_folders:
    name: "Winamp Folders"
    options:
      - None Available

automation:
  - id: load_winamp_folders_select
    alias: "Load Winamp Folders Select"
    description: "Load/Update folders in select."
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sensor.winamp
    action:
      - service: input_select.set_options
        target:
          entity_id: input_select.winamp_folders
        data:
          options: >
            {%- set folders = namespace(value=[]) %}
            {%- for folder in state_attr("sensor.winamp", "file_list") | sort %}
              {%- set folders.value = folders.value + [folder] %}
            {%- endfor %}
            {{ folders.value }}

For a folder structure like this
folders

you will get these results

1 Like

Thanks, this works :+1:

questions:

  • I guess the answer is no, but any simple workarounds to get subfolders?

  • otherwise since I am working with a limited number of subfolders, a potential workaround would be to “combine” multiple folder/sensors to the list:

I created 3 folder sensors :

- platform: folder
  folder: "/media/MUSIC_GENRES/"
  filter: "*/"
- platform: folder
  folder: "/media/MUSIC_OFF_RECORD/"
  filter: "*/"
- platform: folder
  folder: "/media/MUSIC_PLAYLISTS/"
  filter: "*/"

but need some help with the syntax to go through all 3 sensors:

- id: winamp_folders_input_select
  alias: "WINAMP folders input select"
  description: "Load/Update folders in select."
  trigger:
    - platform: homeassistant
      event: start
    - platform: state
      entity_id: sensor.music_genres
    - platform: state
      entity_id: sensor.music_off_record
    - platform: state
      entity_id: sensor.music_playlists
  action:
    - service: input_select.set_options
      target:
        entity_id: input_select.winamp_folders
      data:
        options: >
          {%- set folders = namespace(value=[]) %}
          {%- for folder in (state_attr("sensor.music_genres", "file_list") | map('replace', '/media/', '') | sort) and (state_attr("sensor.music_off_record", "file_list") | map('replace', '/media/', '') | sort) and (state_attr("sensor.music_playlists", "file_list") | map('replace', '/media/', '') | sort)  %}
            {%- set folders.value = folders.value + [folder] %}
          {%- endfor %}
          {{ folders.value }}
  • Also, can I append an extra option to the beginning of the list, something like “select folder:” to ensure I have the option to select any option/folder (as the currently selected option in input_select can’t be “reselected”)

Thanks again for your help!

No, you’d need a custom component, or the folder component to be updated to support recursive mode.

You can merge lists together with +, that should solve your two issues, if I understood correctly what you want:

{%- set folders = namespace(value=['select folder:']) %}
{%- set all_lists = state_attr("sensor.music_genres", "file_list") + state_attr("sensor.music_off_record", "file_list") + state_attr("sensor.music_playlists", "file_list") %}
{%- for folder in (all_lists | map('replace', '/media/', '') | sort)  %}
  {%- set folders.value = folders.value + [folder] %}
{%- endfor %}
{{ folders.value }}
1 Like

Bingo.
Thank you, much appreciated :wink: :+1:

1 Like

I created a pull request to support recursive mode. Hopefully it’ll be added in the near future and you will be able to cleanup your code then :wink:

2 Likes

Great. would definitely be useful.
Following pull request. Thanks again!