Custom Component: Unifi Protect

Can you share this piece of code? Seems nice!

Hello, Iā€™m toying over the idea of unifi protect, and found this component - all looking very promising!!

My main question is surrounding using the motion detectionā€¦ Can I still use the unifi app/ui to define my motion boundaries and exclusion zones? Then once Iā€™ve done this, this component will show me motion scores based on those zones? ā€¦or are we only able to look at full camera frame motion with this component? (I did read through all above, but couldnā€™t spot an answer!)

taaa

Thatā€™s certainly how Iā€™m using it, although Iā€™m still fine-tuning my usage of scoring which is tricky given the area one of my cameras covered.

If Iā€™ve understood the component correctly, it is pulling the score assigned by Protect to each motion event. Logically, this should be influenced by the sensitivity settings in Protect itself.

Hello Briis!

UDM Pro is now also available in EU. I just received mine and what Iā€™m missing is how to specify the port for the udm-api-proxy in your configuration.
Is there any option Iā€™m missing in how to specify that instead of Port 7443 it should then access a different port?
Is there anything I can help you with in creating the direct communicaiton with UDM Pro?
Thanks
Albert

Edit: I just tried to specify the port - this is my config and it works via the proxy

unifiprotect:
  host: 192.168.1.232
  port: 32770
  username: username
  password: secretpassword
  image_width: 640
  minimum_score: 0
1 Like

Iā€™m having the following error when trying to start this component:

Error during setup of component unifiprotect
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/setup.py", line 174, in _async_setup_component
    component.setup, hass, processed_config  # type: ignore
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/unifiprotect/__init__.py", line 111, in setup
    host, port, username, password, use_ssl, minimum_score
  File "/config/custom_components/unifiprotect/unifi_protect_server.py", line 44, in __init__
    self._api_auth_bearer_token = self._get_api_auth_bearer_token()
  File "/config/custom_components/unifiprotect/unifi_protect_server.py", line 74, in _get_api_auth_bearer_token
    raise NvrError("Request failed: %s" % response.status)
AttributeError: 'Response' object has no attribute 'status'

also I have the udm pro, i dont know if this is related

EDIT: I see that there is a unifi-udm-api-proxy but I have no idea how to use docker-compose, can anyone give me some more detailed instructions on how to install the proxy?

Thank you!

Hi adandl,

Do you think you could help me out with setting up the proxy? I also have a udm pro and I canā€™t seem to get this thing to work.
Hereā€™s my discord Stephen#9031

1 Like
Unable to prepare setup for platform unifiprotect.camera: Unable to set up component.

I have this error after installing this component from hacs, I added the following to my configuration.yaml:

unifiprotect:
  host: 192.168.2.11
  username: Myusername
  password: Mypassword
  minimum_score: 50
  
camera:
  - platform: unifiprotect

Edit:

Unable to connect to NVR: HTTPSConnectionPool(host='192.168.2.11', port=443)

Seems the error is related to the port Iā€™m choosing, I guess I dont understand which port I should be using for the proxy

Hi
Yes this error is shown when the component cannot communicate with the CloudKey. I assume you are running on a CloudKey Gen2? If not you need to use the Proxy setup as described here.
Based on what you show above, and the way you set it up, you did not supply the port name, and the default port used then is 7443 and not 443.
Can you give a bit more info on the HW you are running on?

Iā€™m running a udm pro. I started using port 5000 which I thought is what the proxy needs? I still get the error so I donā€™t think Iā€™m correctly setup yet

As stated earlier I cannot support the UDM Pro, but if you look at an earlier post in this thread @adandl states that if he uses port: 32770 then it works for him with the Proxy. But you have to install the Proxy I linked to in my previous reply, to get it working.

Iā€™m doing

docker run -d -p 5000:5000 -v /var/run/docker.sock:/var/run/docker.sock silvenga/unifi-udm-api-proxy

since that is what the proxy seems to be looking for?
This is what is written on the plugin:

unifiprotect <-> unifi-udm-api-proxy (192.168.0.2:5000) <-> protect api (192.168.0.1:443)

solved it by using

docker run -d -p 5000:443 -v /var/run/docker.sock:/var/run/docker.sock silvenga/unifi-udm-api-proxy
1 Like

Hi this integration is working perfectly, thanks so much for your work on it.

I tried to setup a lovelace view, with a grid of horizontal/verticals to show it in panel mode. My goal was to stretch the thumbnails to full size of the screen.

It works, but the video is choppy AF. If you click a camera, it works fine in the popup, click it back and you get an updated frame every 2-3 seconds.

Any ideas what would be causing the extra delay?

Any other way to get a full-size grid of 4 cameras?

What kind of Lovelace entity do you use: Picture, Picture Entity or Picture Glance?

There also this Custom Plugin, which maybe might be useful to you: https://github.com/custom-cards/surveillance-card You can find in HACS under Plugins (Surveillance Card)

I use picture glance cards.

Basically I can put 4 glance cards on the screen, all streams perfectly.

If I copy those same 4 cards into vertical/horizontal grid cards, they get all choppy. Same code otherwise, it just displays bigger (panel mode).

:warning:Motion Detection broken with V1.13.3-beta.1 of Unifi Protect for Cloudkey
I just upgraded my Cloudkey to the above version, and since then motion detection is not triggered. There are some changes in the output from the event log with this version, and one of them is that the motion score is not updated before the event has ended. So checking against the motion score when motion is triggered, now has the effect that motion is never triggered, as the score is 0 until the motion event has ended.

I will release a quick-fix, where you can set the motion_score to 0 (Or not setting it at all), thus avoiding the check, and then look for a more permanent solution.

As a side note, it I found some interesting data in the event API, that could indicate that Ubiquiti is working on some kind of AI, as the below is in the raw output from the event log. But currently it is always null. Iā€™ll keep an eye on that.

  "metadata": {
   "objectType": null,
   "objectCoords": null,
   "objectConfidence": null
  },

Thanks for an excellent plugin.

Is there a URL that doesnā€™t use the RTSP link that we can send to our phones that will open UniFi Protect and view the current stream from a specific camera? I already have HA sending me a message with a screen shot when it detects movement, but I would also like to include a link to click on in that message so I can quickly view the current stream? Thanks

1 Like

AFAIK there isnā€™t a way to view the camera stream externally via HA.

What I did was setup two hidden tabs / views set to panel in lovelace (1 per camera), eg:

 - badges: []
    cards:
      - aspect_ratio: 50%
        camera_image: camera.driveway
        camera_view: live
        entity: camera.driveway
        hold_action:
          action: none
        show_name: false
        show_state: false
        tap_action:
          action: none
        type: picture-entity
    icon: 'mdi:road-variant'
    panel: true
    path: camera_driveway
    title: Driveway
    visible: []

And then use the mobile app notification actions to send link to it:

- id: unifiprotect_driveway_daytime_motion_notification
  alias: "Driveway Daytime Motion Detected"
  trigger:
    platform: state
    entity_id: binary_sensor.unifiprotect_motion_driveway
    from: "off"
    to: "on"
    for:
      seconds: 6
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.notify_driveway
        state: "on"
      - condition: state
        entity_id: input_boolean.holiday_mode
        state: "off"
      - condition: state
        entity_id: input_boolean.notify_via_sms
        state: "off"
      - condition: template
        value_template: "{{ ((is_state('sun.sun', 'above_horizon') and (state_attr('binary_sensor.unifiprotect_motion_driveway', 'motion_score') | int >= 50))) }}"
  action:
    - service: notify.all_mobile
      data_template:
        title: "Driveway Alert"
        message: "Motion detected at {{ now().strftime('%H:%M') }}. Day Score: {{ state_attr('binary_sensor.unifiprotect_motion_driveway', 'motion_score') }}"
        data:
          image: "/api/camera_proxy/camera.driveway"
          actions:
            - action: "URI"
              title: "View Live Feed"
              uri: "/lovelace/camera_driveway" 

This then opens the HomeAssistant Companion (Android) app to show a live feed from the relevant camera.

Eventually Iā€™ll update these to use the ā€œclickActionā€ instead, however that doesnā€™t yet open to the correct tab (itā€™s in beta I believe).

2 Likes

Ahh good idea, I will give that a go! Never thought to use a URI. Thanks

What I actually meant was not a view within HA, but rather a link to the actual UniFi protect app which is also installed on my phone. I thought you could reference applications using a URL type address? I use a iPhone BTW.

Iā€™m not sure that the Android app has reached parity with the iPhone app yet so that may be an option for you.
Tbh I find the stream loads more quickly than the protect app in this scenario