Integrations

Media source

Spook adds actions that list the files in a folder of your media and delete them from an automation, to clean up the camera snapshots and recordings that pile up.

The media source integration is what the media browser of Home Assistant shows. My media in there is the local media: the media folder of Home Assistant, and any other folder you added to it.

Automations put a lot in there. A snapshot of the front door every time somebody rings, a recording every time the camera sees movement. It only ever grows. The media browser deletes a file at a time, by hand, and an automation cannot see what is in a folder, let alone delete it.

Spook adds two actions for that: list the files in a folder, and delete them. Together, they keep a folder of snapshots to the last month, or whatever you like.

Actions

Spook adds two actions to the media source integration. You find them wherever you add an action, marked with a 👻.

Both work on My media only, by the media source ID of a file or folder, like media-source://media_source/local/snapshots. local is the media folder of Home Assistant itself. A folder you added under media_dirs in your configuration goes by its own name there.

List media

Lists the media files in a folder of My media, the oldest first: their name and path, their size, when they last changed, and what kind of media they are.

The files are the ones the media browser shows: images, videos and audio. Other files, folders, and anything hidden (its name starts with a dot) are left out. With Recursive on, the files in the folders inside it are listed too, all the way down.

With Older than, only the files that last changed longer ago than that are listed. Ready to hand to Delete media.

Leave out the folder for the top of My media. With more than one media folder, that is the top of each of them. A media folder that is not there yet has no files. A folder you name that is not there is an error.

Everybody can list media, as everybody can browse it in the media browser.

How to use it

  1. Open the automation or script that needs the list, and select Add action.
  2. Search for list media, and select Media source: List media 👻.
  3. Under Folder, enter the media source ID of the folder, like media-source://media_source/local/snapshots.
  4. Under Response variable, give the list a name, like result. The steps after this one can use it.
  5. Select Save.

Delete media

Deletes one or more files from My media. The same as the delete in the media browser, for as many files as you like. Each goes through the media source of Home Assistant, with its own checks.

A file that is not there is skipped, not an error. Ask for the response to see which files were deleted, and which were not found.

Every file is checked before the first one goes. One folder, one file of another media source like a camera, or one path that leads out of its media folder, and the action deletes nothing at all. Folders are never deleted: give the files in them, List media gives them.

When a file cannot be deleted, for example because Home Assistant is not allowed to, the action still deletes everything else. Then it fails with an error that names each file it could not delete, and why.

Deleting media is for administrators only, as the delete in the media browser is.

How to use it

  1. Open the automation or script that should delete the files, and select Add action.
  2. Search for delete media, and select Media source: Delete media 👻.
  3. Under File, enter the media source IDs of the files to delete. Media source: List media 👻 gives them.
  4. Select Save.

Missing something here?

An action, an entity or a repair this integration should have? Say so. Stuck, or found a bug? There is help.