home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.31k stars 30.62k forks source link

Client error (404): Event thumbnail not found, UnifiProtect Smart Sensors #124860

Closed Rockhound53 closed 1 month ago

Rockhound53 commented 2 months ago

The problem

Since a recent update, I have had the problem that no thumbneils are created for Smart Detections (Person, Animal, Car, etc.) in my Unifi Protect integration.

The following error message appears in the logs when the automation is triggered:

Client error (404): Event thumbnail not found

A thumbnail is created with the Motion Decetion Sensor.

I hope someone can help me so that the automation works again with the smart sensor.

What version of Home Assistant Core has the issue?

2024.8.3

What was the last working version of Home Assistant Core?

2024.7

What type of installation are you running?

Home Assistant OS

Integration causing the issue

UniFi Protect

Link to integration documentation on our website

https://www.home-assistant.io/integrations/unifiprotect

Diagnostics information

Logger: homeassistant.components.unifiprotect.views Quelle: components/unifiprotect/views.py:64 Integration: UniFi Protect (Dokumentation, Probleme) Erstmals aufgetreten: 12:13:14 (52 Vorkommnisse) Zuletzt protokolliert: 15:57:26

Client error (404): Event thumbnail not found

Example YAML snippet

mode: single
max_exceeded: silent
variables:
  input_obj:
    - binary_sensor.garten_g4_pro_person_detected
    - binary_sensor.garten_g4_pro_animal_detected
  input_channel: Garten
  input_lovelace_view: /lovelace-mobile/garten-cam
  input_notify_target_app: notify.mobile_app_pixel_8_pro
  color: green
  icon: mdi:cctv
  input_silence_timer: 60
  input_time_format: '%H:%M'
  input_presence_filter:
    - device_tracker.pixel_8_pro
  input_alarm_entity: ''
  input_alarm_arm_states: []
  input_debug_event_id: ''
  input_cooldown: 120
  lovelace_view: '{{ input_lovelace_view | trim }}'
  is_manual: '{{ ''from_state'' not in trigger }}'
  entity_id: >-
    {% if is_manual %}{{ input_obj[0] }}{% else %}{{ trigger.entity_id }}{%
    endif %}
  entity_name: '{{ state_attr(entity_id, ''friendly_name'') }}'
  device_id: '{{ device_id(entity_id) }}'
  camera_entities: >-
    [{% for eid in device_entities(device_id) %}{%if eid.startswith('camera')
    and not 'package' in eid and not is_state(eid, 'unavailable') %}"{{ eid
    }}",{% endif %}{% endfor %}]
  camera_entity_id: '{{ camera_entities | default([None]) | first }}'
  event_id: >-
    {% if is_manual %}{{ input_debug_event_id }}{% else %}{{
    state_attr(entity_id, 'event_id') }}{% endif %}
  video_start: '{{ states[entity_id].last_changed.isoformat() }}'
  trigger_time: |
    {% if states[entity_id] == None %}
      None
    {% else %}
      {{ as_local(states[entity_id].last_changed).strftime(input_time_format) }}
    {% endif %}
  notification_channel: |
    {% if is_manual %}
      Manual {{ input_channel }}
    {% else %}
      {{ input_channel }}
    {% endif %}
  notification_title: '{{ entity_name }}'
  notification_tag: '{{ notification_channel.lower().replace('' '', ''-'') }}'
  notification_message: >-
    {{ entity_name }}{% if trigger_time != None %} at {{ trigger_time }}{% endif
    %}.
  notification_image: /api/unifiprotect/thumbnail/{{ config_entry_id(entity_id) }}/{{ event_id }}
  notification_url: |
    {% if lovelace_view == "" %}
      None
    {% else %}
      {{ lovelace_view }}
    {% endif %}
  silence_action: silence-smart-{{ entity_id }}
trigger:
  - platform: state
    entity_id:
      - binary_sensor.garten_g4_pro_person_detected
      - binary_sensor.garten_g4_pro_animal_detected
    from: 'off'
    to: 'on'
condition:
  condition: and
  conditions:
    - condition: or
      conditions:
        - condition: template
          value_template: '{{ input_alarm_entity is not defined or input_alarm_entity == '''' }}'
        - condition: and
          conditions:
            - condition: template
              value_template: '{{ input_alarm_entity is defined and input_alarm_entity != '''' }}'
            - condition: template
              value_template: >-
                {{ input_alarm_arm_states is defined and input_alarm_arm_states
                | length > 0 }}
            - condition: template
              value_template: '{{ states(input_alarm_entity) in input_alarm_arm_states }}'
    - condition: or
      conditions:
        - condition: template
          value_template: >-
            {{ input_presence_filter is not defined or input_presence_filter ==
            '' }}
        - condition: template
          value_template: >-
            {{ (input_presence_filter | select('is_state', 'home') | list |
            count) == 0 }}
action:
  - data:
      message: '{{ notification_message }}'
      title: '{{ notification_title }}'
      data:
        tag: '{{ notification_tag }}'
        channel: '{{ notification_channel }}'
        color: '{{color}}'
        notification_icon: '{{icon}}'
        ttl: 0
        priority: high
        alert_once: |
          {% if tag != "" %}
            true
          {% else %}
            false
          {% endif %}
        time-sensitive: 1
        image: '{{ notification_image }}'
        entity_id: '{{ camera_entity_id }}'
        actions: >
          [{% if notification_url != None %} { "action": "URI", "title": "Open
          Camera", "uri": "{{ notification_url }}" }, {% endif %} {% if
          input_silence_timer > 0 %} { "action": "{{ silence_action }}",
          "title": "Silence", "destructive": True }, {% endif %}]
    action: '{{ input_notify_target_app }}'
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: '{{ silence_action }}'
      - platform: event
        event_type: mobile_app_notification_cleared
        event_data:
          message: '{{ notification_message }}'
      - platform: state
        entity_id:
          - binary_sensor.garten_g4_pro_person_detected
          - binary_sensor.garten_g4_pro_animal_detected
        to: 'off'
    timeout:
      seconds: '{{ input_cooldown }}'
    continue_on_timeout: false
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ wait.trigger.platform == ''state'' }}'
        sequence:
          - data:
              message: '{{ notification_message }}'
              title: '{{ notification_title }}'
              data:
                tag: '{{ notification_tag }}'
                channel: '{{ notification_channel }}'
                color: '{{color}}'
                notification_icon: '{{icon}}'
                ttl: 0
                priority: high
                alert_once: |
                  {% if tag != "" %}
                    true
                  {% else %}
                    false
                  {% endif %}
                time-sensitive: 1
                image: '{{ notification_image }}'
                video: >-
                  /api/unifiprotect/video/{{ config_entry_id(entity_id) }}/{{
                  entity_id }}/{{ video_start }}/{{
                  states[entity_id].last_changed.isoformat() }}
                actions: >
                  [{% if notification_url != None %} { "action": "URI", "title":
                  "Open Camera", "uri": "{{ notification_url }}" }, {% endif %}
                  {% if input_silence_timer > 0 %} { "action": "{{
                  silence_action }}", "title": "Silence", "destructive": True },
                  {% endif %}]
            action: '{{ input_notify_target_app }}'
          - wait_for_trigger:
              - platform: event
                event_type: mobile_app_notification_action
                event_data:
                  action: '{{ silence_action }}'
            timeout:
              seconds: >-
                {{ max([input_cooldown - (states[entity_id].last_changed -
                as_datetime(video_start)).total_seconds(), 5]) }}
            continue_on_timeout: false
      - conditions:
          - condition: template
            value_template: >-
              {{ wait.trigger.platform == 'event' and
              wait.trigger.event.event_type == 'mobile_app_notification_action'
              }}
        sequence:
          - delay:
              minutes: '{{ input_silence_timer }}'
      - conditions:
          - condition: template
            value_template: >-
              {{ wait.trigger.platform == 'event' and
              wait.trigger.event.event_type == 'mobile_app_notification_cleared'
              }}
        sequence:
          - delay:
              seconds: >-
                {{ max([input_cooldown - (wait.trigger.event.time_fired -
                as_datetime(video_start)).total_seconds(), 5]) }}
id: '1720791232631'
alias: 📷✉️ Unifi Garten (Tag)
description: ''

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 2 months ago

unifiprotect documentation unifiprotect source

mib1185 commented 2 months ago

Hi @Rockhound53

At the moment this sounds more like a request for support, but not a concrete code issue? You should try our Community Forum or join our Discord chat server. If you could narrow down your automation to the bare minimum and providing the log file, so that reproducing (or at least understanding) the issue might be possible, than we could handle it as an issue again.

Thanks! đź‘Ť

chasut commented 2 months ago

I am seeing something similar. I don't use the blueprint, but get the thumbnail in a similar manner and also getting this error.

This is on the (now official) Unifi Protect 4.1.53 and HA 2024.8.3. I suspect protect and its new "alarm manager" has changed behavior enough to mess up my previously amazing setup.

I think what is happening is that the binary sensor flashes on and off again too quick to get the event_id that is needed to find the image. This is also causing two other problems for me. 1) it is too fast to trigger some template binary_sensors that I also use to manage things. I was able to get them to work using explicit state triggers, but now it 2) doesn't work until AFTER the event (person detection in my case) is over. Not ideal.

mea culpa for jumping too quickly on the protect update. Hopefully this gets sorted quickly and my punishment is not too severe ;)

Rockhound53 commented 2 months ago

Ich sehe etwas Ähnliches. Ich verwende die Blaupause nicht, erhalte aber das Miniaturbild auf ähnliche Weise und erhalte ebenfalls diesen Fehler.

Dies ist auf dem (jetzt offiziellen) Unifi Protect 4.1.53 und HA 2024.8.3. Ich vermute, dass Protect und sein neuer „Alarmmanager“ das Verhalten so stark geändert haben, dass mein zuvor fantastisches Setup durcheinander geraten ist.

Ich denke, was passiert, ist, dass der Binärsensor zu schnell an- und ausgeht, um die Ereignis-ID zu erhalten, die zum Finden des Bildes benötigt wird. Das verursacht bei mir auch zwei weitere Probleme. 1) Er ist zu schnell, um einige Vorlagen für Binärsensoren auszulösen, die ich auch zur Verwaltung von Dingen verwende. Ich konnte sie mithilfe expliziter Statusauslöser zum Laufen bringen, aber jetzt funktioniert er 2) erst, NACHDEM das Ereignis (in meinem Fall die Personenerkennung) vorbei ist. Nicht ideal.

mea culpa, weil ich das Protect-Update zu schnell durchgeführt habe. Hoffentlich wird das schnell geklärt und meine Strafe ist nicht zu streng ;)

Hello,

Your assumptions sound logical, the ringing of the doorbell no longer seems to create a thumbnail.

Where do you turn if there is a problem with integration?

Version 4.1.53 of Unifi Protect is now officially released, a rollback to 4.0.33 is difficult because the cameras and chimes have been updated and require a version greater than 4.1.

Best regards

darkrain-nl commented 2 months ago

There is now support for Events in the integration, since Home Assistant 2024.8. So to get the Event ID I now had to use: event_id: "{{ state_attr('event.g4_doorbell_pro_doorbell', 'event_id') }}"

I use this in an automation I put together myself and have this line in the variables: section in the trigger.

Not sure how to change the blueprint...

But I don't think it has to do with the Unifi Protect upgrade (will test shortly).

edit: Yep, have tested after updating to 4.1.53 and I still get the thumbnail with this method.

chasut commented 2 months ago

I was able to get the snapshot to work by using the event as you posted. I don't understand much about the event structure but best I can tell is that particular event is for the doorbell press. is that correct? In my search so far, I don't see anything similar for "person detected" event type/class (my use case)

Also, nothing appears to happen until AFTER the event is over. I have tested this in just about every way I can think of (automation, trigger, state change). Previously, the person_detected binary sensor would turn on at the start of the event and turn off once that event ended. Now it appears to toggle briefly once the event ends. I have seen the "event_id" appear in the attributes of the binary sensor as before, but just doesn't stick long enough to capture.

I am playing around with the new webhooks available in the new alarm manager. Maybe I can use those to get things to fire at the START instead of at the END

Any ideas?

darkrain-nl commented 2 months ago

Hmm yes the event is for the doorbell ring, so in that sense it makes sense it doesn't fire for person detected...

Not sure how that needs to be done now... I just figured out how to use the event for my use case, I also don't know much about how events work in Home Assistant...

chasut commented 2 months ago

Some good news.

I noticed that the "object_detected" continues to work as the other specific sensors used to work. It turns on at the start of the event and off once it is over. It also contains the "event_id". So retrieving the snapshot/thumbnail and video work again.

So after a bit of re-wiring, I am back in business! Only downside is that it doesn't differentiate as to what object was detected. It seems to be just any of the "people, cars, animals, etc." that are selected in smart detection in Protect.

Rockhound53 commented 2 months ago

Hello everyone,

I've now set it to "object detection". Then at least you don't get a trigger every time a cloud passes by.

Can we expect help from the developers of the Unifi Protect integration on this?

Best regards

Lunkobelix commented 1 month ago

There is now support for Events in the integration, since Home Assistant 2024.8. So to get the Event ID I now had to use: event_id: "{{ state_attr('event.g4_doorbell_pro_doorbell', 'event_id') }}"

I use this in an automation I put together myself and have this line in the variables: section in the trigger.

Not sure how to change the blueprint...

But I don't think it has to do with the Unifi Protect upgrade (will test shortly).

edit: Yep, have tested after updating to 4.1.53 and I still get the thumbnail with this method.

Thanks for sharing. I'm also not getting any thumbnail since a week/weeks.

How would I change your code to make it work witha G4 camera, do you know? "event." doesn't list any cameras.

darkrain-nl commented 1 month ago

There is now support for Events in the integration, since Home Assistant 2024.8. So to get the Event ID I now had to use: event_id: "{{ state_attr('event.g4_doorbell_pro_doorbell', 'event_id') }}" I use this in an automation I put together myself and have this line in the variables: section in the trigger. Not sure how to change the blueprint... But I don't think it has to do with the Unifi Protect upgrade (will test shortly). edit: Yep, have tested after updating to 4.1.53 and I still get the thumbnail with this method.

Thanks for sharing. I'm also not getting any thumbnail since a week/weeks.

How would I change your code to make it work witha G4 camera, do you know? "event." doesn't list any cameras.

The event is only for the doorbell, not for camera's that are not doorbells as far as I know...

chasut commented 1 month ago

How would I change your code to make it work witha G4 camera, do you know? "event." doesn't list any cameras.

Pretty straightforward, at least conceptually. What you need to do is change the trigger from the specific sensor (e.g. binary_sensor.g4_camera_person_detected) to the more generic, but still working (binary_sensor.g4_object_detected). This includes both for the initial trigger, and for capturing the event_id, (e.g. for my doorbell, it is "{{ state_attr('binary_sensor.g4_doorbell_pro_object_detected', 'event_id') }}" )

The problem is not really with the event_id or thumbnail itself, it is with the individual smart detection binary_sensors in the integration (see the issue on this topic). Unifi really made a mess with their latest changes. I have no doubt it will get fixed, eventually. Till then, this patch is working great.

I made this change for all my cameras and everything works great, including the thumbnails. Only issue is that the "object_detected" sensor trips on ANY smart detection you have enabled for the camera (animals, cars, etc.). So you may also want to adjust that as well, in protect.

Lunkobelix commented 1 month ago

Hi Thanks a million for explaining this. So if I understand it right, this is how it should be? Or should also the first "config_entry" be changed? This should then notify me on smart motions (persons, cars, animals)?

data: image: >- /api/unifiprotect/thumbnail/{{ config_entry_id('binary_sensor.altanen_object_detected') }}/{{ state_attr('binary_sensor.altanen_object_detected', 'event_id') }}

Seems to work but I also had to change the automation trigger to binary_sensor.altanen_object_detected, is that correct?

chasut commented 1 month ago

Yes, the "config_entry" needs to be changed to the "_object_detected" as that is where the event_id is in the attributes.

Yes, the automation trigger must be on the "_object_detected" entity as that is the only one that still works correctly.

Lunkobelix commented 1 month ago

Yes, the "config_entry" needs to be changed to the "_object_detected" as that is where the event_id is in the attributes.

Yes, the automation trigger must be on the "_object_detected" entity as that is the only one that still works correctly.

Thank you for your help, highly appreciated! Got it to work but about 20-50% of the times I still don't get an image. Here is an example, pretty strange. Do you have the same?

image

chasut commented 1 month ago

No, I don't have those issues, the thumbnail works every time. Are you using the blueprint?

issue-triage-workflows[bot] commented 1 month ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment đź‘Ť This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

ervwalter commented 1 month ago

FYI for others running into this problem, my solution was to stop using state_attr() which appears to have a timing issue with automations (e.g. the event_id is not there anymore by the time that function gets run). I now get the event_id directly from the trigger object since it has the new attributes directly in the object.

variables:
  camera_name: "{{ device_attr(trigger.entity_id, 'name') }}"
  nvr_id: "{{ config_entry_id(trigger.entity_id) }}"
  event_id: "{{ trigger.to_state.attributes.event_id }}"
  image_path: /api/unifiprotect/thumbnail/{{ nvr_id }}/{{ event_id }}