blakeblackshear / frigate

NVR with realtime local object detection for IP cameras
https://frigate.video
MIT License
18.94k stars 1.73k forks source link

[Support]: Car/person detection during the day and only people at night? #2876

Closed lee-costa closed 2 years ago

lee-costa commented 2 years ago

Describe the problem you are having

I am only opening this as an issue because with the upcoming launch of Frigate+, I suspect this to be something people may request or want to achieve.

I noticed that the way my camera is pointed is my driveway always gives me false alerts with parked cars so along with the power of Home Assistant, I configured my detectors to only be on when we are away and know that the cars are not there. I achieved that using location and a simple automation. The issue now is at night, I would like to turn on the detector but only allow the detector to pick up people, no cars as I know the cars will be there in the zone causing false alerts.

Is this possible? For some reason, I can't think it through.

Version

0.10.0

Frigate config file

cameras:
  driveway_cam:
    rtmp:
      enabled: False

    ffmpeg:
      inputs:
        - path: rtsp://ha:REDACTED$@192.168.141.14:554/cam/realmonitor?channel=1&subtype=0
          roles:
            - detect
            - record

    detect:
      width: 3840
      height: 2160
      fps: 5
      enabled: True

    zones:
      driveway:
        coordinates: 2876,2160,2083,1823,1928,1413,2175,1308,3840,1746,3840,2160
        objects:
          - person
          - dog
          - car

    objects:
      track:
        - person
        - dog
        - car
      filters:
        person:
          # Optional: minimum width*height of the bounding box for the detected object (default: 0)
          min_area: 5000
          # Optional: maximum width*height of the bounding box for the detected object (default: 24000000)
          max_area: 100000
          mask:
            - 0,0,0,2160,2098,2160,1928,1374,2690,948,3840,1130,3840,0
        car:
          # Optional: minimum width*height of the bounding box for the detected object (default: 0)
          min_area: 5000
          # Optional: maximum width*height of the bounding box for the detected object (default: 24000000)
          max_area: 200000
          mask:
            - 0,0,0,2160,2098,2160,1928,1374,2690,948,3840,1130,3840,0
        dog:
          mask:
            - 0,0,0,2160,2098,2160,1928,1374,2690,948,3840,1130,3840,0

    motion:
      # Increasing this value will make motion detection less sensitive
      # threshold: 30
      # default 25

      # Minimum size in pixels in the resized motion image that counts as motion.
      # Increasing this value will prevent smaller areas of motion from being detected. Decreasing will
      # make motion detection more sensitive to smaller moving objects.
      # As a rule of thumb:
      #  - 15 - high sensitivity
      #  - 30 - medium sensitivity
      #  - 50 - low sensitivity
      contour_area: 35
      # default: 30

      mask:
       - 0,0,0,2160,2098,2160,1928,1374,2690,948,3840,1130,3840,0

    record:
      enabled: True
      retain:
        days: 7
        mode: motion
      events:
        max_seconds: 240
        pre_capture: 5
        post_capture: 5
        retain:
          default: 7
          mode: active_objects
        objects:
          - person
          - car
          - dog
        required_zones:
          - driveway

    snapshots:
      enabled: True
      timestamp: True
      bounding_box: True
      crop: False
      required_zones:
        - driveway
      retain:
        default: 7
#####

mqtt:
  host: 192.168.141.21
  user: REDACTED
  password: REDACTED

detectors:
  coral:
    type: edgetpu
    device: usb

database:
  path: /db/frigate.db

snapshots:
  enabled: True
  timestamp: True
  bounding_box: True
  crop: False
  required_zones: []
  retain:
    default: 7

model:
  labelmap:
    15: animal
    16: animal
    17: animal
    18: animal
    19: animal
    20: animal
    21: animal
    22: animal
    23: animal
    24: animal

birdseye:
  enabled: False
  # width: 1920
  # height: 1080
  # quality: 8
  # mode: motion

Relevant log output

NA

FFprobe output from your camera

NA

Frigate stats

No response

Operating system

Debian

Install method

Docker Compose

Coral version

USB

Network connection

Wired

Camera make and model

Amcrest 4K

Any other information that may be helpful

No response

NickM-27 commented 2 years ago

It is possible to ignore it in home assistant but events will still be created. This has been requested previously and I believe there is a similar pinned issue.

lee-costa commented 2 years ago

That's what I am actually considering. I am planning on creating another zone with the exact same dimension as the other but only receiving notifications on this zone at night by enabling the automation with a bedtime routine.

Does that make sense?

lee-costa commented 2 years ago

Actually, I think there's no need for that. Simply creating another automation that only notifies when a person is detected and enabling it at night may be the way to go.

blakeblackshear commented 2 years ago

always gives me false alerts with parked cars

Can you be more specific? There are many ways to avoid alerts for parked cars with current features already.

NickM-27 commented 2 years ago

@blakeblackshear not sure if OP means the same thing but I've experienced similar at night due to cars driving by, their headlights cause the motion / object detection to go wild and there's lots of bounding boxes that are unpredictable to mask out / size filter out.

lee-costa commented 2 years ago

@NickM-27 and @blakeblackshear Exactly that!

NickM-27 commented 2 years ago

To be clear, not complaining as I know night is particularly challenging not to mention I have a feeling the COCO dataset doesn't have much data with night vision shots.

I personally just have the automation filter cars after a certain time. As that seems much more prone to night time false positives than person

lee-costa commented 2 years ago

To be clear, not complaining as I know night is particularly challenging not to mention I have a feeling the COCO dataset doesn't have much data with night vision shots.

I personally just have the automation filter cars after a certain time. As that seems much more prone to night time false positives than person

So you have an automation that at certain times, disables notifications? Just fishing for ideas ;-)

NickM-27 commented 2 years ago

I have a single automation. Says if object label is car then it must be before sunset to send notifications, other labels always send notifications

lee-costa commented 2 years ago

And with the above, you will see many events of your parked car I assume?

NickM-27 commented 2 years ago

And with the above, you will see many events of your parked car I assume?

Yeah I usually just clean them up the next day. It's not everyday there's a car in the driveway so I'm not concerned personally right now to spend more time trying to solve it.

blakeblackshear commented 2 years ago

Does your parked car score too low to be tracked as a stationary object at night?

NickM-27 commented 2 years ago

@blakeblackshear In my case it is possible that some camera settings might help, I haven't tried adjusting that, but the car is barely visible to the camera until the light from another car lights it up.

Screen Shot 2022-02-26 at 7 51 44 PM

blakeblackshear commented 2 years ago

It sounds like both of you have zones in the driveway with required zones set on events/snapshots/notifications. It may just be that you are asking to be notified when cars are in your driveway. Have you tried defining a zone for the street and requiring that cars have entered more than 1 zone for notifications? That should only notify you for cars that enter the driveway after having been on the street.

NickM-27 commented 2 years ago

It sounds like both of you have zones in the driveway with required zones set on events/snapshots/notifications. It may just be that you are asking to be notified when cars are in your driveway. Have you tried defining a zone for the street and requiring that cars have entered more than 1 zone for notifications? That should only notify you for cars that enter the driveway after having been on the street.

Hmm, that's a smart idea, that way I could keep the stationary tracking during the day and also keep the notifications for when my fiancé gets home after dark without worrying about the notifications from what we're talking about here. Will have to try that, and maybe tweaking the camera settings 👌

lee-costa commented 2 years ago

Have you tried defining a zone for the street and requiring that cars have entered more than 1 zone for notifications? That should only notify you for cars that enter the driveway after having been on the street.

That sound like a great idea but I am not sure how to implement it. My street is visible from the camera and is quite active. Do it create a zone for the actual street and one for my driveway?

blakeblackshear commented 2 years ago

Yes. The street would be a separate zone.

lee-costa commented 2 years ago

I see. I think it makes sense. A zone for the ‘street’ and one for ‘driveway’. Under recording and snapshot, only have ‘driveway’ for required otherwise my busy street would be recorded/snapshotted all the time. Under iOS notifications, the required zone would be both?

lee-costa commented 2 years ago

Does your parked car score too low to be tracked as a stationary object at night?

And to answer this earlier question, yes it does.

lee-costa commented 2 years ago

Well, I created the two zones (street and driveway) and made sure to create a notification requiring both zones but I am always alerted when a car enters the street zone. Based on your comment Have you tried defining a zone for the street and requiring that cars have entered more than 1 zone for notifications?, I was under the impression that both zones would need to be triggered for a notification?

Am I missing something obvious? And thank you for your patience and support.

lee-costa commented 2 years ago

Sorry, forgot to attach a screenshot:

Screen Shot 2022-02-28 at 8 46 44 AM

NickM-27 commented 2 years ago

@junior466 How did you setup your notification, are you using the notification blueprint? I believe the notification blueprint ORs the zones. May need to set something up yourself for that.

lee-costa commented 2 years ago

Yes, I am using the Blueprint. That makes sense. I need to do some research on creating my own notification. By any chance, are you using something similar that you can share?

NickM-27 commented 2 years ago

@junior466 Keep in mind this is decently customized to me, and I have my own script which sends notifications to my devices, but this should be a good starting point.

alias: Outside - Driveway & Front Yard - Frigate Event - Cat / Dog / Person Seen
description: ''
trigger:
  - platform: mqtt
    topic: frigate/events
    id: frigate-event
    payload: front_doorbell_cam
    value_template: '{{ value_json["after"]["camera"] }}'
condition:
  - condition: or
    conditions:
      - condition: template
        value_template: '{{ trigger.payload_json[''type''] == ''new'' }}'
      - condition: template
        value_template: '{{ trigger.payload_json["before"]["entered_zones"]|length == 0 }}'
  - condition: template
    value_template: '{{ trigger.payload_json["after"]["entered_zones"]|length > 0 }}'
  - condition: template
    value_template: >-
      {{ ["driveway", "front_yard"]|select("in",
      trigger.payload_json["after"]["entered_zones"])|list|length > 0 }}
  - condition: template
    value_template: '{{ trigger.payload_json["after"]["label"] != "car" }}'
action:
  - choose:
      - conditions:
          - condition: trigger
            id: frigate-event
        sequence:
          - service: script.send_dynamic_notification_to_all_devices
            data:
              id: '{{ trigger.payload_json["after"]["id"] }}'
              title: >-
                {{ trigger.payload_json["after"]["label"] | title }} was
                detected on {{ trigger.payload_json["after"]["camera"] |
                replace("_", " ") | title }}
              message: >-
                {{ trigger.payload_json["after"]["label"] | title }} detected in
                the {{ trigger.payload_json["after"]["entered_zones"][0] }}
              notifTag: '{{ trigger.payload_json["after"]["id"] }}'
              notifIcon: mdi:doorbell-video
              group: >-
                frigate-notification-{{ trigger.payload_json["after"]["camera"]
                }}
              importance: max
              image: >-
                /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
              clickAction: ''
          - 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: '{{ trigger.payload_json["after"]["id"] }}'
                    title: >-
                      {% if (state_attr("sensor.double_take_front_doorbell_cam",
                      "id") == trigger.payload_json["after"]["id"]) and
                      (state_attr("sensor.double_take_front_doorbell_cam",
                      "matches") | length > 0) -%}
                      {{(state_attr("sensor.double_take_front_doorbell_cam",
                      "matches") | first)["name"] | title}} {%- else -%}
                      {{trigger.payload_json["after"]["label"] | title}} {%-
                      endif %} was detected on {{
                      trigger.payload_json["after"]["camera"] | replace("_", "
                      ") | title }}
                    message: >-
                      {% if (state_attr("sensor.double_take_front_doorbell_cam",
                      "id") == trigger.payload_json["after"]["id"]) and
                      (state_attr("sensor.double_take_front_doorbell_cam",
                      "matches") | length > 0) -%}
                      {{(state_attr("sensor.double_take_front_doorbell_cam",
                      "matches") | first)["name"] | title}} {%- else -%}
                      {{trigger.payload_json["after"]["label"] | title}} {%-
                      endif %} detected in the {{
                      trigger.payload_json["after"]["entered_zones"][0] }}
                    notifTag: '{{ trigger.payload_json["after"]["id"] }}'
                    notifIcon: mdi:doorbell-video
                    group: >-
                      frigate-notification-{{
                      trigger.payload_json["after"]["camera"] }}
                    importance: default
                    image: >-
                      /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
                    clickAction: >-
                     {{base}}/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/{{trigger.payload_json["after"]["camera"]}}/clip.mp4
    default: []
mode: parallel
max: 10
NickM-27 commented 2 years ago

You'll want to adjust the

- condition: template
   value_template: '{{ trigger.payload_json["before"]["entered_zones"]|length == 0 }}'

To be something that checks for driveway in current_zones and street in entered_zones

lee-costa commented 2 years ago

I see, thank you very much. I will give this a try.

lee-costa commented 2 years ago

So as I never been good with template, do you mind a little extra assistance? 😬 I am not too sure on what values to edit. As suggested above, I am simply trying to configure a notification for when a car enters the ‘street’ and ‘driveway’ zone I receive a notification on my iOS device.

NickM-27 commented 2 years ago

@junior466 Keep in mind this is decently customized to me, and I have my own script which sends notifications to my devices, but this should be a good starting point.

alias: Outside - Driveway & Front Yard - Frigate Event - Cat / Dog / Person Seen
description: ''
trigger:
  - platform: mqtt
    topic: frigate/events
    id: frigate-event
    payload: NEED TO UPDATE NAME
    value_template: '{{ value_json["after"]["camera"] }}'
condition:
  - condition: or
    conditions:
      - condition: template
        value_template: '{{ trigger.payload_json[''type''] == ''new'' }}'
      - condition: template
        value_template: '{{ trigger.payload_json["before"]["entered_zones"]|length == 0 }}'
  - condition: template
    value_template: '{{ ["street"]|select("in", trigger.payload_json["after"]["entered_zones"])|list|length > 0 and ["driveway"]|select("in", trigger.payload_json["after"]["current_zones"])|list|length > 0 }}'
  - condition: template
    value_template: >-
      {{ ["driveway", "front_yard"]|select("in",
      trigger.payload_json["after"]["entered_zones"])|list|length > 0 }}
  - condition: template
    value_template: '{{ trigger.payload_json["after"]["label"] != "car" }}'
action:
  - choose:
      - conditions:
          - condition: trigger
            id: frigate-event
        sequence:
          - service: script.send_dynamic_notification_to_all_devices
            data:
              id: '{{ trigger.payload_json["after"]["id"] }}'
              title: >-
                {{ trigger.payload_json["after"]["label"] | title }} was
                detected on {{ trigger.payload_json["after"]["camera"] |
                replace("_", " ") | title }}
              message: >-
                {{ trigger.payload_json["after"]["label"] | title }} detected in
                the {{ trigger.payload_json["after"]["entered_zones"][0] }}
              notifTag: '{{ trigger.payload_json["after"]["id"] }}'
              notifIcon: mdi:doorbell-video
              group: >-
                frigate-notification-{{ trigger.payload_json["after"]["camera"]
                }}
              importance: max
              image: >-
                /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
              clickAction: ''
          - 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: '{{ trigger.payload_json["after"]["id"] }}'
                    title: >-
                      {{trigger.payload_json["after"]["label"] | title}} was detected on {{
                      trigger.payload_json["after"]["camera"] | replace("_", "
                      ") | title }}
                    message: >-
                      {{trigger.payload_json["after"]["label"] | title}} detected in the {{
                      trigger.payload_json["after"]["entered_zones"][0] }}
                    notifTag: '{{ trigger.payload_json["after"]["id"] }}'
                    notifIcon: mdi:doorbell-video
                    group: >-
                      frigate-notification-{{
                      trigger.payload_json["after"]["camera"] }}
                    importance: default
                    image: >-
                      /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
                    clickAction: >-
                     BASE URL OF SERVER/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/{{trigger.payload_json["after"]["camera"]}}/clip.mp4
    default: []
mode: parallel
max: 10
NickM-27 commented 2 years ago

Okay I tried to get it close. You will need to adjust the camera name, base url of server, and then either setup a script like I have or change that action to notify the device(s) of your choice

lee-costa commented 2 years ago

@NickM-27 Wow sir, you are amazing! Thank you for your time. I will try this shortly!

NickM-27 commented 2 years ago

Let me know if you have any questions

lee-costa commented 2 years ago

So I tried editing it but don't think I was successful. I tested it by triggering the automation in home assistant but no notification is sent to my phone. I imagine the automation should at least send the notification part to my phone (with no image)? I am also seeing an error in Home Assistant logs:

Error fetching information from http://192.168.141.21:5000/api/stats: 502, message='Bad Gateway', url=URL('http://192.168.141.21:5000/api/stats')

Here's my edited automation. My zones are street for the actual street and driveway for the actual driveway.

- alias: Frigate - Driveway & Street
  description: ''
  trigger:
    - platform: mqtt
      topic: frigate/events
      id: frigate-event
      payload: driveway_cam
      value_template: '{{ value_json["after"]["camera"] }}'
  condition:
    - condition: or
      conditions:
        - condition: template
          value_template: '{{ trigger.payload_json[''type''] == ''new'' }}'
        - condition: template
          value_template: '{{ trigger.payload_json["before"]["entered_zones"]|length == 0 }}'
    - condition: template
      value_template: '{{ ["street"]|select("in", trigger.payload_json["after"]["entered_zones"])|list|length > 0 and ["driveway"]|select("in", trigger.payload_json["after"]["current_zones"])|list|length > 0 }}'
    - condition: template
      value_template: >-
        {{ ["driveway", "street"]|select("in",
        trigger.payload_json["after"]["entered_zones"])|list|length > 0 }}
    - condition: template
      value_template: '{{ trigger.payload_json["after"]["label"] != "car" }}'
  action:
    - choose:
        - conditions:
            - condition: trigger
              id: frigate-event
          sequence:
            - service: notify.mobile_app_USER_iphone
              data:
                id: '{{ trigger.payload_json["after"]["id"] }}'
                title: >-
                  {{ trigger.payload_json["after"]["label"] | title }} was
                  detected on {{ trigger.payload_json["after"]["camera"] |
                  replace("_", " ") | title }}
                message: >-
                  {{ trigger.payload_json["after"]["label"] | title }} detected in
                  the {{ trigger.payload_json["after"]["entered_zones"][0] }}
                notifTag: '{{ trigger.payload_json["after"]["id"] }}'
                notifIcon: mdi:doorbell-video
                group: >-
                  frigate-notification-{{ trigger.payload_json["after"]["camera"]
                  }}
                importance: max
                image: >-
                  /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
                clickAction: ''
            - 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: notify.mobile_app_USER_iphone
                    data:
                      id: '{{ trigger.payload_json["after"]["id"] }}'
                      title: >-
                        {{trigger.payload_json["after"]["label"] | title}} was detected on {{
                        trigger.payload_json["after"]["camera"] | replace("_", "
                        ") | title }}
                      message: >-
                        {{trigger.payload_json["after"]["label"] | title}} detected in the {{
                        trigger.payload_json["after"]["entered_zones"][0] }}
                      notifTag: '{{ trigger.payload_json["after"]["id"] }}'
                      notifIcon: mdi:doorbell-video
                      group: >-
                        frigate-notification-{{
                        trigger.payload_json["after"]["camera"] }}
                      importance: default
                      image: >-
                        /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
                      clickAction: >-
                       http://192.168.141.21:5000/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/{{trigger.payload_json["after"]["camera"]}}/clip.mp4
      default: []
  mode: parallel
  max: 10
NickM-27 commented 2 years ago

Oh my bad, you will also want to remove this condition:

- condition: template
  value_template: '{{ trigger.payload_json["after"]["label"] != "car" }}'

If you suspect something should be triggered you can use the "debug automation" option in HomeAssistant to see the messages that are coming in and which condition it got stopped on.

lee-costa commented 2 years ago

Yea, something is definitely wrong. The logs show no errors and the automation debug looks like this: Screen Shot 2022-02-28 at 7 14 10 PM

Two questions: Do I need to change id: frigate-event to something else? Is the base url I needed to change the URL of Frigate? Just making sure I am not missing anything obvious. I am trying to get to a point where the automation works and at least sends me an alert. Every time I trigger it, nothing pops up on my device.

NickM-27 commented 2 years ago

Can you click on "Changed Variables" and what that shows? Should be able to quickly see what happened here

NickM-27 commented 2 years ago

frigate-event is the TriggerID which is just a home assistant thing and doesn't affect the MQTT stuff / wouldn't block this from running.

And the base URL is the base URL of frigate

lee-costa commented 2 years ago

I see:

  context:
    id: cbe9813f59e337fa69128626121d84bd
    parent_id: 265f9b146deb3fc7f03dca7849511a61
    user_id: null
NickM-27 commented 2 years ago

Sorry, click on the yellow icon with arrows going to the left and right (choose) and then check changed_variables

lee-costa commented 2 years ago

Screen Shot 2022-02-28 at 7 26 06 PM

NickM-27 commented 2 years ago

Just curious, how are you triggering this automation? Are you running manually? This will only work if you post an MQTT messages as just pressing "run actions" will not do anything

lee-costa commented 2 years ago

So that's the issue then. I had a feeling it was something very simple being overlooked on my part. So the best way to test it is to actually physically trigger it?

NickM-27 commented 2 years ago

Uhhh well, to test it "end to end" yes. However HomeAssistant does offer the ability to manually trigger MQTT messages so I would start with that just to make sure the notification and other stuff works. One sec I should be able to cook up what you are looking for.

NickM-27 commented 2 years ago

You will want to use the services section per the screenshot and enter this YAML:

service: mqtt.publish
data:
  topic: frigate/events
  payload: >-
    {"before": {"id": "INSERT VALID EVENT ID", "camera": "INSERT CAMERA NAME",
    "frame_time": 1646094818.516091, "snapshot_time": 1646093963.926109,
    "label": "car", "top_score": 0.78125, "false_positive": false, "start_time":
    1646093962.13158, "end_time": null, "score": 0.71484375, "box": [0, 499,
    101, 598], "area": 9999, "region": [0, 364, 320, 684], "stationary": true,
    "motionless_count": 4271, "position_changes": 1, "current_zones": [],
    "entered_zones": [], "has_clip": false, "has_snapshot": false}, "after":
    {"id": "INSERT VALID EVENT ID", "camera": "INSERT CAMERA NAME",
    "frame_time": 1646094878.639409, "snapshot_time": 1646093963.926109,
    "label": "car", "top_score": 0.78125, "false_positive": false, "start_time":
    1646093962.13158, "end_time": null, "score": 0.71484375, "box": [0, 499,
    101, 598], "area": 9999, "region": [0, 364, 320, 684], "stationary": true,
    "motionless_count": 4572, "position_changes": 1, "current_zones": ["driveway"],
    "entered_zones": ["street"], "has_clip": false, "has_snapshot": false}, "type":
    "update"}

You will need to change the "id" in both before and after as well as the camera to the name you are expecting.

Screen Shot 2022-02-28 at 5 35 31 PM

lee-costa commented 2 years ago

Is the INSERT VALID EVENT ID the id frigate-event on the automation?

NickM-27 commented 2 years ago

That would be the ID of the frigate event. For testing purposes you can use any one you want it is just used for the snapshot

lee-costa commented 2 years ago

I received this:

Error while executing automation automation.automation_19: extra keys not allowed @ data['id']
NickM-27 commented 2 years ago

Okay so that is due to the notification part. You edited the service but you are still using my data args. I wrote a custom script so those aren't valid for a standard notify call. (BUT HEY, that means it almost worked!)

You will need to change the data part of that notify call to:

data:
  title: >-
    {{trigger.payload_json["after"]["label"] | title}} was detected on {{
    trigger.payload_json["after"]["camera"] | replace("_", "
    ") | title }}
  message: >-
    {{trigger.payload_json["after"]["label"] | title}} detected in the {{
    trigger.payload_json["after"]["entered_zones"][0] }}
  data:
    url: >-
      http://192.168.141.21:5000/api/{{trigger.payload_json["after"]["id"]}}/{{trigger.payload_json["after"]["camera"]}}/clip.mp4
    tag: '{{ trigger.payload_json["after"]["id"] }}'
    group: frigate-notification-{{ trigger.payload_json["after"]["camera"] }}
    image: >-
      /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
    attachment:
      url: >-
        /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
lee-costa commented 2 years ago

I know, we are close! Do I remove everything below the notify call - service: notify.mobile_app_USER_iphone and add the new code?

  action:
    - choose:
        - conditions:
            - condition: trigger
              id: frigate-event
          sequence:
            - service: notify.mobile_app_USER_iphone
              data:
                id: '{{ trigger.payload_json["after"]["id"] }}'
                title: >-
                  {{ trigger.payload_json["after"]["label"] | title }} was
                  detected on {{ trigger.payload_json["after"]["camera"] |
                  replace("_", " ") | title }}
                message: >-
                  {{ trigger.payload_json["after"]["label"] | title }} detected in
                  the {{ trigger.payload_json["after"]["entered_zones"][0] }}
                notifTag: '{{ trigger.payload_json["after"]["id"] }}'
                notifIcon: mdi:doorbell-video
                group: >-
                  frigate-notification-{{ trigger.payload_json["after"]["camera"]
                  }}
                importance: max
                image: >-
                  /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
                clickAction: ''
            - 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: notify.mobile_app_USER_iphone
                    data:
                      id: '{{ trigger.payload_json["after"]["id"] }}'
                      title: >-
                        {{trigger.payload_json["after"]["label"] | title}} was detected on {{
                        trigger.payload_json["after"]["camera"] | replace("_", "
                        ") | title }}
                      message: >-
                        {{trigger.payload_json["after"]["label"] | title}} detected in the {{
                        trigger.payload_json["after"]["entered_zones"][0] }}
                      notifTag: '{{ trigger.payload_json["after"]["id"] }}'
                      notifIcon: mdi:doorbell-video
                      group: >-
                        frigate-notification-{{
                        trigger.payload_json["after"]["camera"] }}
                      importance: default
                      image: >-
                        /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
                      clickAction: >-
                       http://192.168.141.21:5000/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/{{trigger.payload_json["after"]["camera"]}}/clip.mp4
      default: []
  mode: parallel
  max: 10
NickM-27 commented 2 years ago

yes, you will want to do on both notify actions

lee-costa commented 2 years ago

Okay, so in the end, I ended up with this and the notification comes through! 👍

  - id: '164037269411544'
    alias: Frigate - Driveway & Street
    description: ''
    trigger:
      - platform: mqtt
        topic: frigate/events
        id: frigate-event
        payload: driveway_cam
        value_template: '{{ value_json["after"]["camera"] }}'
    condition:
      - condition: or
        conditions:
          - condition: template
            value_template: '{{ trigger.payload_json[''type''] == ''new'' }}'
          - condition: template
            value_template: '{{ trigger.payload_json["before"]["entered_zones"]|length == 0 }}'
      - condition: template
        value_template: '{{ ["street"]|select("in", trigger.payload_json["after"]["entered_zones"])|list|length > 0 and ["driveway"]|select("in", trigger.payload_json["after"]["current_zones"])|list|length > 0 }}'
      - condition: template
        value_template: >-
          {{ ["driveway", "street"]|select("in",
          trigger.payload_json["after"]["entered_zones"])|list|length > 0 }}
      # - condition: template
      #   value_template: '{{ trigger.payload_json["after"]["label"] != "car" }}'
    action:
      - choose:
          - conditions:
              - condition: trigger
                id: frigate-event
            sequence:
              - service: notify.mobile_app_USER_iphone
                data:
                  title: >-
                    {{trigger.payload_json["after"]["label"] | title}} was detected on {{
                    trigger.payload_json["after"]["camera"] | replace("_", "
                    ") | title }}
                  message: >-
                    {{trigger.payload_json["after"]["label"] | title}} detected in the {{
                    trigger.payload_json["after"]["entered_zones"][0] }}
                  data:
                    url: >-
                      http://192.168.141.21:5000/api/{{trigger.payload_json["after"]["id"]}}/{{trigger.payload_json["after"]["camera"]}}/clip.mp4
                    tag: '{{ trigger.payload_json["after"]["id"] }}'
                    group: frigate-notification-{{ trigger.payload_json["after"]["camera"] }}
                    image: >-
                      /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
                    attachment:
                      url: >-
                        /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
              - 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: notify.mobile_app_USER_iphone
                      data:
                        title: >-
                          {{trigger.payload_json["after"]["label"] | title}} was detected on {{
                          trigger.payload_json["after"]["camera"] | replace("_", "
                          ") | title }}
                        message: >-
                          {{trigger.payload_json["after"]["label"] | title}} detected in the {{
                          trigger.payload_json["after"]["entered_zones"][0] }}
                        data:
                          url: >-
                            http://192.168.141.21:5000/api/{{trigger.payload_json["after"]["id"]}}/{{trigger.payload_json["after"]["camera"]}}/clip.mp4
                          tag: '{{ trigger.payload_json["after"]["id"] }}'
                          group: frigate-notification-{{ trigger.payload_json["after"]["camera"] }}
                          image: >-
                            /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
                          attachment:
                            url: >-
                              /api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
        default: []
    mode: parallel
    max: 10