Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save felschr/6194c361e4cab383c685490beec09e1e to your computer and use it in GitHub Desktop.
Save felschr/6194c361e4cab383c685490beec09e1e to your computer and use it in GitHub Desktop.
Home Assistant Blueprint - Notify & save a camera snapshot with archive when motion is detected
blueprint:
name: Notify & save a camera snapshot when motion is detected
description: Notify & save a camera snapshot with archive when motion is detected
domain: automation
source_url: https://gist.github.com/felschr/6194c361e4cab383c685490beec09e1e
author: felschr
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
media_dir_name:
name: Media Directory Name
description: >
The name of the media source directory: https://www.home-assistant.io/integrations/media_source/
The default media directory is: local
default: 'local'
media_dir_path:
name: Media Directory Path
description: >
The path to the media source directory: https://www.home-assistant.io/integrations/media_source/
The default path for the "local" media directory is: /media
default: '/media'
file:
name: File Path
description: >
The file path to store the most current snapshot.
This needs to be in the Media Source directory to be accessible from notifications:
https://companion.home-assistant.io/docs/notifications/notification-attachments/#media_source-recommended
default: '{{ media_dir_path }}/cameras/{{ camera_id }}/last_motion.jpg'
archive_file:
name: Archive File Path
description: The file path to store the snapshot in an archive folder.
default: '{{ media_dir_path }}/cameras/{{ camera_id }}/archive/motion_{{ now().strftime("%Y%m%d-%H%M%S") }}.jpg'
notify_devices:
name: Devices to notify
description: >
Devices to send notifications to. If left empty all devices will be notified.
default: []
selector:
device:
multiple: true
integration: mobile_app
notification_title:
name: Notification title (Optional)
description: 'Default: "Motion detected!"'
default: "Motion detected!"
notification_message:
name: Notification message (Optional)
description: 'Default: "{{ motion_sensor_name }} detected movement!"'
default: "{{ motion_sensor_name }} detected movement!"
delay:
name: Delay (Optional)
description: Wait before creating camera snapshot
default: 0.0
selector:
number:
min: 0.0
max: 60.0
unit_of_measurement: seconds
mode: slider
step: 1.0
variables:
motion_sensor: !input motion_sensor
motion_sensor_name: "{{ states[motion_sensor].name }}"
camera: !input camera
camera_id: "{{ states[camera].object_id }}"
camera_name: "{{ states[camera].name }}"
media_dir_name: !input media_dir_name
media_dir_path: !input media_dir_path
media_dir_path_www: "media/{{ media_dir_name }}"
file: !input file
file_www: "{{ file | replace(media_dir_path, media_dir_path_www) }}"
notify_devices: !input notify_devices
trigger:
platform: state
entity_id: !input motion_sensor
from: 'off'
to: 'on'
action:
- delay: !input delay
- service: camera.snapshot
entity_id: !input camera
data:
filename: !input file
- service: camera.snapshot
entity_id: !input camera
data:
filename: !input archive_file
- alias: "Notify devices"
repeat:
for_each: !input notify_devices
sequence:
- service: 'notify.mobile_app_{{ device_attr(repeat.item, "name") | slugify }}'
data:
title: !input notification_title
message: !input notification_message
data:
image: "{{ file_www }}"
- condition: template
value_template: "{{ notify_devices == [] }}"
- alias: "Notify all devices"
service: 'notify.notify'
data:
title: !input notification_title
message: !input notification_message
data:
image: "{{ file_www }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment