Getting the last added file with folder_watcher

I’d rather use the default components to keep everything as simple as possible and also for future updates of Hass.io. But i think i fond something useful, https://github.com/robmarkcole/robins-hassio-config

I would then use the built in component. I use it for live feeds, snap shots and it has motion detection.

If you know your urls (i found mine on the internet), you can point the live camera like this.

camera:
  - platform: ffmpeg
    name: backyard
    input: rtsp://admin:[email protected]:554/cam/realmonitor?channel=1&subtype=0

But that would just display the current image and not the last one when motion detected if i’m correct.

My idea is to use the last motion detected image in a picture glance with the ability to view the live feed when you click on it. Besides displaying it inside a picture glance i would also like to use it to send an alert.

I don’t know how to use the built in motion detection with hass.io.

There is a sensor for ffmpeg that detects motion and then you can snap a shot with camera components and a automation.

The ffmpeg camera component setup mentioned by @RobDYI would allow you to use camera.snapshot service to take snapshot, and store to a user-defined folder, of the camera stream. I use an automation based on motion detection sensor to take snapshot and send with notification using this approach.

I was setting up my door bell picture taking. Do you find the snapshot service responsive and good quality? I was planning to use a shell command with this working snapshot

ffmpeg -loglevel debug -rtsp_transport tcp -i “rtsp://admin:[email protected]:554/cam/realmonitor?channel=2&subtype=0” -vframes 1 /Users/xxx/ram/www/door.jpg

Both of you are correct… if you want to use the basic motion detection. However that gives me a lot of false positives so i decided to use the IVS / Smart detection the camera has by itself.

Dahua cams also have things like tripwire / intrusion detection and that gives me way much less false poositives (better said, none) so i would realy like to use the camera motion detection. It just works better than the “normal” motion detection that’s part of the onfiv standard.

I have it running now, images are uploaded, only thing is i need to get the last image uploaded.

I have the following in my automations config now:

- alias: 'Motion detected'
  trigger:
    platform: event
    event_type: folder_watcher
    event_data:
      event_type: created
  action:
    service: notify.ios_iphone_van_reind
    data_template:
      message: >
        {{ trigger.event.data.file }}

Thing is… it’s not working when i have {{ trigger.event.data.file }} in my message (i end up with a Error rendering data template: UndefinedError: 'trigger' is undefined in the log), when i replace it by anything else like “hi” it works fine but i can’t get the last file nor filename.

Does the example work?

    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 }}"

I would try this part 

        data_template:
          title: New image captured!
          message: "Created {{ trigger.event.data.file }} in {{ trigger.event.data.folder }}"
          data:
            file: "{{ trigger.event.data.path }}"

Nope… i end up with the same trigger error mentioned before.

It seems that the message part is the issue because when i replace it with just “hi”, walk through the tripwire of the cam i’m getting the message so the following is working,

  • camera uploads image
  • folder_watcher detects the image
  • automation runs fine with message: "hi" and i’m notified but fails when using the data as described in the example.

I found the quality is camera-dependent, the HA snapshot service responsiveness, however, is worst with rtsp.
I same setup for both rtsp and mjpeg stream from different camera, HA snapshot on mjpeg stream has been more reliable and responsive, rtsp stream sometimes just don’t work.

If you plan to use the ffmpeg command, be aware that ffmpeg can take couple of second for snapshot (depends on your system), so you may want to adjust your HA script/automation for the delay.

Ok, thanks

Do you get any off the three? {{ trigger.event.data.folder }} {{ trigger.event.data.path }}, {{ trigger.event.data.file }} individually. I understand that it will fire when any file is created so you need a condition in the automation to narrow it down into a certain folder.

I have not try the folder_watch approach for snapshot, but notify for ios requires the file url to be something like http://ha_ip:8123/local/your_snapshot/snapshot.jpg, you may want to double check what {{trigger.event.data.folder}} resolves as.
ios notify example

Nope, i tried the following

Exact copy from component page example

- 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 }}"

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

Then i tried:

- 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: "{{ trigger.event.data.path }}"

Same error

- 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: "{{ trigger.event.data.file }}"

Same error

- 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: "{{ trigger.event.data.folder }}"

Same error

I really don’t have a clue where to “debug” this. The images are uploaded, the folder_watcher detects the changes (because when the {{…}} stuff is removed i’m getting the notification) but getting the actual file name and location doesn’t seem to work (it begins to become frustrating now :wink: ).

Images are uploaded… could it be the filename? (don’t think so else the folder would have worked).

I copied and tested your last version (only the message), did not get any error and {{ trigger.event.data.folder }} resolved correctly to the full path folder name on my iphone notification message. So, it’s not syntax.
A wild shot, try adding a “- delay: 3” before the notify service, don’t forget the dash before service for multiple actions.

Don’t think a delay will solve the issue because i’m firing the event from the hassio interface (automations card) so the image is already there.

Will do further testing today to see if I can find a solution.

———edit ———

Ok I’m available for the dumbest user of the month award. Seems that it’s just working fine when a image is uploaded but it doesn’t work when the automation is fired from home assistant automations card.

When I left the house this morning I received multiple notifications with the image path in it.

Next up, display that data inside a Lovelace picture glance.

hi,
in the last days, i set up a webcam with the plugin motion1, which creates a snapshot in the folder /share/motion. for this folder i set up the folder_watch component succesfully.
Now i’d like to get an ios notification with the last snapshot created. the push notification works fine, but i don’t see the snapshot, only the text is pushed to my iphone.

@R2D2: Which of your configs is working for you? Do you see an image? I tested all of them, but none works for me :frowning:

Thanks in advance!

hi, i just tested the code. but did not receive the picture. only notification with folder path

this is my automation

- alias: 'Motion detected'
  trigger:
    platform: event
    event_type: folder_watcher
    event_data:
      event_type: created
  action:
    service: notify.anok_ike_bot
    data_template:
      title: Motion Detected captured!
      message: "Created {{ trigger.event.data.file }} in {{ trigger.event.data.folder }}"
      data:
        file: "{{ trigger.event.data.folder }}"

and this is what i got in my telegram without the picture

Motion Detected captured!
Created 2020-04-10-01-51-37.jpg in /share/Picture

can someone help me on this

Use trigger.event.data.path instead of trigger.event.data.folder

hi all same mather.

but good issus on android. and HASSIO os
sorry for my english.
i 'm french
use : : Folder watcher

# voie si un jpg à été ajouté
folder_watcher:
  - folder: /share
    patterns:
      - '*.jpg'
  - folder: /config/www
    patterns:
      - '*.jpg'

and configuration.yaml: https://www.home-assistant.io/docs/configuration/basic/#allowlist_external_urls

homeassistant:
# authorise d'utiliser les fichiers
  allowlist_external_dirs:
    - /config
    - /share

and template dir/ (for android notify)

title: New image captured!
message: Alarme caméra
data:
  image: >-
    https://XXXX.freeboxos.fr/local/mages/192.168.0.X4_XXXxe8efd7/{{
    now().strftime("%Y-%m-%d") }}/01/pic//{{trigger.event.data.file}}

local= config/www.
see you stefan

3 Likes