Check if a file exist in wait_template

Hello,
I have a question about the wait_template in automations/scripts.

In scripts and automations you can use delay and wait_template. I have an automation that triggers a script that takes a picture from a ip-camera and then sends it to my mail and pushbullet. Right now i use an 10 seconds delay between the photo is taken and that the photo is sent so it have time to save.

I wonder if you instead of delay could use the wait-template that checks if the file exist yet?

Thanks for your help!

Have you already had a look at the folder watcher component?

Hello!
Thanks, and yes I have looked into that.

As I understands it that component just looks for if thereā€™s a new file, not a specific file with a specific file name.

What I do is that I have an automation that triggers by an motion detector and sends the current date and time to a script, that date and time is then the file name for the pictures. The script is taking 5 pictures in a row and gives the new photos the name sent from the automation and a number from 1 to 5. When the fifth picture is taken I want to add all five to one mail and send it. With Folder Watch I can make it send each picture in separate mails, but I want all of them in the same mail.

In my script I take the pictures, then a delay for 10 seconds and then sends them in an e-mail. Instead of the delay I would want a wait_template that checks if the last file exist and when i does it sends the email. Some times I notice that it takes a bit longer to save the pictures and all of them doesnā€™t arrive and sometimes it goes faster. with wait_template it sends it as fast he last picture is saved.

How do you propose looking for a ā€˜specific file nameā€™ when that file name is unknown in advance?

The fileā€™s name is a date and time. The time is whenever the camera write its image. That can happen at any time and is unknown in advance. So how is something supposed to look for a file whose actual name it doesnā€™t know?

The folder_watcher can accomplish this by looking for ā€˜file createdā€™ events. In other words, it detects when a file is created and reports the name of the file that was created.

After the picture is taken you know the file name since it is decided in the automation that triggers the script that takes the photo. I send what file name to use from the automation to the script. I want the wait_template to look for that file name in the folder. When its saved the script proceeds.

How do I use the folder watcher inside a script where the folder watchers not the trigger. and what I understand if you use the folder watcher as a trigger it going to send each picture in its own mail. How do i do so the folder watcher waits until all pictures is taken and adds all of them in the same mail?

Thanks for the clarification. So the fileā€™s name is known in advance because your automation assigns it.

To my knowledge, thereā€™s nothing available in the Jinja2 templating language to test for the presence of a file. Therefore you need to rely on some other ā€˜thingā€™ to tell you when the desired file is present.

The problem is that this other ā€˜thingā€™ such as folder_watcher doesnā€™t accept ā€˜on-the-flyā€™ requests. In other words, you canā€™t pass the name of a file you want it to detect. You preconfigure it to monitor for specific file-management events (created, deleted, modified, etc) and it reports the names of files that match the specified event.

:thinking:


EDIT
The documentation for folder_watcher includes an example of monitoring file-creation events and then sends a notification containing the name of the freshly created file.

However, this example will do want you donā€™t want it to do, namely trigger for each new file created as opposed to when the last of a batch of files is created.

#Send notification for new image (including the image itself)
automation:
  alias: New file alert
  trigger:
    platform: event
    event_type: folder_watcher
    event_data:
      event_type: created
  action:
    service: notify.notify
    data_template:
      title: New image captured!
      message: "Created {{ trigger.event.data.file }} in {{ trigger.event.data.folder }}"
      data:
        file: "{{ trigger.event.data.path }}"

You could create an extra automation that triggers on the file created event and sets the value of an input text entity to the file name of the created file. This way you will always have the latest file to compare to the file you want.

Another solution would be to create 2 automations. One to create the images and another to check if the fifth file has been created (using fnmatch) with folder watch.

If the second automation triggers you should be able to create an email with multiple attachment by looping through the file names with suffix 1-5.

@metbril

Oh! Yeay!

Instead of waiting for any file to turn up i could wait for the file

folder_watcher:
  - folder: /config
    patterns:
      - '*_5.jpg'

to turn up and then attach all the pictures. Shall look into that!

Thanks!

BTW, if you have Home Assistant installed as docker container, getting folder_watcher to work is problematic (to say the least). There are several threads on the forum where people have reported no success in getting it to work from a docker container.

Okay, to bad.

It would be nice if there were a way to check for certain files.

for example

is_file('the path and file name', 'true')

for example. Too bad it doesnā€™t. You have to work with what you got i guess. I also want to remove the file after itā€™s sent and I found shell command. The problem is that if a new photo is taken close to the first one the remove command from the first trigger removes both files before the second trigger had time ti send the second file. I guess its the same there, you canā€™t remove just a specific file like that when the file name is decided in the automation.

Thanks for your help!

@123
Thank you for the information!

Luckily I donā€™t have it installed as docker container! =)

Theoretically, the way folder_watcher works is more efficient. Instead of waiting around (using wait_template) for the image files to appear (the script is blocked from being executed again while it is waiting), folder_watcher is event-based. Therefore nothing is twiddling its thumbs in blocking-mode; youā€™re notified when the event occurs.

I have two instances of Home Assistant and one of them is installed as a docker container. I canā€™t folder_watcher to work with that one. Iā€™ve done everything required (whitelist the external directory) but it never reports any events. :man_shrugging:

Do you have any examples of those posts with issues? I know about the limitation to whitelist the /backup folder, but the config and shared folder should work, donā€™t they?

As a ā€œthank youā€ I would appreciate you posting the automations once you get it working. :hugs:

This thread summarizes my own experience with the issue. I could get file_sensor to work but not folder_watcher (using version 0.94.2 as a docker container).

Configuration is trivial:

homeassistant:
  whitelist_external_dirs:
    - /config

folder_watcher:
  - folder: /config
    patterns:
      - 'automations.yaml'

After restart, it created a new sensor: sensor.automations_yaml.

Nevertheless, this automation never triggered:

- alias: 'detect automation changes'
  trigger:
    platform: event
    event_type: folder_watcher
    event_data:
      event_type: modified
  action:
    service: system_log.write
    data:
      message: "Automations file was modified."
      level: warning