Folder watcher (Watchdog) component

You should add to the automation a condition on trigger.event.data.folder
I’ve not tried this myself yet

Thank you again Rob,
Please share when/if you try.

OK I’ve cooked up this fairly hacky way to display the folder_watcher event data on the HA front end. I can break out event data and display it using an input_text component. In configuration.yaml:

input_text:
  last_added_file:
    name: Last added file
    initial: None

I then use an automation to write the folder_watcher event.data.path to the input_text. In automations.yaml:

- action:
    data_template:
      value: " {{ trigger.event.data.path }} "
    entity_id: input_text.last_added_file
    service: input_text.set_value
  alias: Update input_text
  condition: []
  id: '1520092824600'
  trigger:
  - event_data: {"event_type":"created"}
    event_type: folder_watcher
    platform: event

If anyone knows a more direct way to achieve this I would love to hear it.
Cheers

I have a problem using this component:

2018-04-21 07:52:01 ERROR (SyncWorker_11) [homeassistant.components.folder_watcher] folder /config is not valid or allowed
2018-04-21 07:52:01 ERROR (MainThread) [homeassistant.setup] Setup failed for folder_watcher: Component failed to initialize.

Ok I just noticed that the folder_watcher docs fail to mention that you must configure your watchlist, see the folder sensor docs. I’ve created a PR on the docs

I’ll share a use case I just completed using Folder Watcher (Thanks for this!!)

I have a security camera that has motion zones configured within its view
and if motion is detected in a zone it takes a picture and sends a jpeg file (each time with a different name) to an NFS mounted directory that is accessible by HA’s Folder Watcher.
When Folder Watcher sees a new *.jpg file showing up in that directory I use an automation trigger based on folder watcher event to fire an action which allows the image to show up in the front end. The action is now possible due to a recently added service in the camera “Local File” platform component that allows the file path to be specified in the service call of the camera Local File with the path name being supplied from the folder watcher!!

1 Like

@wmaker that was exactly my use case too and the motivation for these contributions. If there’s more services required to complete your application let me know. In the meantime I’ve been working on image classification - Facebox coming in 0.70 :slight_smile:

This sounds exactly like the thing I’m looking for. Mind sharing the actual code?

The code robmarkcole sent seems too complicated for my setup as it actually creates the picture itself from the camera, I have my cameras in another system, just saving snapshots to my NAS but with different file names each time.

Kind regards,

Its been a while so I had to sit down and do a write up even for myself.
I think I got it all here (hopefully) :slight_smile:

Application Description:
One has a security camera that has motion zones configured within its view
and if motion is detected in a zone(s) the camera takes a picture and sends a jpeg file (each time with a different name) 
to an NFS mounted directory that is accessible by HA’s Folder Watcher.

When Folder Watcher sees a new *.jpg file showing up in that directory it fires an event and this
can be used to trigger an automation.  The Event also contains the file name and path. 
An action is used to to present this image in the front end via the camera component. 
  In particular, the camera “Local File” platform component 
  which now allows the file name and path to be specified in a service call. 
  In this application, the path name is being supplied from the folder watcher event!!

Note: White list is not needed for files in www directory.

A binary Motion Sensor can also be derived out of this using a template sensor that
is driven by the automation to activate it and separately a timer to inactivate it.

Another Sensor (Folder Sensor) is used to monitor the directory's size.

Configuration -
#Configure Folder Watcher to look for any jpeg file changes in a specified directory
folder_watcher:
  - folder: /home/homeassistant/.homeassistant/www/<Directory-of-Interest>
    patterns:
      - '*.jpg'

binar_sensor:
# Set up a Motion Sensor for these Folder Watcher based Security Camera images
  - platform: template
    sensors:
      front_yard_motion:
        friendly_name: "Front Yard Motion"
        device_class: 'motion'
        value_template: >-
          {% if is_state('input_text.last_added_file', 'None') %}
            false
          {% else %}
            true
          {% endif %}

sensor:
#Setup a Folder sensor to monitor the directory size (and number of files)
#Note: the name of the sensor comes from the name of the directory being monitored.
  - platform: folder
    folder: /home/homeassistant/.homeassistant/www/<Directory-of-Interest>

#Setup a Local File Camera to show the pictures on the front end.
#One has to initialize it to some dummy jpeg filename.
#  The automation will change this as new camera images appear.
camera:
  - platform: local_file
    name: Local Picture
    file_path: /home/homeassistant/.homeassistant/www/<some-file>.jpg

#A variable is used to store the latest jpeg filename that folder watcher detected
input_text:
  last_added_file:
    name: Last added file
    initial: None

#Setup a timer to make the motion detector go back to inactive
timer:
  front_yard_unmotion:
    name: Front Yard UnMotion
    icon: mdi:history
    duration: '00:01:00'

Automation -
#Folder Watcher triggers when files are created. The path and name of the file are passed in the event
#Action to set the camera with the file path and name of the newly created image.
#Action to activate the motion sensor and start a timer to inactivate it later.
  - alias: File_watcher_automation
    trigger:
      platform: event
      event_data: {"event_type":"created"}
      event_type: folder_watcher
    action:
      - service: input_text.set_value
        entity_id: input_text.last_added_file
        data_template:
          value: " {{ trigger.event.data.path }} "
      - service: camera.local_file_update_file_path
        entity_id: camera.local_picture
        data_template:
          file_path: " {{ trigger.event.data.path }} "
      - service: timer.start  #Start timer to expire binary sensor
        entity_id: timer.front_yard_unmotion

 #Expired timer that changes input_text.last_added_file to "None"
  #which a binary template sensor uses to change state.
  #This in effect sets the binary sensor to off.
  - alias: Front_Unmotion_timed_out
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.front_yard_unmotion
    action:
      - service: input_text.set_value
        data:
          entity_id: input_text.last_added_file
          value: "None"
1 Like

This works really good, however it only seems to work for 1 of my cameras, and a random one too. Every time I get a new picture in the folder a random camera takes it and uses it. How can I control it? Preferably with different folders. I know that I can make several folders and add several folder_watchers BUT how can I differentiate between them? When I try to rename the folder_watcher it gives me errors

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!