[Custom Component] Fetch latest file

Hello everyone,

I wanted to share with you a custom component I’ve created that has simplified my home security setup. I use Reolink cameras and although Home Assistant receives notifications when a camera identifies a person or animal, there is a delay when attempting to create a snapshot or a recording. By the time the snapshot or recording is captured, the person or animal may have already left the frame, rendering the snapshot or recording less useful. Additionally, accessing event recordings directly from the Reolink integration within Home Assistant can be challenging (at least for my partner).

To address these issues, I developed this custom component that in combination with an automation/script fetches the latest camera screenshots and video events sent via FTP by the Reolink NVR to a shared folder accessible by Home Assistant. It then sends these files via Discord, providing a cloud copy that is easily accessible for my partner (At least she’s on Discord not just for D&D!).

The custom component supports various file types:

  • Image: jpg, jpeg, png, gif, bmp, webp, svg, heic, raw

  • Video: mp4, mkv, webm, flv, vob, ogv, avi, mov, wmv, mpg, mpeg, m4v

  • Audio: mp3, flac, wav, aac, ogg, wma, m4a, opus

  • Document: doc, docx, odt, pdf, rtf, tex, txt, wpd

  • Spreadsheet: xls, xlsx, ods, csv

  • Presentation: ppt, pptx, odp

  • Web: html, htm, xhtml, xml, css, js, php, json

  • Archive: zip, tar, gz, rar, 7z

  • Executable: exe, msi, bin, command, sh, bat, crx

  • Config: yaml, yml, ini, cfg, conf

  • Log: log, txt, log, syslog, eventlog, debug, audit

  • Unknown: none of the above.

(each category will be used as a attribute)

How to Use:

See my example

Once you’ve set up the custom component in your Home Assistant instance, you can call it using the service fetch_latest_file.fetch with the following parameters:

  • directory: Specifies the directory to search for files. (required)
  • filename: Sets the prefix of the file name to search for. (required)
  • extension: Specifies the file extension(s) to search for. (optional)

Here’s an example of how to call this service:

service: fetch_latest_file.fetch
data:
   directory: "/path/to/your/directory"
   filename: "cam1"
   extension: ["jpg", "mp4"]

In this example, the component will search for the latest .jpg and .mp4 files starting with “cam1” in the specified directory.

It will update the entity “fetch_latest_file.file” which will have this attributes (in this case):

video: /path/to/your/directory/cam1_20230613102757.mp4
image: /path/to/your/directory/cam1_20230613102757.jpg

If you have any questions, suggestions or would like to share how you plan to incorporate it into your own setup, feel free to ask/share!

More info: Github Repository

1 Like

This component seems like it’d be useful, but I can’t get it to find any files. In a normal HA install, is there some parent portion of “directory”? (e.g., is it really www/ or media-source/ ?)

Also I notice that there’s some case sensitiviting going on, like FileName (vs filename) for the key in the latest version. No biggie, just slightly different than the Github example.