[UniFi Protect | openHAB 3] Adding UniFi Protect to OpenHAB Part 2

This is Part 2 of adding UniFi Protect to OpenHAB. Although the previous article was sufficient to integrate UniFi Protect devices into the openHAB local automation platform, unlike the other platforms like Home Assistant or HomeSeer, openHAB UniFi Protect binding add-on itself does not include viewing of video streaming contents. For this, we need to use different binding.

Prerequisite 

  • Functioning OpenHAB OS Installation (recommend OpenHABian)

What do you need?

  • UniFi Protect device & controller
  • openHAB Hub

In this article, you will learn:

Camera Stream Setup

openHAB is a highly modular system. This design may be introduce a point of confusion at various stage of setup for newer users. Basically, UniFi Protect binding discussed on the Part I are only good for integrating functional part of the camera but not video streaming. For this purpose, we need another binding called IPCamera.

With the modular design of the system, it is not hard to group two binding entities into a single virtual equipment, and still create recreate the Unifi Doorbell virtual entity.

In a nutshell, we want to use IPCamera binding as a generic camera that takes UniFi’s RTSP video stream and optionally anonymous snap shot image (snap.jpeg) and convert these into MJPEG and HLS streaming format. This binding needs FFmpeg, which is an open source audio/video stream conversion tool, which is not installed by default even with openHABian setup so you need to install this.

FFmpeg installation

Go to your OpenHab command line input either via SSH or directly on the console if you are using ProxMox like I do. The installation steps are described here.

In the main command line input, you simply enter the command below:

sudo apt update && sudo apt install ffmpeg

If you are accessing the command line input as a root account, you do not need to type in sudo command. If you are not logged in as root and get an error message saying sudo command is not installed or your user account is not sudo user, you can read here for instruction of how to add sudo command tool.

You can check if this has successfully installed the ffmpeg by typing in

ffmpeg

Enable Anonymous Snapshot on UniFi Protect Camera

Whatever the reason, this seems not optional at the moment. If you do not enable anonymous snapshot on the UniFi protect camera, IPCamera won’t be able to connect to them.

You must repeat these process for each device.

Enter individual UniFi Protect Device IP address to the browser e.g. 192.168.1.X

Default user ID and password for accessing these are “ubnt” and “ubntprotect”.

Enable anonymous snapshot

Make sure to hit SAVE CHANGES.

Gather RTSP link from UniFi Protect Controller

How this process works is UniFi Protect camera has RTSP streaming output. RTSP streaming protocol is not compatible for displaying in the browsers; therefore, IPCamera binding takes RTSP as input and using FFMPEG to convert the RTSP stream into the browser compatible format like MJPEG, HLS, JPEG snapshots.

Go to UniFi Protect Controller Menu > Devices
Click individual camera device > Manage > Expand RTSP

You want to copy a link from the list and keep for the later use. Pick Unsecure link as I was not able to get secure link work personally. Depending on the quality of image you need and processing power of the server, pick the quality at your own choice. I personally picked Medium Unsecure and the performance is near instant when working.

Install IPCamera Binding

Settings > Add-ons | Bindings > Hit + at right lower corner

Here you want to enter “ipcamera.”

Create UniFi Protect Camera Thing

Settings > Configuration | Things > Hit + at right lower corner

Here you want to select IpCamera Binding.

On the above menu, you want to select RTSP/HTTP IP Camera, which is generic camera.

The sections you need to fill in are:

Label: Put the name of the thing. I called UniFi Protect Doorbell part, just Doorbell. So here I call Doorbell camera but you can name anyway you like and this is changeable later.

IP address: This is IP address of the UniFi Protect Camera itself and not the Protect controller.

Snapshot URL: You can leave this as default i.e. ffmpeg. If you do, FFMPEG will create snapshot from the RTSP stream. Because UniFi protect camera supports anonymous snapshot, I prefer using the direct URL to off-load the conversion. You can enter http://[camera IP address]/snap.jpeg

Server Port: IPCamera’s output will use this port # with openHAB server IP address. For example, if openHAB is running on 192.168.1.100, you enter Port # here as 1000, and you want to look mjpeg output then it will be accessible at http://192.168.1.100:1000/ipcamera.mjpeg. For this reason, port # must be unique for the openHAB server. I personally prefer using source UniFi Protect device last four digits of the IP address part e.g. 1100.

Surprisingly, you do not need to enter Username nor Password, which I believe because we use anonymous snapshot and unsecure RTSP connection.

FFmpeg Input: This is the input for this THING to work. This is where you enter RTSP:// IP address that you wrote down earlier.

If all is well, after hitting Save you will see Status turn into Online.

Create Items

Following the previous Create Items steps described in Part I, you can creates Items for the IPCamera binding Thing.

Personally, I felt this is where I saw a little potential of modular design. Instead of having two separate Equipments and multiple point items for each, I thought it would make the most sense to have a single Equipment for Unifi Doorbell that contains all the related points including the Camera stream point items from IPCamera Binding. All I did was after creating Doorbell Camera Equipment and corresponding items, I just changed each item’s “direct parent groups” to the Doorbell, which was the equipment created during UniFi Protect binding.

Items Menu

Then I deleted Doorbell Camera Equipment Item. This is not the same as deleting Doorbell camera Thing, which will delete the the real link between UniFi camera stream to OpenHAB.

Doorbell Camera Equipment is nothing but Group item i.e. it is a place holder to group multiple items. In fact, if you delete Equipment before migrating its point items, the point items will still remain, just no longer having parent.

As far as which channels to use for creating viewable streaming, you need:

  • HLS URL
  • MJPEG URL

HLS is a real video streaming with audio. The downside is it has some time lag. MJPEG is a more real time but it is a serial jpeg snap shot images so no audio and frame rate is seen as ultra low.

Adding Stream Video to UI

The modular design of openHAB applies to UI as well. There are a few different ways to setup user interface. The more choices to users can be great but also can be a source of confusion to new users like myself.

For the starter, it would have made the most sense to me if it was presented as two primary UI options: Main UI and Sitemaps with latter being mobile interface/legacy UI.

From a novice point of view, I see sitemaps as a legacy UI while Main UI is more modern, newer user interface. Unfortunately, neither is perfect at the moment. The main challenge I have encountered is incorporating video stream from the protect devices either locally or remotely.

Locally, the best option is main UI, which supports live stream with sounds when HLS format is used. However, remotely even if you use openHAB’s remote access service, you won’t be able to see it. Because IPCamera Binding creates converted stream outputs at local IP port level. So I believe one could potentially setup port forwarding rule on the network firewall, but that would be a bit of work and may not the most secure route.

So the recommended method is to create VPN connection and use main UI. If you have UniFi network, you can create it on the UniFi network controller with relative ease. Once it is configured, there will be an extra-step each time you go out of your local network as you need to turn on VPN, but once you are connected to the VPN, you are essentially on the local network; therefore you will be able to see the full functionality of the main UI including the video streaming.

On my personal test, this was very responsive to the point it is almost local instant speed though this would vary significantly with your home network as well as cellular network.

So let’s take a look how you would exactly add streaming tile to main UI.

Adding HLS Streaming Card

Settings > Pages

Click Overview, which is a default main UI.

Add Row > Add Column

*It is important that openHAB cad options will change depending on the size of cell.

Now click +

Select Video Card for HLS streaming.

Now click right upper corner icon and choose configure Widget.

Click Video Item

Select HLS URL Point from the Doorbell Camera or other UniFi Protect Camera device that you created using IP Binding.

Now you should see video streaming.

Adding MJPEG Streaming

Essentially the same as above except this time you want to use image card.

For Image section select Item, and choose MJPEG URL from IP Binder based Item’s point.

Viewing on OpenHAB

For some reason I totally missed main UI access on the openHAB first. This page can be accessed by simply clicking left upper corner openHAB icon.

The page at the bottom have four tabs.

Locations are used when you create Semantic Model and organize Items by Locations. If you haven’t assigned your Protect device(s) to specific location, you can go to Equipment Tab. If you have selected subsets of Equipment e.g. Doorbell, you may see Doorbell tab but if have’t done so i.e. left Equipment as just Equipment, everything are under Miscellaneous tab.