Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frigate reports incorrect occupancy in Home Assistant after restart #662

Open
soonerfan237 opened this issue Apr 13, 2024 · 35 comments
Open
Labels
bug Something isn't working pinned

Comments

@soonerfan237
Copy link

Version of the custom_component

5.1.0

Configuration

garage_camera:
    ffmpeg:
      inputs:
        - path: rtsp://127.0.0.1:8554/garage_camera
          input_args: preset-rtsp-restream
          roles:
            - record
        - path: rtsp://127.0.0.1:8554/garage_camera_sub
          input_args: preset-rtsp-restream
          roles:
            - detect
            - audio
      output_args:
        record: preset-record-generic-audio-aac
    live:
      stream_name: garage_camera_sub
    detect:
      width: 896
      height: 512
      fps: 10
    motion:
      mask:
        - 680,0,680,35,192,35,192,0
    zones:
      garage_zone:
        coordinates: 0,512,896,512,896,0,369,0,379,103,85,279
        filters:
          car:
            min_score: 0.7
            min_area: 50000
      driveway_zone:
        coordinates: 362,104,363,0,236,0,22,88,71,273
    record:
      enabled: True
      retain:
        days: 7
        mode: all 
      events:
        retain:
          default: 30
          mode: all
    snapshots:
      enabled: True
      retain:
        default: 14
    audio:
      enabled:
        true
      listen:
        - engine_starting
        - bark
        - speech
    birdseye:
      order: 4
    ui:
      order: 4
    objects:
      track:
        - person
        - dog
        - car
      filters:
        person:
          min_score: 0.8
        dog:
          min_score: 0.75
        car:
          min_score: 0.76

Describe the bug

The Frigate add on is not reporting the correct occupancy state after Home Assistant is restarted while an object is being tracked. For example, if Frigate is detecting a car in my garage, and then I restart Home Assistant, the occupancy sensor will become clear even though Frigate is still actively detecting the car in the garage. Here's a video showing the situation.

I think this is related to this issue:
#376

Debug log

N/A

@NickM-27
Copy link
Collaborator

This is because the state is not retained which is not incorrect because otherwise you'd have the opposite problem - an object would show occupancy even if frigate wasn't running

@soonerfan237
Copy link
Author

soonerfan237 commented Apr 13, 2024

which is not incorrect

Sorry, maybe I'm not understanding. But Frigate is detecting an occupancy, the Add On says Clear. That is not the correct state.

because otherwise you'd have the opposite problem - an object would show occupancy even if frigate wasn't running

That problem already exists in the current implementation. When frigate isn't running it is reporting Clear by default even if it's not actually clear. It's the opposite side of the coin, but still essentially the same issue. If Frigate isn't running, then I would expect the state should be Unavailable.

I won't pretend to know what the solution is, but it is definitely reporting a state that is not the real, current state after a restart.

When Home Assistant restarts, could Frigate re-send the current states to update the occupancy entities?

@NickM-27
Copy link
Collaborator

To be clear I am saying retaining is not a solution because it would cause the other problem.

When Home Assistant restarts, could Frigate re-send the current states to update the occupancy entities?

that would require frigate to be aware of home assistant and home assistant to communicate back to frigate which I am not sure is desirable.

One solution might be to have an api in frigate that would send the current state for all objects. Then home assistant could call this api on startup to get its initial state.

@soonerfan237
Copy link
Author

Yeah, that makes sense, I see what you mean now.

I just implemented a workaround with an automation that triggers when Home Assistant starts. I have it publish an MQTT message to the frigate/restart topic. That works to newly detect all the objects and update the occupancy entities, but it takes extra time waiting for Frigate to start up.

Not sure if there's an MQTT topic to request re-publishing of current states? If so, maybe that could automatically be published as part of the normal HACS Frigate Add On startup process so it stays in sync?

And if there was a way to get the entities to show a state of Unavailable when Frigate isn't running, I think that would be ideal. I noticed the camera entities and sound levels already show Unavailable when Frigate is stopped. Maybe that could be extended to the occupancy entities, etc.?

@soonerfan237
Copy link
Author

@NickM-27 I've been thinking about this some more, and am curious how all my other integrations maintain states after a restart.

For example I have lots of sensors through zigbee2mqtt. When I restart home assistant my door sensors don't all default to reporting that they're open. My zwave locks don't default to reporting as unlocked. My Hue bulbs don't all default to showing an off state.

Do other integrations retain messages? Or do they re-poll for states after restart? Are there plans for Frigate to do the same?

@NickM-27 NickM-27 added bug Something isn't working pinned labels Apr 13, 2024
@NickM-27
Copy link
Collaborator

NickM-27 commented Apr 13, 2024

Looks like zigbee2mqtt retains all their messages. Like I said I don't think that is a suitable solution for the way frigate works with mqtt. Object detection data is a lot more transient than the attributes of a zigbee device

@soonerfan237
Copy link
Author

I see what you mean. Although, I will say that I am using object detection for things that are not transient, so it depends on the use case.

Either way, I think occupancy sensors reporting accurate states after a restart would be a best practice. Is there a fix for this on the roadmap?

@NickM-27
Copy link
Collaborator

There are a number of things that make the object count and occupancy sensors sub optimal currently. It will likely be rewritten and made better at some point, we are currently rewriting the frigate UI among many new features so I wouldn't expect this to be improved in 0.14

@soonerfan237
Copy link
Author

Understood. And thanks for being responsive and for all the work you do on this project - it's great stuff. I would definitely put my vote in for this to be moved up the priority list following the 0.14 release. These sensors get used in automations for many users and would love to see them fixed.

@soonerfan237
Copy link
Author

There are a number of things that make the object count and occupancy sensors sub optimal currently. It will likely be rewritten and made better at some point, we are currently rewriting the frigate UI among many new features so I wouldn't expect this to be improved in 0.14

@NickM-27 Thanks for an awesome 0.14 release! I wanted to check back on this issue. Is improving the way Frigate reports occupancy sensors so the state is accurate following a restart something that will be prioritized for the next major release?

@lucasteligioridis
Copy link

@soonerfan237 hey mate I'm definitely seeing this issue as well, thanks for raising it.

I have it publish an MQTT message to the frigate/restart topic

Mind sharing the automation/workaround for this? Would be good for myself/future travellers.

@soonerfan237
Copy link
Author

soonerfan237 commented Aug 11, 2024

@lucasteligioridis I changed the way I restart the add-on since I made that MQTT comment. Here's my current method. The automation triggers every time that Home Assistant starts. The action is "Home Assistant Supervisor: Restart add-on" with the Frigate add-on selected.

alias: Restart Frigate after Home Assistant starts
description: >-
  Frigate occupancy sensors get messed up whenever Home Assistant is restarted.
  The fix is to restart Frigate.
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - metadata: {}
    data:
      addon: ccab4aaf_frigate
    enabled: true
    action: hassio.addon_restart
mode: single

If you do want to use the MQTT method, here's the action section of the automation.

action:
  - data:
      qos: 0
      retain: false
      topic: frigate/restart
    enabled: false
    action: mqtt.publish

@lucasteligioridis
Copy link

Thanks a heap @soonerfan237

@Nooton92
Copy link

Not sure if I can add this onto here or if I should open my own ticket but ever since 0.14 HA is falsely detecting occupancy in one of my zones while frigate does not have any events at the time stamps.

Only at night it seems. We have our front doorbell camera set to go through our do not disturb if someone is at our front door...so 3+ false positives from HA with no video clips or recorded events is odd.

I reviewed the footage at the timestamps in frigate. Looks like most of the timestamps was a car driving by on the road(it has been raining here so had some weird reflections. What I find odd is frigate did not even see this as a motion or detection event so I am not sure why HA thought is was worthy of an alert.

@soonerfan237
Copy link
Author

@Nooton92 A couple days ago I got a notification that a person was detected in my house while I was away. My dog sometimes gets falsely detected as a person. So after I got home I went to send the false positive to Frigate+. But I never saw a detection/motion event in Frigate during that time.

At the time, I figured it was just a random occurence, but now seeing your comment, there may be something more to it. Not sure how to reproduce it though.

It could be that event thresholds set in yaml aren't being used the same in every part of the UI? If it happens again, I'll try to do some more digging.

@NickM-27
Copy link
Collaborator

The occupancy sensors have less verification in order to be able to respond faster. This is working as expected.

The /events or /reviews topic should be used for notifications

@soonerfan237
Copy link
Author

@NickM-27 Interesting, didn't know that! Will switch to the /events topic going forward.

Was this a recent change to occupancy sensors or has it always been like this? Can you expand the "less verification" comment? Is it bypassing threshold checks or something?

@NickM-27
Copy link
Collaborator

No, this is how it has worked for quite a while

@soonerfan237
Copy link
Author

Thanks! Is there documentation out there for what causes the occupancy sensors to be turned on? Or can you help me with an explanation?

And follow up question - is the occupancy sensor behavior configurable?

@55ldb
Copy link

55ldb commented Aug 19, 2024

Good morning. I have also noticed that with the latest version of Frigate 0.14 and the version 5.3.0 of the integration, sometimes the "Occupancy Sensor" is activated but there is no recorded snapshot nor clip. With version 0.13 I had not experienced anything similar. Maybe it's just coincidence (?) but I see that other users report the same problem above. Inside the yard where the cameras are operating there is a big dog and sometimes Frigate recognizes him as a "person". But I don't have a snapshot to see exactly what has happened.

@NickM-27
Copy link
Collaborator

There are a number of things that are different:

  1. frigate events require that the object has moved at some point after it was detected. The occupancy sensor does not.
  2. Frigate events required that either the snapshot or the recordings config be met (0.13) or the review config be met (0.14) to show in the UI, but this is not the case for the occupancy sensor

@Belox86
Copy link

Belox86 commented Aug 20, 2024

Hi everyone, going back to the original problem, i have a similar one.
If i stop the frigate add-on from HA while is detecting one or more persons, when i start frigate again, the person count and occupancy sensors in HA are not cleared and retain the previous values.
On the other hand, the "all count" and "all occupancy" are cleared.
I'm using Frigate 0.14.0.
Frigate is stopped by a script as follow (and started in the same way):

service: hassio.addon_stop
data:
  addon: ccab4aaf_frigate-fa-beta

In the attached picture an example of how the HA integration is showing frigate sensors of a clean scene.
Particularly Frigate was stopped while detecting 2 persons, and started it afterwards when the scene was empty.
Note the "all" sensors cleared, while "person" sensor with incorrect retained value .
fc2ba4f1-d85c-4659-aa46-2a544283d7ef

@soonerfan237
Copy link
Author

@NickM-27 following up on this topic.

The occupancy sensors have less verification in order to be able to respond faster. This is working as expected.
The /events or /reviews topic should be used for notifications

I have switched my automations to use the /events topic, but I'm still getting notifications even when there are no detections seen in the Frigate UI. And I can confirm there was not actually a person within view of the camera.

Here is my automation yaml. The trigger is an mqtt message on frigate/events. Then I have conditions to specify that it is a new detection. The object is a person. And the score is >=0.8.

Am I making any errors here? My guess is that it's something to do with the minimum detection score, but I can't see what I'm missing.

alias: House - Notify when back porch camera detects person while alarm is armed
description: ""
trigger:
  - platform: mqtt
    topic: frigate/events
    id: frigate-event
    payload: back_porch_camera
    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 }}"
      after_attributes: "{{ trigger.payload_json['after']['current_attributes'] }}"
    alias: Back Porch Camera MQTT message received
condition:
  - condition: or
    conditions:
      - condition: template
        value_template: "{{ trigger.payload_json['type'] == 'new' }}"
      - condition: template
        value_template: "{{ trigger.payload_json['type'] == 'update' }}"
      - condition: template
        value_template: "{{ before_zones | length == 0 }}"
    alias: New detection
    enabled: true
  - condition: template
    value_template: "{{ trigger.payload_json[\"after\"][\"label\"] == \"person\" }}"
    alias: Object is a person
  - alias: Min score for detection
    condition: template
    value_template: "{{ trigger.payload_json[\"after\"][\"score\"] >= 0.8 }}"
action:
  - data:
      filename: /config/www/back_porch_camera_snapshot.jpg
    target:
      entity_id:
        - camera.back_porch
    action: camera.snapshot
  - data:
      message: Person detected
      title: Back Porch Camera
      data:
        push:
          sound:
            name: default
            critical: 1
            volume: 1
        image: >-
         REDACTED/back_porch_camera_snapshot.jpg
        actions:
          - action: URI
            title: View Cameras
            url: /lovelace/home
    action: notify.notify
mode: single

@NickM-27
Copy link
Collaborator

we need to see the mqtt payload that triggered it to know why, but if I had to guess the object never moved which is a requirement for a snapshot, clip, review item to be saved

@soonerfan237
Copy link
Author

I don't have the mqtt message saved. But my guess is that a shadow or something is getting detected and so its probably not moving.

Is there an additional condition I can add to my automation to prevent it from triggering?

@NickM-27
Copy link
Collaborator

you can check position_changes > 0

@soonerfan237
Copy link
Author

Thanks! I'll give that a shot.

@gvillo
Copy link

gvillo commented Sep 24, 2024

I came here looking for a bug and I think I found it 😅 Definitely there is something going on, today this issue happened to me (twice in 2 minutes, 7.13pm and 7.15pm), I got occupancy triggered in HA but nothing in Frigate's Review tab, this triggered the siren, alarming the neighborhood :P. There was no restart at all from any device.

Running latest version of everything (HA and Frigate v0.14), I didn't run into this issue before (or if I got a falsy detection I was always able to see it on Frigate). I couldn't collect any MQTT log, I'll see if using /events I could fix it, wondering what position_changes > 0 does btw!

Thanks!

@gvillo
Copy link

gvillo commented Sep 25, 2024

Wondering if there is a quick way to add a binary_sensor like person occupancy based on /events or /reviews for a specific zone (because I need a binary_sensor entity for Alarmo).

@soonerfan237
Copy link
Author

I just wanted to jump back in this thread to say that I haven't had any more false triggers since adding the position_changes>0 condition to my automation. Although I agree with @gvillo that a simple binary_sensor that follows /events or /reviews would be the cleanest solution in the future.

Here's my current automation yaml in case anyone wants to implement it on their side.

alias: House - Notify when back porch camera detects person
description: ""
trigger:
  - platform: mqtt
    topic: frigate/events
    id: frigate-event
    payload: back_porch_camera
    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'] }}"
      position_changes: "{{ trigger.payload_json['after']['position_changes'] }}"
      time_clip_start: "{{ trigger.payload_json['after']['start_time'] - 10.0 }}"
      after_attributes: "{{ trigger.payload_json['after']['current_attributes'] }}"
    alias: Back Porch Camera MQTT message received
condition:
  - alias: Prevent multiple, consecutive notifications
    condition: template
    value_template: >-
      {{ (now().timestamp() -
      state_attr('automation.house_notify_when_back_porch_camera_detects_person',
      'last_triggered').timestamp()) / 60 > 5 }}
    enabled: true
  - condition: or
    conditions:
      - condition: template
        value_template: "{{ trigger.payload_json['type'] == 'new' }}"
      - condition: template
        value_template: "{{ trigger.payload_json['type'] == 'update' }}"
      - condition: template
        value_template: "{{ before_zones | length == 0 }}"
    alias: New detection
    enabled: true
  - condition: template
    value_template: "{{ trigger.payload_json[\"after\"][\"label\"] == \"person\" }}"
    alias: Object is a person
  - alias: Min score for detection
    condition: template
    value_template: "{{ trigger.payload_json[\"after\"][\"score\"] >= 0.8 }}"
  - alias: Objected has moved
    condition: template
    value_template: "{{ trigger.payload_json[\"after\"][\"position_changes\"] > 0 }}"
action:
  - data:
      filename: /config/www/back_porch_camera_snapshot.jpg
    target:
      entity_id:
        - camera.back_porch
    action: camera.snapshot
  - data:
      message: Person detected
      title: Back Porch Camera
      data:
        push:
          sound:
            name: default
            critical: 1
            volume: 1
        image: >-
          REDACTED/back_porch_camera_snapshot.jpg
        actions:
          - action: URI
            title: View Cameras
            url: /lovelace/home
    action: notify.notify
mode: single

@55ldb
Copy link

55ldb commented Sep 25, 2024

I would agree 100% with that: "a simple binary_sensor that follows /events or /reviews would be the cleanest solution in the future".

In my case I take a different approach. I don't use the "position_changes>0" option but I look into the MQTT messages to see if Frigate has created a snapshot. So far it seems to be working οκ, but if anyone knows better I would like their opinion on the automation I am using:

  • id: '.....'
    alias: ALARM Front Garden Person Detected - MQTT
    description: ''
    trigger:

    • platform: mqtt
      topic: frigate/events
      id: frigate-event
      payload: front-garden
      value_template: '{{ value_json[''after''][''camera''] }}'
      variables:
      camera: '{{ trigger.payload_json[''after''][''camera''] }}'
      id: '{{ trigger.payload_json[''after''][''id''] }}'
      label: '{{ trigger.payload_json[''after''][''label''] }}'
      has_snapshot: '{{ trigger.payload_json[''after''][''has_snapshot''] }}'

    condition:

    • condition: or
      conditions:
      • condition: and
        conditions:
        • condition: device
          device_id: .....
          domain: alarm_control_panel
          entity_id: .....
          type: is_armed_home
        • condition: template
          value_template: '{{ trigger.payload_json[''after''][''has_snapshot''] == true }}'
      • condition: and
        conditions:
        • condition: device
          device_id: ....
          domain: alarm_control_panel
          entity_id: ....
          type: is_triggered
        • condition: template
          value_template: '{{ trigger.payload_json[''after''][''has_snapshot''] == true }}'

    action:

    • device_id: ....
      domain: alarm_control_panel
      entity_id: ....
      type: trigger
    • action: media_player.volume_set
      metadata: {}
      data:
      volume_level: 0.8
      target:
      device_id: ....
      enabled: true
    • action: tts.speak
      metadata: {}
      data:
      cache: true
      media_player_entity_id: media_player.vlc_telnet
      language: el
      message: ....
      target:
      entity_id: tts.google_en_com
      enabled: true
      mode: single

@miketyson666
Copy link

miketyson666 commented Oct 21, 2024

@Belox86 > Hi everyone, going back to the original problem, i have a similar one. If i stop the frigate add-on from HA while is detecting one or more persons, when i start frigate again, the person count and occupancy sensors in HA are not cleared and retain the previous values. On the other hand, the "all count" and "all occupancy" are cleared. I'm using Frigate 0.14.0. Frigate is stopped by a script as follow (and started in the same way):

I have exactly the same issue, anybody found a solution?

@gvillo
Copy link

gvillo commented Oct 21, 2024

I was trying to create my custom binary sensor but I couldn't make it work ok, hmmm wondering if Frigate can expose them based on mqtt events

@heifisch
Copy link

heifisch commented Nov 4, 2024

I once built a sensor as a template. In a quick test it worked.

template:
  - trigger:
      platform: mqtt
      topic: frigate/events
      payload: garage
      value_template: '{{ value_json[''after''][''camera''] }}'
      variables:
        label: '{{ trigger.payload_json[''after''][''label''] }}'
        position_changes: '{{ trigger.payload_json[''after''][''position_changes''] }}'
        has_snapshot: '{{ trigger.payload_json[''after''][''has_snapshot''] }}'
        type: '{{ trigger.payload_json[''type''] }}'
    condition:
      - '{{ label == ''person'' }}'
      - '{{ position_changes > 0 }}'
      - '{{ has_snapshot == true }}'
    binary_sensor:
      - name: Kamera Garage Person erkannt
        unique_id: binary_sensor.kamera_garage_person_erkannt
        device_class: motion
        icon: mdi:motion-sensor
        auto_off: 300
        state: >
          {% if type != 'end' %}
            true
          {% else %}
            false
          {% endif %}

@bagobones
Copy link

Following because I just realized why my occupancy sensor was wrong so often as every restart of HA from a config change is clearing all of these.

These object occupancy and object count sensors are useful but very unreliable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pinned
Projects
None yet
Development

No branches or pull requests

10 participants