Save Camera Image (when motion detected)

I would be cool if there would be a built-in feature which enables you to easily save images of a attached webcam at local storage or in the cloud (e.g. owncloud, nextcloud) when motion is detected…

I know there is camera.local_file, but with saving the image only when necessary you could reduce read/write access to local/external storage.

I have something like this already setup via script. If you are interested let me know and I can shed more details as it is a little involved.

Sure, I’m highly interested

what type of camera do you have?

I’m using an USB camera attached to the rPi running Hass.

camera:
  - platform: ffmpeg
    input: -i /dev/video0

Was hoping you were going to say foscam or amcrest. I leverage the camera api to handle motion and poll the camera using a command line sensor to detect if the camera has detected motion. This may not work in your case if the USB camera does not have a means of detecting motion.

I have a motion sensor (PIR) right underneath the camera. In case I can also use:

Here is the script I use:

I grab the images from the camera these commands:

snapshot: wget “http://192.168.1.73:88/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=admin&pwd=pwd” -O {{filename}}
fix_snapshot: convert {{filename}} {{filename}}

The cameras are PTZ so I move them to grab other images with the ptz commands.

Then use them in this script:
################
## HOME ALARM ##
################

 alarm:
   alias: 'Run alarm when motion is detected'
   sequence:
   - delay:
       seconds: 1   
# FAMILY ROOM - POOL
   - service: shell_command.snapshot
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot.jpg
   - service: shell_command.fix_snapshot
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot.jpg
   - delay:
       seconds: 1
# GARAGE
   - service: shell_command.snapshot1
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot1.jpg
   - service: shell_command.fix_snapshot1
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot1.jpg       
   - delay:
       seconds: 1
#FRONT DOOR
   - service: shell_command.snapshot2
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot2.jpg
   - service: shell_command.fix_snapshot2
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot2.jpg
   - delay:
       seconds: 1
#KITCHEN
   - service: shell_command.snapshot3
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot3.jpg
   - service: shell_command.fix_snapshot3
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot3.jpg
   - delay:
       seconds: 1
#FAMILY ROOM - FAMILY ROOM
   - service: shell_command.familyroom_ptz_familyroom
   - delay:
       seconds: 2
   - service: shell_command.snapshot4
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot4.jpg
   - service: shell_command.fix_snapshot4
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot4.jpg
   - delay:
       seconds: 1
# FRONT DOOR - FOYER
   - service: shell_command.frontroom_ptz_foyer
   - delay:
       seconds: 2
   - service: shell_command.snapshot5
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot5.jpg
   - service: shell_command.fix_snapshot5
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot5.jpg
   - delay:
       seconds: 1
#KITCHEN - FAMILY ROOM
   - service: shell_command.kitchen_ptz_family
   - delay:
       seconds: 2
   - service: shell_command.snapshot6
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot6.jpg
   - service: shell_command.fix_snapshot6
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot6.jpg
   - delay:
       seconds: 1
#FAMILY ROOM - UPSTAIRS
   - service: shell_command.familyroom_ptz_upstairs
   - delay:
       seconds: 2
   - service: shell_command.snapshot7
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot7.jpg
   - service: shell_command.fix_snapshot7
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot7.jpg
   - delay:
       seconds: 1
#FRONT DOOR - DINING ROOM
   - service: shell_command.frontroom_ptz_dining
   - delay:
       seconds: 2
   - service: shell_command.snapshot8
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot8.jpg
   - service: shell_command.fix_snapshot8
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot8.jpg
   - delay:
       seconds: 1
#KITCHEN - PATIO
   - service: shell_command.kitchen_ptz_patio
   - delay:
       seconds: 2
   - service: shell_command.snapshot9
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot9.jpg
   - service: shell_command.fix_snapshot9
     data:
       filename: /home/hass/.homeassistant/snaps/snapshot9.jpg
   - delay:
       seconds: 1
   - service: notify.emailer
     data:
       title: 'Intruder alert'
       message: 'Motion detected at Home'
       data:
         images: 
           - /home/hass/.homeassistant/snaps/snapshot.jpg
           - /home/hass/.homeassistant/snaps/snapshot1.jpg
           - /home/hass/.homeassistant/snaps/snapshot2.jpg
           - /home/hass/.homeassistant/snaps/snapshot3.jpg
           - /home/hass/.homeassistant/snaps/snapshot4.jpg
           - /home/hass/.homeassistant/snaps/snapshot5.jpg
           - /home/hass/.homeassistant/snaps/snapshot6.jpg
           - /home/hass/.homeassistant/snaps/snapshot7.jpg
1 Like

Thanks for the code, although it’s a little hard to read without proper formatting :sweat_smile:
I think I can use some of your ideas.

Anyway, what does?

convert {{filename}} {{filename}}

I know the command can convert from one file type into another, but there are now file endings.
So do you just compress them?

Have a look here: https://partofthething.com/thoughts/?p=1037 This is where I got the idea. I modified the code to include PTZ defined points.

I have solved it as follows:

configuration.yaml

shell_command:
  take_snapshot_webcam: 'bash /home/pi/.homeassistant/scripts/take_snapshot_webcam.sh'
  transfer_snapshot: 'scp -r -p /tmp/webcam_images user@server:/var/media/backup'

take_snapshot_webcam.sh

#!/bin/bash

outdir=/tmp/webcam_images

if [[ ! -d "$outdir" ]]; then
  mkdir -p "$outdir"
fi

ffmpeg -f v4l2 -video_size 1280x720 -i /dev/video0 -vframes 5 -vf fps=1 -strftime 1 "$outdir"/%Y-%m-%d_%H-%M-%S.jpg

script.yaml

webcam_snapshot:
  alias: Take Webcam Snapshots
  sequence:
    - service: shell_command.take_snapshot_webcam
    - service: shell_command.transfer_snapshot

automation.yaml

- alias: Take Webcam Snapshots
  trigger:
    platform: state
    entity_id: binary_sensor.pir_hall
    to: 'on'
  action:    
  - service: homeassistant.turn_on
    data:
      entity_id: script.webcam_snapshot

I hope this will help others, too. :slight_smile:

4 Likes

I’ve done similar also but used sendmail to email the grabs

as follows. Get the list of files:

files=`ls /scripts/alerts/* | sed ':a;N;$!ba;s/\n/ -a /g'`

then send the email with the files

sendEmail .......... -a $files > /scripts/email.txt

Is this really the best way?
If homeassistant has access to all my cameras, why do I need an external script to grab an image. Seems like unnecessary duplication

1 Like

You are free to make a new feature request :slight_smile: or if you already have a solution even a pull request :grin:

So to clarify, @7h30n3 are you stating that there is no current way within HASS to get a current snapshot from the camera entity? Which I think was the question that @brucealdridge asked.

Yes, it seems that there is no built-in way to easily capture images or videos.

During some research I discovered that the foscam.component uses the snapPicture2 CGI-Command from the Foscam API, as it can be seen here: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/camera/foscam.py#L65

Where is the snapped picture stored and is it possible to save it in case it’s needed?

Picking this back up, I’ve tweaked this, and now the images go via my telegram bot, with buttons on the message sent to request an additional set of images if needed!

This works much better than email!

Here’s the latest related to the status of motion detection and getting snapshots in HA: https://github.com/home-assistant/home-assistant/commit/659dc2e55706a15444adf212b32cc047c708af26

@pvizeli indicated some time back that @fabaff was working on a snapshot service, but there are no updates on it. See this PR by @tchellomello for some context.

1 Like