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ā€ %}