I recently got a sensor created that polls the Unifi Video API to determine if there is motion on a camera. This utilizes the REST platform. I’ve not seen anything else here that directly uses this method (some of the other ones I have seen use MQTT or check logs on the Unifi Video system). So far this has been working great for the past few days.
Note that I’m fairly new to Home Assistant, and to working with APIs in general, so this is probably not the most ideal way to do this. Suggestions are welcome!
Note that for the ‘resource’ URL in the code below, you’ll need a couple pieces of info. First is your API key which can be obtained in the Unifi Video user settings. Second, you’ll need the ID of your camera. This ID can be obtained by sending a POST to the url below. I used Postman to get this.
http://unifi-video-ip:7080/api/2.0/login
The resulting JSON will contain (among many other things) a section like this. The ID you’ll need for the ‘resource’ URL in the code below is in the ‘cameraPermissions’ section:
{
"data": [
{
"userGroup": {
"groupType": "PUBLIC_GROUP",
"cameraPermissions": {
"5c29835792587293475e7": 25
},
},
}
],
}
Actual config for the motion sensor:
binary_sensor:
- platform: rest
resource: http://unifi-video-ip:7080/api/2.0/camera/5c29835792587293475e7?apiKey=asd0f9098sdf098sf09df
method: GET
authentication: digest
username: video-username
password: !secret video-password
name: 'Camera Motion'
headers:
User-Agent: Home Assistant
Content-Type: application/json
device_class: motion
scan_interval: 1
value_template: >
{% if value_json.data.0.recordingIndicator == 'MOTION_STARTED'
or value_json.data.0.recordingIndicator == 'MOTION_INPROGRESS'
or value_json.data.0.recordingIndicator == 'MOTION_DONE' %}
{{'ON'}}
{% else %}
{{'OFF'}}
{% endif %}
I have the scan interval set pretty high (every 1 second) but it doesn’t seem to be causing any issues so far. If you poll for status of more than one camera it might overload the NVR, I’m really not sure.
Enjoy!