The thumbnail is created by the controller once the event has finished - your automation triggers when motion starts, so longer motion events will outlast your 10 second delay and the thumbnail will not be available.
Still yet to refine my current G4 automation to send smart object detection events when first triggered, but the current state (including dynamic file name for the notification) is below in case it helps.
Notes:
- I use the event score and length to provide additional filtering of motion events unless the motion is a smart detection of an object.
- I trigger on the end of the motion event to ensure that the thumbnail is available (only seems to 404 when it’s raining). I’m planning to move the person / vehicle smart detections to be triggered on the start of a motion event and captured with a snapshot, but haven’t got round to it yet.
- On HA startup I load sensors with local and remote image paths from my secrets.yaml for easy maintenance.
- I use the original driveway_motion.jpg to provide a picture entity in my dashboard for the last motion event, and send the copied image with a dynamic file name as the notification. Images are cleaned up with a maintenance automation that runs a command line removal of images over 14 days old.
- The camera always records but whether we are notified or not depends on input_boolean.notify_driveway which is set by time of day or through the dashboard.
- /lovelace/camera_driveway is a hidden Lovelace view / tab with a panel mode live view picture entity showing the driveway camera.
- Notification is sent to the official Android companion app.
- id: unifiprotect_driveway_motion_notification
alias: "Driveway Motion Notification"
mode: queued
trigger:
platform: state
entity_id: binary_sensor.motion_driveway
from: "on"
to: "off"
condition:
condition: and
conditions:
- condition: template
value_template: >
{{ ((is_state('sun.sun', 'above_horizon') and (state_attr('binary_sensor.motion_driveway', 'event_score') | int >= 50)))
or ((is_state('sun.sun', 'below_horizon') and (state_attr('binary_sensor.motion_driveway', 'event_score') | int >= 50)))
or (not is_state_attr('binary_sensor.motion_driveway', 'event_object', 'None Identified')) }}
- condition: template
value_template: >
{{ (is_state('sun.sun', 'above_horizon') and (state_attr('binary_sensor.motion_driveway', 'event_length') | int >= 6))
or (is_state('sun.sun', 'below_horizon') and (state_attr('binary_sensor.motion_driveway', 'event_length') | int >= 12))
or (not is_state_attr('binary_sensor.motion_driveway', 'event_object', 'None Identified')) }}
action:
- variables:
notify_image: "driveway_motion_{{ now().strftime('%Y%m%d%H%M') }}.jpg"
motion_type: "{{ 'Motion' if is_state_attr('binary_sensor.motion_driveway', 'event_object', 'None Identified') else (state_attr('binary_sensor.motion_driveway', 'event_object') | capitalize) }}"
motion_score: "{{ state_attr('binary_sensor.motion_driveway', 'event_score') }}"
motion_length: >
{% set time = (state_attr('binary_sensor.motion_driveway', 'event_length') | int) %}
{% set mins = ((time % 3600) / 60) | int %}
{% set secs = time - (mins * 60) %}
{% if time < 60 %}{{ secs }} seconds
{% else %}{{ mins }} minutes {{ secs }} seconds
{% endif %}
- service: input_datetime.set_datetime
data:
entity_id: input_datetime.last_driveway_motion
time: "{{ (as_timestamp(now()) | timestamp_custom('%H:%M:%S', true)) }}"
date: "{{ (as_timestamp(now()) | timestamp_custom('%Y-%m-%d', true)) }}"
- choose:
- conditions:
# no object type detected, just motion
- condition: template
value_template: "{{ not is_state('sensor.motion_recording_driveway', 'never') }}"
- condition: template
value_template: "{{ is_state_attr('binary_sensor.motion_driveway', 'event_object', 'None Identified') }}"
sequence:
- delay: "00:00:05"
- condition: state
entity_id: binary_sensor.motion_driveway
state: "off"
- service: unifiprotect.save_thumbnail_image
data:
entity_id: camera.driveway
filename: "{{ states('sensor.snapshot_local') }}driveway_motion.jpg"
image_width: 2688
- conditions:
# object detected (person, etc) - don't specify image width
- condition: template
value_template: "{{ not is_state('sensor.motion_recording_driveway', 'never') }}"
sequence:
- delay: "00:00:05"
- condition: state
entity_id: binary_sensor.motion_driveway
state: "off"
- service: unifiprotect.save_thumbnail_image
data:
entity_id: camera.driveway
filename: "{{ states('sensor.snapshot_local') }}driveway_motion.jpg"
default:
- service: camera.snapshot
data:
entity_id: camera.driveway
filename: "{{ states('sensor.snapshot_local') }}driveway_motion.jpg"
- service: shell_command.copy_file
data:
src_file: "{{ states('sensor.snapshot_local') }}driveway_motion.jpg"
dst_file: "{{ states('sensor.snapshot_local') }}{{ notify_image }}"
- choose:
- conditions:
- condition: and
conditions:
- condition: state
entity_id: input_boolean.notify_driveway
state: "on"
- condition: state
entity_id:
- input_boolean.holiday_mode
- input_boolean.notify_via_sms
state: "off"
sequence:
- service: notify.all_mobile
data:
title: "Driveway Alert"
message: >
{{ motion_type }} at {{ as_timestamp(state_attr('binary_sensor.motion_driveway', 'last_tripped_time')) | timestamp_custom('%H:%M') }}.<br>
Score: {{ motion_score }}. <br>
Length: {{ motion_length }}.
data:
image: "{{ states('sensor.snapshot_remote') }}{{ notify_image }}"
clickAction: "/lovelace/camera_driveway"
group: Driveway