Unifi: protect camera setup

Posted on
Thu Mar 11, 2021 10:49 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Unifi: protect camera setup

This requires mac os 10.14 or higher

Code: Select all
starting in V 7.38.346  @ 2021-03-11
================== 
1. added support for unifi protect cameras
   - get cameras from unifi systems and create indigo devices
   - the plugin will listen to events, create dev states for start time / end time/ time length of event
     get thumbnails jpegs and or heatmap jpegs
        enable / disable / configure thumbnails in camera device edit
     events are also setting variables:
        Unifi_Camera_Event_Date  date string of last event; eg "2021-03-11 22:33:26"
        Unifi_Camera_with_Event  device name of camera of last event; eg "Camera_Reserve-UVC G3  Flex"
        Unifi_Camera_Event_pathtoThumbnail  path to last event thumbnail; eg "/Users/karlwachs/indigo/protect/Camera_Reserve-UVC G3  Flex_snapshot.jpeg"

   - get snap shots through menu or actions at any time, with selectable resolution
   - set parameters of cameras for IR-LED, status-LED, speaker, mic, motion trigger etc in menu and actions
   - added debug option for protect in config
   - the dev/state "smartDetect" is blank or "vehicle" or "person" if the smart detect option is enabled to detect objects for events

 ==> to enable: set camera to protect in config (just above debug)
                and set repeat frequencies ...
     all camears connected / adopted by protect will be created in the folder unifi_created.
==> how to use / setup:
Code: Select all
    1. enable protect in config (bottom, above debug section)
       select "protect"
       set repeat query for device state checks and event tracking
       sel path for thumbnails
       save
       then the cameras will automatically be created in device folder unifi_created
    2. Then trigger on any change camera xx dev/states["eventStart"] ==> new movement at camera xx
       or on variable Unifi_Camera_Event_Date
       the path to the thumbnail is in dev/state "eventJpeg" and in variable "Unifi_Camera_Event_pathtoThumbnail"
       eg: "/Users/youruserid/Camera_living window G3 Flex_snapshot.jpeg"
       the thumbnail are only availabe at the end of teh event (dev/states["eventEnd"] is changed to !=-1)
    3. Get snapshots at anytime: setup action with in unifi action "CAMERAS get Snapshot" then execute  at your pleasure
       or use menu/cameras/snaphot for testing or onetime pictures
   4. a door bell event is indicated as eventType ="ring" instead of "motion" in dev / states

The door bell needs a special chime if used directly with the door bell (same wire)
Both devices are in series = the same currents runs through both. The doorbell has a resistance R of ~ 150 Ohm. So at 24 v the current is ~ 180mA
When pressing the button the cable to the doorbell gets shorted = R=0 and it does not get any power. It likely lives of power stored in a super capacitor ( >1Farad). That that is used up after ~ 12 seconds.
To work with the unifi door bell you need a special chime. One that stops ringing on its own in less that 12 seconds (when the door bell is still up) and shortly interrupts the power to the door bell. Then the door bell comes back to live. The special chimes are mentioned in the unifi docs.

It also has as stand alone mode where the button does not short the cable, and a direct in line chime will not work

Setup protect in config:
Screen Shot 2021-03-11 at 22.40.47.png
Screen Shot 2021-03-11 at 22.40.47.png (24.52 KiB) Viewed 10541 times

The following states will be created for each camera :
Screen Shot 2021-03-15 at 16.45.59.png
Screen Shot 2021-03-15 at 16.45.59.png (89.24 KiB) Viewed 10269 times

menu options:
Screen Shot 2021-03-11 at 22.42.36.png
Screen Shot 2021-03-11 at 22.42.36.png (114.13 KiB) Viewed 10541 times

thumbnails jpegs:
Screen Shot 2021-03-15 at 16.41.30.png
Screen Shot 2021-03-15 at 16.41.30.png (123.44 KiB) Viewed 10269 times

variables:
Screen Shot 2021-03-15 at 16.43.45.png
Screen Shot 2021-03-15 at 16.43.45.png (100.86 KiB) Viewed 10269 times

reporting:
Code: Select all
   uniFiAP                         Protect Camera devices      START =============================================================================================================================
                                                                                   ThumbNail     HeatMap       Device        Events----------------------------------- is   Volume- ir-LED----------------- stat
DevName---------------------- MAC#------------- ip#----------- DevType--- FWV----- On-resolutn   On-resolutn   lastSeen----- last-motion-- lastRing----- ---#of Mode   dark mic spk En  Sens Mode       Lvl LED
Camera_back UVC G3            F0:9F:C2:C1:BA:95 192.168.1.64   G3         4.34.22  ena-640/480   ena-320/240   3-14 11:13:35 3-14 19:58:00                    8 motion Day  100 100 ena low  a-Filt-Onl   0 ena
Camera_Doorbell               E0:63:DA:EF:EB:7B 192.168.1.79   G4 Doorbell4.34.22  ena-640/480   dis-320/240   3-15 16:39:30 3-15 16:39:31 3-15 14:31:25    640 motion Day  100  80 dis low  auto       255 ena
Camera_Family room G3 Flex    FC:EC:DA:1F:12:96 192.168.1.70   G3 Flex    4.34.22  ena-640/480   ena-320/240   3-14 11:13:35 3-13 04:33:12                   51 motion Nite 100 100 dis low  auto       255 dis
Camera_Front top UVC G4 Pro   B4:FB:E4:05:B9:E9 192.168.1.74   G4 Pro     4.34.22  ena-1024/768  ena-320/240   3-14 11:13:35 3-15 16:39:29                 2971 motion Day  100 100 dis low  auto       255 ena
Camera_Garage Dome            F0:9F:C2:14:46:17 192.168.1.67   G3 Dome    4.34.22  ena-640/480   ena-320/240   3-14 11:13:35 3-15 15:21:13                    7 motion Day  100 100 dis low  auto       255 ena
Camera_hallway UVC G3 Micro   F0:9F:C2:C1:96:7B 192.168.1.62   G3 Micro   4.34.22  ena-640/480   ena-320/240   3-14 11:13:35 3-15 16:37:34                  220 motion Day  100 100 dis low  auto       255 ena
Camera_kitchen outside UVC G3 F0:9F:C2:C1:14:CB 192.168.1.65   G3         4.34.22  ena-640/480   ena-320/240   3-14 11:13:35 3-11 14:42:51                    4 motion Day  100 100 dis low  a-Filt-Onl   0 ena
Camera_Kons-Window            F0:9F:C2:C1:C2:FD 192.168.1.68   G3         4.34.22  ena-640/480   ena-320/240   3-14 11:13:35                                  0 motion Day  100 100 ena low  auto       255 ena
Camera_Leo Window             B4:FB:E4:7F:5B:D3 192.168.1.69   G3         4.34.22  ena-640/480   ena-320/240   3-14 11:13:35 3-14 04:43:19                    5 motion Day  100 100 dis med  auto       255 ena
Camera_living room G3 Flex    B4:FB:E4:9E:FE:31 192.168.1.73   G3 Flex    4.34.22  ena-640/480   ena-320/240   3-14 11:13:35 3-14 18:06:33                    4 motion Day  100 100 dis low  auto       255 ena
Camera_living window G3 Flex  FC:EC:DA:1F:15:32 192.168.1.72   G3 Flex    4.34.22  ena-640/480   ena-320/240   3-14 11:13:35 3-15 16:12:35                  344 motion Day  100 100 dis low  off          0 ena
Camera_Side walk west G3 Flex FC:EC:DA:1F:15:5C 192.168.1.71   G3 Flex    4.34.22  ena-640/480   ena-320/240   3-14 11:13:35 3-15 16:36:53                  304 motion Day  100 100 dis low  off          0 ena
Camera_Study outside UVC G3   F0:9F:C2:C1:BA:F6 192.168.1.63   G3         4.34.22  ena-640/480   ena-320/240   3-14 11:13:35 3-15 15:19:03                   62 motion Day  100 100 dis low  auto       255 ena
Camera_UVC G3 Flex reserve    74:83:C2:3F:D3:E5 192.168.1.75   G3 Flex    4.34.22  ena-640/480   dis-320/240   3-12 09:59:11 3-12 10:00:21                    5 motion Day  100  80 dis      ?            0 dis
Camera_UVC G3 Instant 237D    74:AC:B9:9F:23:7D 192.168.1.76   G3 Instant 4.34.22  ena-640/480   dis-320/240   3-14 11:13:35 3-15 16:37:35                  200 always Day  100  80 dis low  auto       255 ena
   uniFiAP                         Protect Camera devices      END   =============================================================================================================================

Posted on
Fri Mar 12, 2021 6:56 pm
Korey offline
User avatar
Posts: 813
Joined: Jun 04, 2008
Location: Henderson, NV

Re: unifi- protect camera setup

Thanks so much for this!!!

:D :D :D

--
Korey

Posted on
Fri Mar 12, 2021 10:10 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: unifi- protect camera setup

My pleasure. Took me 6 months to figure this out.


Sent from my iPhone using Tapatalk

Posted on
Sat Mar 13, 2021 10:00 am
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: unifi- protect camera setup

Out of curiosity, are you listening to to the Unifi Protect updates websocket stream or doing like your others (IIRC) and using SSH commands to read the data?

Posted on
Sat Mar 13, 2021 12:10 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: unifi- protect camera setup

getting events: http polling every xx seconds, takes 0.08 ..0.16 secs elapsed time

some comments:
thumbnails / heat maps are only available at end of events
with smart detect it takes some time to trigger an event. It can recognize cars and persons. It seems to do fine, but have not done stats
the ring event is immediate

Karl

[edit] here the basic code to get the CURRENT event listing (set time window)
Code: Select all
import json
import requests
import time

requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)

session = requests.Session()
loginDict = { "username": userID, "password": passwd,"remember": True}
loginHeaders ={"Accept": "application/json", "Content-Type": "application/json", "referer": "/login"}
response = session.post("https://"+ipN+":"+port+"/api/auth/login", headers=loginHeaders,  json=loginDict, verify=False, timeout=10.0)

cookies_dict = requests.utils.dict_from_cookiejar(session.cookies)
cookies = {"TOKEN": cookies_dict.get('TOKEN')}
headers = {"Accept": "application/json", "Content-Type": "application/json", 'X-CSRF-Token': response.headers['X-CSRF-Token']}
endTime = int( time.time()*1000 )# now  its in integer m-secs (*1000)
startTime = endTime -4*1000 # - 4 secs
response = session.get("https://"+ipN+":"+port+"/proxy/protect/api/events"  ,  headers=headers, cookies=cookies, params={"end": str(endTime), "start": str(startTime)}, verify=False)

returns a list of events, each:
{u'smartDetectEvents': [], u'end': None, u'smartDetectTypes': [], u'camera': u'604b049d0206a503e700d760', u'partition': None, u'thumbnail': u'e-604b12f10052a503e700d7fb', u'heatmap': u'e-604b12f10052a503e700d7fb', u'start': 1615532781871, u'score': 0, u'modelKey': u'event', u'type': u'motion', u'id': u'604b12f10052a503e700d7fb'}
end = end time stamp (m-secs) of event
thumbnail is available when end is not None
type can be motion or ring ( thats what I have seen)

Posted on
Sat Mar 13, 2021 2:40 pm
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: unifi- protect camera setup

Cool deal! It is really annoying that they have not yet done a truly open API... the updates websocket with its binary (and non-published format) seems really risky, though the guys at Home Assistant have been using it for a while. That is the one I was watching trying to decide if I would attempt to port from TypeScript but always worried I would do that and it break the next day. :-|

I assume that works on (in theory) all Protect boxes or is that something you won't know until someone tries it? Meaning here, the CloudKey(s), NVR, etc.

Posted on
Sat Mar 13, 2021 2:42 pm
Korey offline
User avatar
Posts: 813
Joined: Jun 04, 2008
Location: Henderson, NV

Re: unifi- protect camera setup

RogueProeliator wrote:
Cool deal! It is really annoying that they have not yet done a truly open API... the updates websocket with its binary (and non-published format) seems really risky, though the guys at Home Assistant have been using it for a while. That is the one I was watching trying to decide if I would attempt to port from TypeScript but always worried I would do that and it break the next day. :-|

I assume that works on (in theory) all Protect boxes or is that something you won't know until someone tries it? Meaning here, the CloudKey(s), NVR, etc.


Works well on my UDMP!

--
Korey

Posted on
Sat Mar 13, 2021 3:31 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: unifi- protect camera setup

there is an example of a py3 interface https://github.com/briis/pyunifiprotect, it uses asyncio and web socket to listen, that modul needs py3.
I have not found anything that does that with py2.

And I was hesitating doing an
indigo py2 <-> py3 unifi wrapper
because you need to have several py3 modules installed and keeping that up to date is not too simple

The current solution with reading the controller (cloud key 2+) every xx secs does NOT show any significant increase in cpu load on the indigo server(*) and the response time is reasonable. The protect systems needs > 2 secs to trigger a motion event.

Karl
(*) the overall unifAP plugin uses in 3 minutes the following cpu amount (activity monitor) on a 2012 mac pro
there is a single http session that stays open all the time and serves: get event, get client info, get camera info, get unifi device info
14 secs no protect
12 secs w protect; read events every 6 secs
13 secs w protect; read events every 1 sec
13 secs w protect; read events every 0.5 secs
I guess that is in the margin of error (w protect less than w/o)
== nothing significant ==

Posted on
Sat Mar 13, 2021 10:24 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: unifi- protect camera setup

With event query every 0.5 secs and 13 cameras and 17 unifi devices and 100+ clients, the cpu on the gen2 plus is < 3% utilization (/settings/general on the controller).
That is astonishingly low, but the controller has 8 64bit CPUs

Karl

Posted on
Mon Mar 15, 2021 4:48 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: unifi- protect camera setup

Code: Select all
changes in V 7.38.352@ 2021-03-15
================== 
1. fixed some exception handling errors
2. now handles overlapping events better eg
   doorbell has motion event and then a doorbell rung event before the motion event ends
   smart detected person or vehicle: it will create several events for the same detection.
   they are shown in the variables
3. handling of orphan events in the plugin that do not create and end event.
   those would keep the status at eg motion for ever or until when the next event occures
4. added some more columns to the camera report
5. added some more help in config and camera print out


I will be adding some more help and screen shots to the first post.

Karl

Posted on
Thu Apr 08, 2021 11:55 am
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Unifi: protect camera setup

For those that like to understand or do some simple electronics:

As I went through 2 unifi door bells and always the magic little white box that connects the doorbell to the chime died,I tried to come top with my own solution,

This one works :

the circuit:
Screen Shot 2021-04-26 at 09.57.43.png
Screen Shot 2021-04-26 at 09.57.43.png (41.97 KiB) Viewed 9833 times

to top side of the board:
Screen Shot 2021-04-08 at 12.31.20.png
Screen Shot 2021-04-08 at 12.31.20.png (754.41 KiB) Viewed 10047 times

the bottom side of the board:
Screen Shot 2021-04-08 at 12.31.46.png
Screen Shot 2021-04-08 at 12.31.46.png (886.64 KiB) Viewed 10047 times


the green wires go to the transformer, polarity is not important
the black go to the unifi door bell , polarity is not important
the red and green go to the chime, red goes to the T (transformer) blue goes to F or B (front of back). Here the polarity IS IMPORTANT. The diode delivers constant power to the chime. (1/2 of the AC)

explanation:
When the door bell is pressed the unifi device shorts the 2 connections for about 2 seconds
That raises the voltage across the 5-15Ohm resistor (RX) from ~ 6 V to the full 24 V AC.
Through the 100 ohm feed resistor(RF) and diode bridge and the capacitor the relay (most relay have a + and - side!!) gets enough DC voltage to close.
That shortens the diode that feeds 1/2 AC to the chime w/o the relay. Now the chime gets the full AC current and rings / chimes.
W/o the diode the chime is off. It comes w the option for 3 1.5V D batteries. With batteries you do not need the diode.
The resistor RY(15-30 Ohm) is to limit the current / voltage into the chime

The circuit works w AC from 15V to 27 V.
Also simulated was a resistor of 0-5 Ohm for the long cable to the UniFi door bell (the black cables).


With the little white block that comes with the door bell the chime does only a short ring - like a “power on” ring. It does not play the whole melody. But with the 3D batteries (or the diode) it plays the whole melody.

I guess the white block that comes w the unifi door bell is just a 5 ohm resistor with some fancy connectors. At least it that’s what the dvm says

Karl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests