Is there a way for automation to enumerate files in a directory and pick first one?

Case: I want my VTO to grab a picture of caller and send it via telegram to home group members.

Problem: My VTO is by default capturing photos on call and saving them on FTP server (and I want to keep that functionality), therefore when I capture image with automation which grabs http://192.168.1.240/cgi-bin/snapshot.cgi it is getting delayed and most of the times person is already entering gate.

Possible solution: Mount that FTP directory to HA as media share (done, works) grab picture from there and send it via telegram.

Example image path on FTP is:

SnapShot/2024-01-17/12/12.53.51_1.jpg

So I created path like this:

file: >-
/media/SnapShot/{{ now ().year }}-{{ now
().month }}-{{ now ().day }}/{{ now ().hour }}/{{ now ().hour
}}.{{ now ().minute }}.{{ now ().second }}_1.jpg

and the problem is that is works only from time to time, when seconds really match.

As You can notice calls are divided in directories per paticular hour (dir 12 between dir with date and actual file) and as I usually dont really get two calls in same hour all I need is to enumerate files in that folder and pass first position found to automation. Is that doable?

Sorry I’m no expert and this is a response off the back of a quick google :face_with_open_eyes_and_hand_over_mouth:

Maybe Folder Watcher - Home Assistant is what you’re looking for, filtering for file events that have been ‘created’

Some community examples

1 Like

At first moment I thought this can be it, but I am afraid it works only within one directory, while I need to jump deeper → to date, and even deeper → to actual hour. But I will test that anyway, just to be sure.

EDIT:
Oh my god, it works!
And I dont even need my VTO integration for that, it is just monitoring that dir and pushing everything that appears through telegram. Thank You very much!

EDIT2:
LOL, doesn’t work :frowning:
I mean it is working if I copy new file via explorer like I did for test. FTP service from VTO somehow puts files there without being noticed. I think I will try to mirror that dir with some NAS backup synchronisation and monitor the destination dir instead of FTP directly.

1 Like

Folder replication didnt solve it. Somehow the automation only discovers new files when I create them via mounted path on HA side.

I put new file over FTP to directory which is being monitored → automation dont catch it
I go directly to NAS and put new file → automation dont catch it
I go to HAhost/media// and put file there → automation catches it

How are you managing to do this ? Is this a samba mount ?

1 Like

System → Storage → Add network storage. SMB share from NAS.

This must be the problem

2 Likes

Switched the way around as well - unmounted NAS from HA and mounted HA share on NAS. Created replication between NAS FTP and HA share mounted on NAS - didn’t work as well but I decided to play with event type.

I think it started to work once I changed
event_type: created
to
event_type: modified

Further tests tomorrow.

Tested and it works and intended now. Dont have much time for further tests like checking if changing event_type from created to modified will help on original location, but I don’t care, it works now and that is all I needed.

Thank You for brainstorming this case _dev_null.