Frigate provides a review topic to subscribe to for the purpose of sending notifications of an event that is being catalogued/processed by frigate.
The review status go from New, Update to End. It’s fairly straight forward to update the notifications via the Home assistant companion by simply retaining the tag as the review id {{ trigger.payload_json['after']['id'] }}
.
I’ve been trying to extend this to pick up additional information about the series of events from another MQTT topic tracked_object_update
. This is where I might find the AI descriptions for the series of events so far if they exist yet or at all.
I’ve assumed that by running my automation in parallel, that each instance is in its own scope. Also, as I understand it there is no way to globally update global variables from nested code.
The problems i’m having are:
- if the AI description arrived before UPDATE or END, then it gets overridden. I could solve this by ignoring UPDATE,END events, but these do provide updated snapshots etc. Would be nice to retain.
- if there are concurrent reviews, then I don’t always get the right description. This questions my understanding and implementation of sequencing and script mode, I have no idea how this happens and haven’t got a sure way to recreate this scenario.
What can I do to solve these issues?
- alias: "Security: Object Detected"
id: "HD2f4Q5RbLgXv"
mode: parallel
triggers:
- trigger: mqtt
alias: "topic published to /reviews"
topic: "frigate/reviews"
id: frigate-event
payload: "alert"
value_template: "{{ value_json['after']['severity'] }}"
variables:
type: "{{ trigger.payload_json['type'] }}"
review_id: "{{ trigger.payload_json['after']['id'] }}"
camera: "{{ trigger.payload_json['after']['camera'] }}"
before_zones: "{{ trigger.payload_json['before']['data']['zones'] }}"
after_zones: "{{ trigger.payload_json['after']['data']['zones'] }}"
before_objects: "{{ trigger.payload_json['before']['data']['objects'] }}"
after_objects: "{{ trigger.payload_json['after']['data']['objects'] }}"
sub_labels: "{{ trigger.payload_json['after']['data']['sub_labels'] }}"
detections: "{{ trigger.payload_json['after']['data']['detections'] }}"
thumbnail: "https://ha.mydomain.com/api/frigate/notifications/{{detections[0]}}/thumbnail.jpg"
snapshot: "https://ha.mydomain.com/api/frigate/notifications/{{detections[0]}}/snapshot.jpg"
clip: "https://ha.mydomain.com/api/frigate/notifications/{{detections[0]}}/clip.mp4"
# msg_description: "{{ type }}: {{ after_objects|join(', ')|title }} detected"
# msg_description: "{{ type }}: rid:{{ review_id }} dids: {{ detections | join(', ') }}"
msg_title: "{{ camera|title }} camera detected {{ after_objects|join(', ')|title }}"
# condition: # This condition just delays getting any early notifications.
# - alias: Object lifecycle has ended
# condition: template
# value_template: '{{ type == "end" }}' # or before_zones | length < after_zones | length }}'
# condition: # This would prevent the AI description from being overridden in subsequent events.
# - alias: Object lifecycle has started
# condition: template
# value_template: '{{ type == "new" }}' # or before_zones | length < after_zones | length }}'
actions:
- parallel:
- sequence:
- condition: template # This might reduce duplication, as we expect the automation to fire for 'new', 'end' and each 'update'
alias: Object lifecycle has ended
value_template: '{{ type == "new" }}'
# - condition: template # This might reduce duplication, but may miss updates to the description.
# value_template: "{{ msg_description != None }}"
- wait_for_trigger:
- trigger: mqtt
topic: frigate/tracked_object_update
value_template: '{{ value_json["type"] == "description" and value_json["id"] in detections }}'
# variables: # does it matter where msg_description is defined?
# msg_description: "{{ type }}: {{ wait.trigger.payload_json['description'] }}"
timeout:
seconds: "120"
- condition: template
alias: Object updated
value_template: "{{ wait.completed }}"
- variables:
# msg_description: "{{ type }} update: rid:{{ review_id }} oid:{{ wait.trigger.payload_json['id'] }} dids: {{ detections | join(', ') }}"
msg_description: "{{ wait.trigger.payload_json['description'] }}"
- action: notify.residents
data:
title: "{{ type }}: {{ msg_title }}"
message: "{{ msg_description }}"
data:
group: "Security"
channel: "MEDIUM"
image: "{{ thumbnail }}"
tag: "{{ review_id }}"
entity_id: camera.{{ camera }}
color: "#FFFF00" # YELLOW
priority: "high"
ttl: 0
persistent: "false"
sticky: "false"
actions:
- action: "URI"
title: "Thumbnail"
uri: "{{ thumbnail }}"
- action: "URI"
title: "Image"
uri: "{{ snapshot }}"
- action: "URI"
title: "Video"
uri: "{{ clip }}"
- sequence:
# - condition: template
# value_template: "{{ msg_description != None }}"
- action: notify.residents
data:
title: "{{ type }}: {{ msg_title }}"
message: "{{ msg_description }}"
data:
group: "Security"
channel: "MEDIUM"
image: "{{ thumbnail }}"
tag: "{{ review_id }}"
entity_id: camera.{{ camera }}
color: "#FFFF00" # YELLOW
priority: "high"
ttl: 0
persistent: "false"
sticky: "false"
actions:
- action: "URI"
title: "Thumbnail"
uri: "{{ thumbnail }}"
- action: "URI"
title: "Image"
uri: "{{ snapshot }}"
- action: "URI"
title: "Video"
uri: "{{ clip }}"
Thanks