Messing with alternative options at the moment.
Unfortunately the thumbnail is not available at the point motion ends, giving a 404 response to the service call.
2020-05-28 12:50:26 ERROR (MainThread) [homeassistant.components.automation] Driveway Daytime Motion Ended Detected: Error executing script. Unexpected error for call_service at pos 1: Thumbnail Request failed: 404 - Reason: Not Found
Traceback (most recent call last):
File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 153, in _async_step
self, f"_async_{cv.determine_script_action(self._action)}_step"
File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/script.py", line 623, in _async_call_service_step
*self._prep_call_service_step(), blocking=True, context=self._context
File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 1253, in async_call
task.result()
File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 1288, in _execute_service
await handler.func(service_call)
File "/home/homeassistant/.homeassistant/custom_components/unifiprotect/__init__.py", line 160, in async_save_thumbnail
await async_handle_save_thumbnail_service(hass, call)
File "/home/homeassistant/.homeassistant/custom_components/unifiprotect/__init__.py", line 258, in async_handle_save_thumbnail_service
await _write_thumbnail(camera_id, filename, image_width)
File "/home/homeassistant/.homeassistant/custom_components/unifiprotect/__init__.py", line 244, in _write_thumbnail
camera_id, image_width
File "/home/homeassistant/.homeassistant/custom_components/unifiprotect/unifi_protect_server.py", line 362, in get_thumbnail
f"Thumbnail Request failed: {response.status} - Reason: {response.reason}"
custom_components.unifiprotect.unifi_protect_server.NvrError: Thumbnail Request failed: 404 - Reason: Not Found
So currently I can either continue as is without the scoring filter, or grab an image after the motion event finishes without the reason for the motion shown. I could (I suppose) delay the service call for a few seconds, but that would rather defeat the purpose.
If anyone has any better ideas I’m all ears - test automation below for reference.
- id: unifiprotect_driveway_daytime_motion_ended_notification
alias: "Driveway Daytime Motion Ended Detected"
trigger:
platform: state
entity_id: binary_sensor.unifiprotect_motion_driveway
from: "on"
to: "off"
condition:
condition: and
conditions:
- condition: state
entity_id: input_boolean.notify_driveway
state: "on"
- condition: state
entity_id: input_boolean.holiday_mode
state: "off"
- condition: state
entity_id: input_boolean.notify_via_sms
state: "off"
- condition: template
value_template: >
{{ ((is_state('sun.sun', 'above_horizon') and (state_attr('binary_sensor.unifiprotect_motion_driveway', 'event_score') | int >= 50)))
or ((is_state('sun.sun', 'below_horizon') and (state_attr('binary_sensor.unifiprotect_motion_driveway', 'event_score') | int >= 50))) }}
- condition: template
value_template: >
{{ (is_state('sun.sun', 'above_horizon') and (as_timestamp(now()) - as_timestamp(state_attr('camera.driveway', 'last_tripped_time')) | int >= 6))
or (is_state('sun.sun', 'below_horizon') and (as_timestamp(now()) - as_timestamp(state_attr('camera.driveway', 'last_tripped_time')) | int >= 12)) }}
action:
- service: unifiprotect.save_thumbnail_image
data_template:
entity_id: camera.driveway
filename: "{{ states('sensor.snapshot_local') }}driveway_lastmotion.jpg"
- service: notify.mobile_app_turk
data_template:
title: "Driveway Alert"
message: >
Driveway camera motioned detected at {{ as_timestamp(state_attr('camera.driveway', 'last_tripped_time')) | timestamp_custom('%H:%M') }}.{{ '\n' -}}
Score: {{ state_attr('binary_sensor.unifiprotect_motion_driveway', 'event_score') }}.{{ '\n' -}}
{% set time = (as_timestamp(now()) - as_timestamp(state_attr('camera.driveway', 'last_tripped_time'))) | int %}
{% set mins = ((time % 3600) / 60) | int %}
{% set secs = time - (mins * 60) %}
{% if time < 60 %}Length: {{ secs }}s
{% else %}Length: {{ mins }}m {{ secs }}s
{% endif %}
data:
image: "{{ states('sensor.snapshot_remote') }}driveway_lastmotion.jpg"
actions:
- action: "URI"
title: "View Live Feed"
uri: "/lovelace/camera_driveway"