Automation to delete files from folder older than x days

Hello,
I would like HA to record short video files when motion in my camera is detected. They will be stored somewhere in www folder let’s say www/camera_motion. I would like to automatically delete content of that folder when it is larger thant 500 MB. I know I might use folder sensor to know folder’s size but I don’t know how to delete files from it but only those which are older than x days. I assume I will need shell command or something. Can anyone please help me? Cheers

1 Like

What’s your OS?

Sorry forgot to write about it. HassIO

So you’d either need to do this via a custom integration or appdaemon. There’s nothing built in that will get you date modified or date created file properties. Without those, you won’t know how old the file is. If you have linux as a OS you could run some cli commands to handle it for you. But I don’t believe that’s an option with HassOS.

So I will do it manually…not smart though…Thanks

It wouldn’t take much in an appdaemon app. I could help you create it.

1 Like

Sure :slight_smile: looking forward to…

I don’t use hass.io but use a switch configured this way:

switch:
  platform: command_line
  switches:
    clean_cctv_recordings:
      command_on: 'find /config/tensorflow/storagemotion/ -maxdepth 2 -type f -mtime +5 -exec rm {} -f \;'
      command_off: 'find /config/tensorflow/storagemotion/ -maxdepth 2 -type f -mtime +5 -exec rm {} -f \;'

It deletes all files older than 5 days.
Then all I need is to toggle the switch in an automation which runs daily. You can of course also just have a shell_command
You will also need to make sure you have whitelisted the directory:

homeassistant:
  whitelist_external_dirs:
    - /config/tensorflow/
1 Like

Thanks. I will give it a try

Yeah, I use supervised so it’s handled on my OS. Not sure if it would work in HomeAssistant hassio.

check this too: Automatically delete old camera snapshots

1 Like

Thanks. No info of working on hassio though…

I don’t know much about the differences in OSes. Mine is Home Assistant Supervised installed on Debian, so I apologize if this won’t work for you! Might work for someone else.

If your filenames contain dates like M-D-Y-H-M-S.mp4, you can use the folder sensor to trigger the automation, then run the shell_command service with something like this.

shell_command:
  delete_old_clips: 'ssh -i /config/keys/id_rsa -o StrictHostKeyChecking=no [email protected] find /usr/share/hassio/homeassistant/www/camera_motion -type f -name "{{ (as_timestamp(now()) - 864000) | timestamp_custom(''%M-%d-%y'') }}*.mp4" -delete'

Would delete files that are ten days old.

But, this might be exactly what @petro says won’t work.

I don’t believe that will work because it needs access to a ‘normal’ ssh. Worth a shot to try though.

what syntax will be for delete all files? Or files with specific extension?

that syntax deletes all files in that folder , regardless of extension
If you want to only delete some extensions, you need to add a filter to the find command, e.g. -name "*.jpg"

Maybe I wasn’t precise enough. I mean syntax which deletes all files even created a moment ago.

rm is the standard command to delete (remove) files and directories
http://manpages.ubuntu.com/manpages/trusty/man1/rm.1.html

@echojjj
Hi Jennifer,

On the same note, I understand that you are able to store the last motion-triggered clips in Home Assistant? did you pull it from Blue Iris?
Can you please share how did you achieve that?

Many thanks!! :slight_smile:

Didi

You might find a shell command will work even on HassOS. Depends on the command being used being supported in the container. You can run a console (with Portainer) and then run the command there. If it works in the container it will work in HA as well.