SgtBatten / HA_blueprints

Somewhere to store automation blueprints
151 stars 55 forks source link

[Issue]: Zone based notifications not triggering properly #185

Closed dfoley4 closed 3 months ago

dfoley4 commented 3 months ago

Description

I have zone filters enabled for my garage camera but instead of triggering for my desired zones, it filters out all notifications. Any ideas on how to get it to function properly? I want to get notifications if there's a car in the driveway but not for one in the road but I don't want to filter out all road cars from recordings.

Version

0.12.0.3

Automation Config

alias: Frigate Garage
description: Sends notifications for garage camera
use_blueprint:
  path: SgtBatten/Beta.yaml
  input:
    camera: camera.garage
    notify_device: 88b5e8850e40720e1cfedaf7ed857df3
    notify_group: home_group
    base_url: https://afhktdl2p1w8nlejnytcxnwgau3iob9r.ui.nabu.casa/
    title: Garage Notification
    message: >-
      A {{ label }} {{ 'is loitering' if loitering else 'was detected' }} on the
      {{ camera_name }} camera{% if enteredzones %} in the {{ enteredzones |
      join(', ') | replace('_',' ') }}{% endif %}.
    attachment: snapshot.jpg
    update_thumbnail: true
    video: >-
      {{base_url}}/api/frigate{{client_id}}/notifications/{{id}}/{{camera}}/clip.mp4
    color: "#000000"
    ios_live_view: camera.garage
    zone_filter: true
    zones:
      - driveway
      - front_yard_left
      - front_yard_right
    labels:
      - car
      - dog
      - person
      - cat
    cooldown: 0
    button_3: Open Home Assistant
    url_3: /lovelace
    tv: true
    tv_position: top-right
    tv_duration: 15

Frigate Config

garage:
    ui:
      order: 1
    ffmpeg:
      inputs:
        - path: rtsp://XXXXX/cam/realmonitor?channel=1&subtype=0
          roles:
            - record
        - path: rtsp://XXXXX/cam/realmonitor?channel=1&subtype=1
          roles:
            - detect
    objects:
      filters:
        car:
          mask:
            - 0,332,0,487,994,446,1920,464,1920,294
        person:
          mask:
            - 0,445,1920,399,1920,0,0,0
            - 253,512,235,512,236,472,261,472
            - 1138,514,1175,515,1169,438,1129,440
    zones:  
      driveway:
        coordinates: 1086,549,891,1080,0,1080,0,891,437,754,797,554
        objects:
          - suitcase
          - person
          - dog
          - cat
          - car

Any other relevant information

No response

SgtBatten commented 3 months ago

Enable debug and review the trace please. Guide here; https://github.com/SgtBatten/HA_blueprints/blob/main/Frigate%20Camera%20Notifications/Guide%3A%20Debug%20Option.md

djak250 commented 3 months ago

I think I've found the issue location. Here Here's a relevant debug log:

Frigate Notification () DEBUG (in loop): Info: Last Zones: ['garage', 'driveway-near'], Current zones: ['garage', 'driveway-near', 'driveway-far'], sublabel: None, iOS sound: yes due critical notifications, Android Sound: disabled by alert once, Triggers: New Snapshot: False, Presence Changed: False, stationary moved: False, entered zones changed: False, sublabel changed: False, Conditions: Loitering: 0 or Presence Entity not home: OFF - PASS, zone filter TEST: ON - PASS, multi-zone filter: ON - PASS, state filter TEST: OFF - PASS image: "/api/frigate/notifications/1710810480.311976-l43p5t/thumbnail.jpg"

Note that even though Last Zones has fewer items than Current zones, entered zones changed is still reporting false. I messed around in the template engine to see what the issue was, and I think it's just some bad syntax on the conditionals. Below gave me the results I'd expect:

{% set zones = ['driveway-near', 'driveway-far'] %}
{% set last_zones = ['garage', 'driveway-near'] %}
{% set entered_zones = ['garage', 'driveway-near', 'driveway-far'] %}
{% set filtered_last_zones = zones|select('in', last_zones)|list %}
{% set filtered_entered_zones = zones|select('in', entered_zones)|list %}
{{ zones|length > 0 and (filtered_entered_zones|length > 0 and filtered_entered_zones|length != filtered_last_zones|length) }}

Essentially, I don't think and not is behaving the way you're expecting it to. I'm happy to fork and see if my solution fixes the issue in practice.

SgtBatten commented 3 months ago

Are you using the stable or beta, your automation suggests beta but you are looking at the stable code. A lot has been worked on in the beta, especially with fine tuning the debug outputs for zones. Note that the debug outputs can simply be wrong as many of them are not the exact tests of the conditions themselves due to changes over time.

Please confirm exactly what you are using.

dfoley4 commented 3 months ago

Morning,

You are correct, this automation is running on the beta release. I'll post a trace this afternoon.

Of note, I updated my zones on frigate by removing and replacing the coordinates and now a few notifications have gone through with this automation. Not sure what might've been the problem but I'll update here if I can figure out what was going on.

Thanks!

SgtBatten commented 3 months ago

I've done pretty heavy testing with zones on the beta because I know the stable isn't perfect. I thought I had it nailed down pretty well.

Regarding your pull request. I only do edits to he beta. Then merge them over to the stable manually once satisfied. I've got a hell of a lot of changes to bring over when I get time

SgtBatten commented 3 months ago

Essentially, I don't think and not is behaving the way you're expecting it to. I'm happy to fork and see if my solution fixes the issue in practice.

Hi @djak250

I didn't realise you were not the original issue opener.

Please see here in the beta this is already improved similar to what you have discovered in stable i believe

entered_zones_changed: "{{ zones|length > 0 and (zones|select('in', enteredzones)|list|length > zones|select('in', last_zones)|list|length) }}"

djak250 commented 3 months ago

Sorry. Yeah it's working in the beta. You can ignore my pr.

dfoley4 commented 3 months ago

I'm going to go ahead and close this issue, looks like everything is working as advertised in the beta. Thanks @SgtBatten !