IP Camera Snapshot Automation

Hi,

I am trying to get a snapshot from an IP Camera when motion is detected on a separate motion sensor.

I have setup an automation that sends me a pushbullet notification when motion is detected but i wish to attach an image to this.

I have tried the snapshot functionality but the file wont save into the tmp file I have created. I have white listed the folder:

homeassistant:
  whitelist_external_dirs:
    -/tmp`Preformatted text

The automation for the snapshot is:

- action:
  - data:
      entity_id: camera.name
      filename: /tmp/snapshot.jpg
    service: camera.snapshot
  alias: SnapShot
  condition: []
  id: '1517233794420'
  trigger:
  - entity_id: sensor.room_motion
    from: inactive
    platform: state
    to: active

I do have a URL for my IP camera that grabs a still image, is this a better way?

Can anyone please help me understand what I am doing wrong?

I do have the same automation.

I think youā€™re missing the " in the path, but not sure if is so important.

Hereā€™s my script:

  presence_kitchen_notifications_when_detected:
    alias: "Occupancy Detection - kitchen - Notifications"
    sequence:
    - condition: state
      entity_id: input_boolean.presence_kitchen_notify
      state: 'on'
    - service: camera.snapshot
      data:
        entity_id: camera.kitchen
        filename: "/config/www/cam_captures/kitchen_{{ now ().year }}_{{ now ().month }}_{{ now ().day }}_{{ now ().hour }}_{{ now ().minute }}.jpg"
    - delay: 
        seconds: 5
    - service: notify.telegramChat_dave
      data:
        title: "Presence Detected at kitchen"
        message: "1.- presence_kitchen_notifications_when_detected - Occupancy Detection - kitchen - Notifications"
        data:
          photo:
            - file: "/config/www/cam_captures/kitchen_{{ now ().year }}_{{ now ().month }}_{{ now ().day }}_{{ now ().hour }}_{{ now ().minute }}.jpg"
              capture: Snapshoot

Hope this helps!

5 Likes

Thanks!

This enabled the snapshot to save to the correct folder. I am now having issues sending this file on pushbullet.

My automation is:

- action:
  - delay: '2'
  - alias: ''
    data:
      filename: /config/tmp/snapshot.jpg
      message: motion detected
    service: notify.home_assistant_alert
  alias: Motion Camera
  condition: []
  id: '1517144410974'
  trigger:
  - entity_id: sensor.name_motion
    from: inactive
    platform: state
    to: active

I am getting this error:

2018-01-30 10:14:21 ERROR (MainThread) [homeassistant.core] Invalid service data for notify.home_assistant_alert: extra keys not allowed @ data[ā€˜filenameā€™]. Got ā€˜/config/tmp/snapshot.jpgā€™

I have tried with ā€œfileā€ rather than ā€œfilenameā€ with the same error.

Any ideas?

In my case I use telegram and based on the docs i needed yo use this way:

    data:
      photo:
        - file: "/config/www/cam_captures/kitchen_{{ now ().year }}_{{ now ().month }}_{{ now ().day }}_{{ now ().hour }}_{{ now ().minute }}.jpg"
          capture: Snapshoot

Please chechk if you have the data & photo tags for pushbullet ā€¦

only thing I can think of :slight_smile:

Thanks again got it working like this:

- action:
  - alias: ''
    data:
      message: motion detected
      title: Test
      data:
        file: /config/tmp/snapshot.jpg
    service: notify.home_assistant_alert
  alias: Motion Camera
  condition: []
  id: '1517144410974'
  trigger:
  - entity_id: sensor.name_motion
    from: inactive
    platform: state
    to: active

Great!! So what was the issue? (just out of curiosity :slight_smile: )

I needed to split out the message and file, so it went from:

data:
  filename: /config/tmp/snapshot.jpg
  message: motion detected
service: notify.home_assistant_alert

to

data:
  message: motion detected
  title: Test
  data:
    file: /config/tmp/snapshot.jpg
service: notify.home_assistant_alert
1 Like

Donā€™t supposed youā€™ve tried this with Pushbullet? Iā€™ve used your template (thank you!) and have the notify set up like this:

  - service: notify.pushbullet
    data:
     title: Doorbell Pushed
     message: Photo from front door attached
     data:
      file: /config/www/cam_captures/porch_{{ now ().year }}{{ now ().month }}{{ now ().day }}-{{ now ().hour }}{{ now ().minute }}.jpg

Trouble is, Pushbullet is not finding the file that gets created returning the following error:

FileNotFoundError: [Errno 2] No such file or directory: '/config/www/cam_captures/porch_{{ now ().year }}{{ now ().month }}{{ now ().day }}-{{ now ().hour }}{{ now ().minute }}.jpg'

The file is there and called porch_2018327-1246.jpg but the template code is obviously wrong as it is looking for the template code itself rather than the correct file name.

Any suggestions would be appreciated.

P.S If I create a static image like this, it works fine:

- service: notify.pushbullet
      data:
        title: test
        message: test
        data:
          file: /config/www/cam_captures/camera.jpg

hey,

so I had the same issue and ended up that the Snapshot took longer than expected so I included a little delay on sending the photo.

- service: camera.snapshot
  data:
    entity_id: camera.kitchen
    filename: "/config/www/cam_captures/kitchen_{{ now ().year }}_{{ now ().month }}_{{ now ().day }}_{{ now ().hour }}_{{ now ().minute }}.jpg"
- delay: 
    seconds: 5
- service: notify.telegramChat_dave
  data:
    title: "Presence Detected at kitchen"
    message: "1.- presence_kitchen_notifications_when_detected - Occupancy Detection - kitchen - Notifications"
    data:
      photo:
        - file: "/config/www/cam_captures/kitchen_{{ now ().year }}_{{ now ().month }}_{{ now ().day }}_{{ now ().hour }}_{{ now ().minute }}.jpg"
          capture: Snapshoot

Also, for me it didnā€™t work without adding the filename with the ".

let me know if it works.

3 Likes

Did you ever get this to work? iā€™m experiencing the same issue with Pushbullet.

Nope. I just grab a single image now as Synology NAS is recording all video anyway so building a snapshot database inside Hass not really necessary.

- id: ID8
  alias: 'Pool Gate Open'
  trigger:
      platform: state
      entity_id: binary_sensor.door_window_sensor_158d00019fac86
      from: 'off'
      to: 'on'
  action:
    - service: tts.google_say
      entity_id:
        - media_player.kitchen_home
        - media_player.gaming_room_home
      data_template:
        message: "The pool gate just opened"
    - service: camera.snapshot
      data:
          entity_id: camera.pool_deck
          filename: '/config/www/images/pool_gate.jpg'
    - service: notify.pushbullet
      data:
          data:
            file: '/config/www/images/pool_gate.jpg'
          title: "Pool Gate was opened"
          message: "Image of pool gate attached"
2 Likes

I struggled mightily with this, too. Turns out this is what works for my notify.smtp setup. Your mileage may vary:

- service: notify.gmail
  data:
    message: Gameroom door was opened
    title: We've been attacked
    data:
      html: <h4>Check the gameroom</h4>
      images:
      - /config/www/snapshots/new/gameroom.jpg

indent preformatted text by 4 spaces

I had already made a node-red flow using json that worked. I would never have figured out the yaml with a converter. JSON to YAML converter

Iā€™ve done a script + automation, with ffmpeg, which creates a small video from the CCTV feed when someone goes past the motion detector which is next to the CCTV.

The video is then sent to me via Telegram.

Do you want me to share the script?

2 Likes

Hi Metropolis, please share your script because Iā€™m sure there are more people (like me) who want this too.

Yes please, trying to do this exact thing and having trouble.

under ā€œshell_commandā€
ipcam2_mp4: bash -x /config/shell_scripts/ipcam2_mp4.sh >> /config/temp/ipcam2.log 2>&1

under ā€œautomationā€

- alias: IPCam2 Snapshot
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_1
    to: 'on'
  action:
  - service: shell_command.ipcam2_mp4
  - service: notify.ha_telegram
    data:
      message: ''
      data:
        video:
          - file: /config/temp/output.mp4

and the script itself : shell_scripts/ipcam2_mp4.sh

#!/bin/bash
date

folder=/config/temp
id=$(date +"%y-%m-%d_%H-%M-%S")
rtsp_url=rtsp://admin:[email protected]:554/12
frames=100
fps=5

mkdir $folder
ffmpeg -y -i $rtsp_url -r $fps -frames:v $frames -vcodec copy $folder/$id.mp4
ls -lh $folder/$id.mp4 $folder/output.mp4
cp $folder/$id.mp4 $folder/output.mp4
ls  -lh $folder/$id.mp4 $folder/output.mp4
find $folder -type f -name '*.mp4' -mtime +30 -exec rm {} \;

Please let me know if something isnā€™t clear :wink:

4 Likes

i got my automation working using this:

- id: '1532581029802'
  alias: Cam Picture Notification
  trigger:
    - platform: state
      entity_id: input_boolean.doorbell_boolean
      to: 'on'
#    - platform: state
#      entity_id: binary_sensor.front_door_camera_motion
#      to: 'on'
  action:
    - service: camera.snapshot
      data:
        entity_id: camera.front_door
        filename: '/config/www/images/frontdoor.jpg'
    - service: notify.home_assistant
      data:
        data:
          file: '/config/www/images/frontdoor.jpg'
        title: "Motion near the door"
        message: "Motion detected at door {{now().strftime('%H:%M %d-%m-%Y')}}"
5 Likes

Thanks for the example scripts! Do you guys also delete the screenshots after e.g. a week to prevent the sd card gets full? Or do you have another solution for this?

My automation only produces 1 file it keeps rewriting over the same file

@goprojojo see my script above, thatā€™s what is being done, deleting mini-videos older than 30 days

2 Likes