Save a camera snapshot with archive when motion is detected

Github Gist: https://gist.github.com/niemyjski/08c566a4fe64fb2e70998ee4f38c9d1e

Explanation

Added the ability to save a camera snapshot to two folders one for latest and one for archive. I’m using it for taking snapshots of when my mailbox is opened.

Known issues:

There seems to be a bug with templates where it doesn’t pick up the defaults for file and archive file. Please copy those values from the blueprint.

Blueprint Code

blueprint:
  name: Save a camera snapshot when motion is detected
  description: Save a camera snapshot with archive when motion is detected
  domain: automation
  source_url: https://gist.github.com/niemyjski/08c566a4fe64fb2e70998ee4f38c9d1e
  input:
    motion_sensor:
      name: Motion sensor
      description: The sensor wich triggers the snapshot creation
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    camera:
      name: Camera
      description: The camera which creates the snapshot
      selector:
        entity:
          domain: camera
    file:
      name: File Path
      description: The file path to store the most current snapshot.
      default: '/media/CAMERA_NAME/last_motion.jpg'
    archive_file:
      name: Archive File Path
      description: The file path to store the snapshot in an archive folder.
      default: '/media/CAMERA_NAME/archive/motion_{{ now().strftime("%Y%m%d-%H%M%S") }}.jpg'
    delay:
      name: Delay (Optional)
      description: Wait before creating camera snapshot
      default: ''
      selector:
        number:
          min: 0.0
          max: 60.0
          unit_of_measurement: seconds
          mode: slider
          step: 1.0
trigger:
  platform: state
  entity_id: !input motion_sensor
  from: 'off'
  to: 'on'
variables:
  motion_sensor: !input 'motion_sensor'
  camera: !input 'camera'
  file: !input file
  archive_file: !input archive_file
  delay: !input 'delay'
action:
- delay: '{{ delay }}'
- service: camera.snapshot
  entity_id: !input 'camera'
  data:
    filename: !input file
- service: camera.snapshot
  entity_id: !input 'camera'
  data:
    filename: !input archive_file
8 Likes

Hi
Could you ad delay as an option as the original?

Best Regards

I’ve added the delay

Thank you :smiley:

Excellent !
Thanks @niemyjski for your sharing !
It works perfectly and is super simple!

I have a question: I would like to take a sequence of photos.
For example 4 pictures during the next minute after motion detection.
How can I modify the Blueprint to get such a sequence?
Thanks for your feedback

1 Like

Welcome and thanks for reaching out! I would probably put the actions inside of a sequence: https://www.home-assistant.io/blog/2020/07/22/release-113/#automations--scripts-sub-second-precision

Awesome !
Thanks for the Quick Reply !
Also, if I want to put a condition to trigger the snapshot, what would you advise?
Directly put this condition in the automation YAML ?
Thanks for your help :pray:

Is it possible to add VIBRATION in device_class? i’m using a Xiaomi Aqara vibration sensor put in front of the camera to trigger the automation.

Thanks

What kind of condition do you want to put? You’d probably have to modify the blueprint. I thought they were going to allow you to override a conditions section of a blueprint but I’m not sure if this is still the plan.

I’m wondering if we should just remove the device class completely. What are your thoughts?

1 Like

It would be great to be able to add more than one trigger entity too. For example both a motion sensor and a door sensor.

3 Likes

Yes ! +1 with @teachingbirds

2 Likes

Hey quick question … how do you manage your archive folder ? do you keep like infinite snapshot ?

Thanks

I have a ton of storage so I don’t clear anything out. But you could always use the samba addon or ssh addon to quickly remove images.

if you could add the option that you also receive a notification - it would be great!

I have home assistant running in a docker and when running automation i get the following error.

Can’t write /media/sonoff_01/last_motion.jpg, no access to path!
Can’t write /media/sonoff_01/archive/motion_20211208-172553.jpg, no access to path!

I’ve already looked at the folder’s permissions in the container but I still can’t.

Thanks

Sounds like you need to mount storage with read write permissions

I have the home assistant running on container. I’ve already given permission both on the local folder (/usr/share/hassio/media) and on the container that runs the home assistant (/media) and I have the same problem. Where would it be necessary to give permission?

Thanks

I’m not sure as you’ve done what I would have tried. I’d reach out to support on discord as it’s a bit out of the scope of this blueprint.

First thanks for sharing and I am a newbie!

I am trying to use your blueprint with this automation:

- id: '1642174438258'
  alias: New Automation
  description: ''
  use_blueprint:
    path: homeassistant/camera.yaml
    input:
      archive_file: /media/CAMERA_NAME/archive/motion_{{ now().strftime("%Y%m%d-%H%M%S")
        }}.jpg
      motion_sensor: binary_sensor.ring_upstairs_motion
      camera: camera.ring_upstairs

and I am getting the following error:

and in the logs I get
" Logger: homeassistant.components.automation.new_automation
Source: helpers/script.py:1382
Integration: Automation (documentation, issues)
First occurred: 3:34:03 PM (2 occurrences)
Last logged: 3:34:54 PM

New Automation: Error rendering New Automation delay template: offset should be format ‘HH:MM’, ‘HH:MM:SS’ or ‘HH:MM:SS.F’"

any suggestion? Thanks