Folder watcher (Watchdog) component

Hi all
this component is for monitoring the contents of a folder. This differs from the folder sensor as it fires an event whenever a file is added, modified or deleted in a folder.

This is an official component:

4 Likes

Rob thank you for creating this. Want to use it to trigger an automation when my letsencrypt certs are renewed.
Will it detect in this folder /etc/… and what would be the event; “change”?
Thank you again

Hi Juan, the events are:

  • created
  • deleted
  • modified
  • moved

Rob, many thanks for your prompt reply.
Yes I saw your project a month ago but was waiting for it to be released in HA and I updated yesterday.

On the etc query, I suspect it should work? The folder has read permissions, so should be ok.

How would you follow different locations I.e.
Config/
Etc/

Thank you

Monitoring is recursive by default, therefore just monitor the containing folder

Apologies Rob, didnt understand.
So in this example it monitors this folder
https://hastebin.com/qimonukavu.makefile

For example:

folder_watcher:
  - folder: /Users/robincole/.homeassistant/images
    patterns:
      - '*.jpg'
  - folder: /Users/robincole/.homeassistant/www
1 Like

Thank you very much Rob and thank you for creating this.

1 Like

Thanks Juan. It’s always a join effort, so my thanks to the reviewers :slight_smile:

Rob, One more question, on the trigger side how would you discriminate the folder triggering if you have a few folders. this is the example in the docs

  • event_data: {“event_type”:“created”}
    event_type: folder_watcher
    platform: event

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