Folders Sensor - How to sort files?!

Hi!

I started to use the Folder Sensor, in order to see and “run” the files from lovelace, but… the problem is that the files are shown in a very strange way. There is any solution to sort the files by name, extension, filesize, anything?

Thank you!

Solved: I just “cloned” the sensor into a template, adding " | sort" at the end of the attribute

Can provide the details on what you mean by “cloned into a template adding sort” I trying to move from the customer files integration to use folder for a gallery-card and I need the files ordered by creation date. Newest file first. If you post the yaml for your template sensor that would be helpful.

media_files:
      value_template: >
        {{ states("sensor.media") }}
      attribute_templates:
        path: >
          {{ states.sensor.media.attributes.path }}
        filter: >
          {{ states.sensor.media.attributes.filter }}
        number_of_files: >
          {{ states.sensor.media.attributes.number_of_files }}
        bytes: >
          {{ states.sensor.media.attributes.bytes }}
        file_list: >
          {{ states.sensor.media.attributes.file_list | sort }}
        unit_of_measurement: >
          {{ states.sensor.media.attributes.unit_of_measurement }}
        icon: >
          {{ states.sensor.media.attributes.icon }}

I have created the sensor “manually” and added the “sort” detail, in order to sort the files… so practically, I have two media sensors, the default one and the one which will do what I want.

1 Like

Thanks, very helpful

if i add this code to my configuration.yaml i get
Integration error: media_files - Integration ‘media_files’ not found.

sensor.media is the name of the original folder watcher? like

sensor:
  - platform: folder
    folder: /config/www/media

This is what I have as sensor


- platform: folder
  folder: /config/www/media

and this is what I have as “custom” sensor

- platform: template
  sensors:
	media_files:
	      value_template: >
	        {{ states("sensor.media") }}
	      attribute_templates:
	        path: >
	          {{ state_attr("sensor.media","path") }}
	        filter: >
	          {{ state_attr("sensor.media","filter") }}
	        number_of_files: >
	          {{ state_attr("sensor.media","number_of_files") }}
	        bytes: >
	          {{ state_attr("sensor.media","bytes") }}
	        file_list: >
	          {{ state_attr("sensor.media","file_list") | sort }}
	        unit_of_measurement: >
	          {{ state_attr("sensor.media","unit_of_measurement") }}
	        icon: >
	          {{ state_attr("sensor.media","icon") }}

media_files should be a template sensor :smiley:

It is working very good.

2 Likes