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

107 lines
5.1 KiB
YAML

alias: Outside - Driveway & Front Yard - Frigate Event - Cat / Dog / Person Seen
description: ''
# The trigger is mqtt and will fire whenever an object is detected by YOUR_CAMERA_NAME
trigger:
- platform: mqtt
topic: frigate/events
id: frigate-event
payload: YOUR_CAMERA_NAME
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 driveway or front_yard zone. This can be removed or those names changed to fit your use case.
for example:
- condition: template
value_template: >-
{{ ["driveway", "front_yard"] | select("in",
after_zones) | list | length > 0 }}
alias: Object is in Driveway or Front Yard
# This filters out any car objects as I have a separate automation with different behavior for car. 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: frigate/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: >-
HASS_URL_BASE/api/{{id}}/{{camera}}/clip.mp4
clickAction: >-
http://HASS_URL_BASE/api/frigate/notifications/{{id}}/{{camera}}/clip.mp4
default: []
mode: parallel
max: 10