Skip to content

Receiving and processing MQTT messages from Frigate NVR #632

@sevmonster

Description

@sevmonster

I set up a Frigate -> Mosquitto -> mqttwarn -> Apprise -> Ntfy -> Nginx flow (whew) and everything is working... Almost. The filter seems to not be blocking bad messages even though it should be based on my debugging. And, I can't figure out how to send attachments. My only lead is to save the attachment to a local file before using the attach query parameter in my Ntfy URI, but it would be nice to be able to send it through the body of the message instead, which I believe Apprise supports.

Depending on if any of this not working is a bug or potential feature request, this issue could turn into an actual issue and not a support request :)

mqttwarn.ini

[defaults]
functions = funcs.py
launch    = apprise-ntfy

status_publish = True


[config:apprise-ntfy]
module   = apprise_single
baseuri  = ntfys://ntfy/frigate


[frigate/events]
filter  = frigate_events_filter()
alldata = frigate_events()
targets = apprise-ntfy
title   = {title}
format  = {format}
click   = {click}

funcs.py

# -*- coding: utf-8 -*-
from datetime import datetime

try:
    import json
except ImportError:
    import simplejson as json

def frigate_events(topic, data, srv=None):
    if srv is not None:
        srv.logging.debug('events ******************************************')
    a = json.loads(data['payload'])['after']
    f = lambda x: (y.replace('_', ' ') for y in x)
    r = {
      'camera': a['camera'],
      'label':  a['sub_label'] or a['label'],
      'current_zones': ', '.join(f(a['current_zones'])),
      'entered_zones': ', '.join(f(a['entered_zones'])),
      'time':   datetime.fromtimestamp(a['frame_time'])
    }
    r.update({
      'title':  f"{r['label']} entered {r['entered_zones']}",
      'format': f"In zones {r['current_zones']} at {r['time']}",
      'click':  f"https://frigate/events?camera={r['camera']}&label={r['label']}&zone={a['entered_zones'][0]}"
    })
    return r

def frigate_events_filter(topic, message, section, srv=None):
    if srv is not None:
        srv.logging.debug('filter ******************************************')
    try:
        message = json.loads(message)
    finally:
        message = None
    if message and message.get('type', None) != 'end' and 'after' in message:
        a = message['after']
        return False not in (x in a and (x == 'current_zones' or a[x]) for x in
                                  ('false_positive', 'camera', 'label',
                                   'current_zones', 'entered_zones',
                                   'frame_time'))
    return False

Sample payload

{
  "before": {
    "id": "1680791459.255384-abcdef",
    "camera": "camera",
    "frame_time": 1680791459.255384,
    "snapshot_time": 0,
    "label": "car",
    "sub_label": null,
    "top_score": 0,
    "false_positive": true,
    "start_time": 1680791459.255384,
    "end_time": null,
    "score": 0.7,
    "box": [
      0,
      20,
      0,
      20
    ],
    "area": 400,
    "ratio": 1,
    "region": [
      0,
      0,
      320,
      320
    ],
    "stationary": false,
    "motionless_count": 0,
    "position_changes": 0,
    "current_zones": [],
    "entered_zones": [],
    "has_clip": false,
    "has_snapshot": false
  },
  "after": {
    "id": "1680791459.255384-abcdef",
    "camera": "camera",
    "frame_time": 1680791506.638857,
    "snapshot_time": 1680791506.638857,
    "label": "car",
    "sub_label": null,
    "top_score": 0.75,
    "false_positive": false,
    "start_time": 1680791459.255384,
    "end_time": null,
    "score": 0.8,
    "box": [
      1,
      21,
      1,
      21
    ],
    "area": 400,
    "ratio": 1,
    "region": [
      0,
      0,
      320,
      320
    ],
    "stationary": false,
    "motionless_count": 1,
    "position_changes": 2,
    "current_zones": [],
    "entered_zones": [
      "zone1"
    ],
    "has_clip": true,
    "has_snapshot": true
  },
  "type": "new"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions