Folder watcher (Watchdog) component

Yes I only set this up for one camera. To do multiple cameras would take
a little more thought. I think the biggest change would be to make
a directory per camera and an automation per camera that uses conditions to filter in only the directory that automation is interested in.

Hey there,

How did you access the NAS? I can’t seem to find a way to access my NAS from my PI through samba.

I’m having a issue wit the code provided on the sample page. I have a Dahua cam that uploads it’s snapshots to the /share folder (works fine i see the images), i have folder_watcher running on this directory to get me notified when a new image has been uploaded (i wan’t to use it also in a picture glance when i have this working but thats something else) BUT when using the example code as described here, https://www.home-assistant.io/components/folder_watcher/ i’m getting the following error:

Error rendering data template: UndefinedError: 'trigger' is undefined

The code i’m using in my automations.yaml is exactly the same as on the docs page,

- alias: 'Motion detected'
  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 }}"

When i replace the message with something like “hi” i’m receiving the notification so the component works but i can’t get the message like in the docs to work.

Any suggestions?

HI @R2D2 I just checked the following in 0.81.0 and works. Check your config for whitespace or indentation errors:

- action:
  - data_template:
      title: New image captured!
      message: "Created {{ trigger.event.data.file }} in {{ trigger.event.data.folder }}"
      data:
        file: "{{ trigger.event.data.path }}"
    service: notify.pushbullet
  alias: New file alert
  condition: []
  id: '1120092824611'
  trigger:
  - platform: event
    event_type: folder_watcher
    event_data:
      event_type: created

You are right! When I fire the automation from the hassio dashboard I get the error… but when a new image is uploaded it just works like it should.

So this wasn’t a component fault but just a dumb user :wink:

1 Like

Anu suggestions on how to filter out certain files that start with .xx.jpg? I have a filter now so folder watcher only fires when a .jpg file has been added but for some reason the ip camera also uploads a file that starts with . that can’t be read by hassio and fils up my log with messages like,

Could not read camera IP Camera oprit (still image) image from file: /share/dahua/3H03C08PAA00138/2018-10-28/001/jpg/19/48/._11.jpg

I found that because my filenames are “ch0XXXXXXXXXX.jpg”, using ‘ch0*.jpg’ does not work, but '*ch0*.jpg’ does work. Hope that’s useful information as it took me three days to figure out why it didn’t work, and it was due to a single “*”.

I have my CCTV DVR save motion images to an FTP server that runs on a local Windows machine, where the folder the FTP server uses is a SAMBA shared folder on my HASSIO (Raspberry Pi), so I could not pick the filenames it uses to save the images. The folder_watcher then just uses the local saved images to show up on my UI.

In configuration.yaml I have:

folder_watcher:
  - folder: /config/www/kguard_ftp/FTP/Motion
    patterns:
      - '*ch0*.jpg'

The automation is:

- id: '1520095552343'
  alias: Kguard FTP File Path Update
  trigger:
  - event_data:
      event_type: created
    event_type: folder_watcher
    platform: event
  condition: []
  action:
  - data_template:
      file_path: ' {{ trigger.event.data.path }} '
    entity_id: camera.kguard_test_active
    service: camera.local_file_update_file_path

Oh, sorry, the camera config is:

- platform: local_file
  name: Kguard Test Active
  file_path: /config/www/kguard_ftp/FTP/Motion/ch05_18_10_27_06_22_44_1540614165.jpg

You just have to make sure the file in the file_path is in the folder when HA starts up otherwise it doesn’t update to the new file path.

I have tried this example from the docs but it does not want to work for me. After adding the following to my automations.yaml I reload automations. When I go into my automations in the ui everything is loaded from automations.yaml except for the action. Action just lists the Json as empty braces {}. No issues verifying the config file. notify.alert is my smtp email notification that I use successfully in other automations.


- id: '1564975892253'
  alias: person_alert
  trigger:
    platform: event
    event_type: folder_watcher
    event_data:
      event_type: created
  action:
    service: notify.alert
    data_template:
      title: New image captured!
      message: Person Detected
      data:
        file: "{{ trigger.event.data.path }}"

Is it possible to make a record of the user who has modified it? I’m interested in using it to monitor user access to a NAS folder.

Thanks!

@ender7 that might just be a limitation of the automations UI - use the YAML instead

I’m attempting to build an automation around folder watcher that is similar to what is described above, but different in a couple key ways. The main thing I’m trying to do is change the contents of the notify message depending upon the path in which the file is found. I think I should do this by doing some basic string analysis on the contents of trigger.event.data.folder, but I’m having a hard time figuring out a specific implementation.

Specifically, what I’m trying to do is send a discord notification every time I add a new file to a particular directory on my media server. I have it all setup and working using the following automation:

- alias: New file alert - Movies
  trigger:
    platform: event
    event_type: folder_watcher
    event_data:
      event_type: created
  action:
    service: notify.discord
    data_template:
      title: New media uploaded.
      target: '888888888888888888'
      message: "__**Movie Added:**__\n```css\n{{ trigger.event.data.file }}\n``` in {{ trigger.event.data.folder }}"

I’m happy with it, but I’d like to be able to change the contents of the message based on whether the file is detected in my TV directory, or in my Movie directory. In practice, I’d like one message to send if the file is detected /Media/Movies/ and another message if the file is detected in /Media/TV/.

If anyone has any advice about how to approach this, I’d appreciate it.

see this example:

- id: image_folder_watcher 
  alias: Image folder watcher
  initial_state: true
  trigger:
  - platform: event
    event_type: folder_watcher
    event_data: {"event_type":"created"}
  action:
  - service_template: input_text.set_value
    data_template:
      entity_id: '{% if trigger.event.data.folder == "/config/www/image_processing/tmp" %} input_text.image_processing_tmp {% else %} input_text.dummy {% endif %}'
      value: "{{ trigger.event.data.file }}"

it updates and input_text when a picture file is added to the directory

That looks great! I think that’ll get me 90% of the way there, with one issue. In the example you posted, the there is some logic based on the folder path, but it is checking for exact folder path.

 entity_id: '{% if trigger.event.data.folder == "/config/www/image_processing/tmp" %} input_text.image_processing_tmp {% else %} input_text.dummy {% endif %}

In my situation, the path is going to vary a bit more, specifically with TV Shows where the path will look like this: /media/tv/show_name/season_xx/. The show name portion of the path is going to be constantly changing and not predictable. Is there a way to use wildcards in this comparison somehow? Like:

 {% if trigger.event.data.folder == "/media/tv/*" %}

Is it that simple? From what I’ve read in the docs, wild cards are not supported this way.

I havent tried it, but perhaps if you leave the path as /media/tv it will pick up anything created in the subfolders.
Best way is to try it…

So I just tested it. The if/else logic is working, but the check only works if the path is exactly what is specified. I setup the message to send using the following line in my automation:

message: '{% if trigger.event.data.folder == "/media/movie" %} __**Movie Added:**__```css{{ trigger.event.data.file }}``` in {{ trigger.event.data.folder }} {% else %} __**TV Episode Added:**__```css{{ trigger.event.data.file }}``` in {{ trigger.event.data.folder }} {% endif %}

It only send the “movie added” message if the file is created within the /media/movie directory, but if the file is added within a subdirectory, like /media/movie/movie_name/moviename.mkv, then the else condition is activated and the TV added message is sent incorrectly.

Seems I’m screwed unless theres a way to do wildcards. Or I suppose some regex stuff could make this happen as well, maybe. Have to come up with a different approach.

So why dont you look at some sensor that tells you when the torrent is done. I think I saw something in HACS

Variety of reasons, but primarily because torrents are not my only (or even primary) source of media. May have to check it out for that use case specifically though.

Okay, I think I’ve gotten closer. I’m now using the following automation:

#Send discord notification when new media is added.
- alias: New file alert - Movies
  trigger:
    platform: event
    event_type: folder_watcher
    event_data:
      event_type: created
  action:
    service: notify.discord
    data_template:
      title: New media uploaded.
      target: '888888888888888888'
      message: >
        {% if '/media/movie' in {{ trigger.event.data.folder }} %}
        __**Movie Added:**__```css{{ trigger.event.data.file }}``` in {{ trigger.event.data.folder }}
        {% elif '/media/tv' in {{ trigger.event.data.folder }} %}
        __**TV Episode Added:**__```css{{ trigger.event.data.file }}``` in {{ trigger.event.data.folder }}
        {% else %}
        __**Error:**__ this folder has not been configured for notifications yet
        {% endif %}

But its still not working. Home Assistant throws a notification every restart saying:

 The following components and platforms could not be set up :

* automation

Please check your config.

I’m run my automation through multiple YAML validators and there dont seem to be any issues there. The if/elif/else/endif logic seems solid based on the docs here: https://www.home-assistant.io/docs/configuration/templating/#building-templates. The only thing I’m not positive about is the check I preform in my if and elif statements here:

 {% if '/media/movie' in {{ trigger.event.data.folder }} %}

and here

 {% elif '/media/tv' in {{ trigger.event.data.folder }} %}

I believe these two lines should check the string {{ trigger.event.data.folder }} for any substring matching ‘/media/movie’ and ‘/media/tv’ respectively. I pulled this idea from the top answer to this question on stack overflow. They are not using HA, but it is all within the context of jinja2 templating, so I believe it should work.

Anyone notice anything I’m missing here? I think I’m very close, but not quite there.

My jinja2 skills are limited. so cant really comment on your approach, but why dont you use the one I proposed which i know it works:

{% if trigger.event.data.folder == “/media/movie” %}