docker-apps/home-assistant/home-automation3.yml

107 lines
5.3 KiB
YAML

alias: Frigate Person Detect Notification
description: 'This automation is used to send a notification when a person is detected by Frigate. It uses the frigate MQTT event stream to trigger the notification and includes a snapshot of the detected object. The notification will be sent to all devices with the specified group and will include a video clip of the detection if available.'
# The trigger is mqtt and will fire whenever an object is detected by YOUR_CAMERA_NAME
trigger:
- platform: mqtt
topic: homeassistant/events
id: frigate-event
payload: Driveway
value_template: "{{ value_json['after']['camera'] }}"
variables:
after_zones: "{{ trigger.payload_json['after']['entered_zones'] }}"
before_zones: "{{ trigger.payload_json['before']['entered_zones'] }}"
camera: "{{ trigger.payload_json['after']['camera'] }}"
id: "{{ trigger.payload_json['after']['id'] }}"
label: "{{ trigger.payload_json['after']['label'] }}"
score: "{{ trigger.payload_json['after']['score'] }}"
time_clip_start: "{{ trigger.payload_json['after']['start_time'] - 10.0 }}"
# Conditions must be used to filter out objects / detections that are not of interest
condition:
# This condition is used to ensure that I am only notified of objects that are new or only just entered a zone. This is to avoid getting loads of updated notifications for the same dog in my backyard (as an example).
- condition: or
conditions:
- condition: template
value_template: "{{ trigger.payload_json['type'] == 'new' }}"
- condition: template
value_template: "{{ before_zones | length == 0 }}"
# This ensures that the object at least has one zone, this can be removed if you don't care about zones
- condition: template
value_template: '{{ trigger.payload_json["after"]["entered_zones"]|length > 0 }}'
# This specifically checks for the object to be in the specified zones. This can be removed or those names changed to fit your use case.
#for example:
- condition: template
value_template: >-
{{ ["driveway_z1", "Driveway_Paving"] | select("in",
after_zones) | list | length > 0 }}
alias: Object is in Driveway
# This filters out any car objects. This can be removed if not applicable to your use case.
- condition: template
value_template: '{{ trigger.payload_json["after"]["label"] != "car" }}'
action:
- choose:
- conditions:
- condition: trigger
id: frigate-event
sequence:
# Send the initial notification with snapshot and info.
- service: script.send_dynamic_notification_to_all_devices
data:
id: "{{ id }}"
title: >-
{{ label }} was
detected on {{ camera |
replace("_", " ") | title }}
message: >-
{{ label }} detected in
the {{ after_zones[0] |
replace("_", " ") | title }}
notifTag: "{{ id }}"
notifIcon: mdi:doorbell-video
group: >-
frigate-notification-{{ camera }}
importance: max
image: >-
/api/frigate/notifications/{{ id }}/snapshot.jpg
clickAction: ''
# Wait until the end of the notification has occurred.
- repeat:
until:
- condition: template
value_template: '{{ wait.trigger.payload_json["type"] == "end" }}'
sequence:
- wait_for_trigger:
- platform: mqtt
topic: homeassistant/events
payload: "{{ trigger.payload_json[\"after\"][\"id\"] }}"
value_template: "{{ value_json[\"after\"][\"id\"] }}"
continue_on_timeout: false
timeout: '00:02:00'
- condition: template
value_template: '{{ wait.trigger.payload_json[''type''] == ''end'' }}'
- service: script.send_dynamic_notification_to_all_devices
data:
id: '{{ id }}'
# Uses double take for title and message to include face info if a match is detected.
title: >-
{% if (wait.trigger.payload_json["after"]["sub_label"] !=
None) -%} {{ (wait.trigger.payload_json["after"]["sub_label"])
| title }} {%- else -%} {{ label | replace("_", " ") | title
}} {%- endif %} was detected in the {{ after_zones[0] |
replace("_", " ") | title }}
message: Detected with {{ int(score | round(2) * 100) }}% confidence
notifTag: '{{ id }}'
notifIcon: mdi:doorbell-video
group: >-
frigate-notification-{{
trigger.payload_json["after"]["camera"] }}
importance: default
image: >-
/api/frigate/notifications/{{ id }}/snapshot.jpg
video: >-
home-assistant.sthome.org/api/{{id}}/{{camera}}/clip.mp4
clickAction: >-
https://home-assistant.sthome.org/api/frigate/notifications/{{id}}/{{camera}}/clip.mp4
default: []
mode: parallel
max: 10