Watch Unifi Video Camera recordings from iOS Notification Center

The following automation will send an iOS push notification to your iOS device that will let you watch the recording right from the notification.

Requirements:

  • Home Assistant iOS app
  • Unifi Camera
  • Unifi Video accessible from the internet

In Unifi Video you will have to make sure it is set to record only motion. Also, you’ll have to generate an API key if you haven’t already.

sensor:
  - platform: rest
    name: Latest entryway recording ID
    method: GET
    resource: 'https://<Unifi NVR domain name>/api/2.0/recording?apiKey=<Unifi NVR API KEY>'
    value_template: >
      {% set last = (value_json.data | count) - 1 %}
      {{ value_json.data[last]['_id'] }}

automation:
  - alias: Unifi Video - Notify on motion with attachment
    trigger:
      platform: state
      entity_id: sensor.latest_entryway_recording_id
    condition:
      condition: state
      entity_id: group.primary_devices
      state: 'off'
    action:
      - service: notify.ios_<iOS Notify>
        data_template:
          message: >
            Entryway motion detected at {{ now().strftime('%-I:%M%P') }}
          data:
            attachment:
              url: 'https://<Unifi NVR domain name>/api/2.0/recording/{{ states.sensor.latest_entryway_recording_id.state }}/download?apiKey=<Unifi NVR API KEY>'
              content-type: MPEG4

It’s been working very well for me so far in testing. I receive the push notification within seconds of the recording being saved.

2 Likes

thanks for sharing i already have this set up my question is how you sent the icon-emoji

I removed it from the example to make it more clear. In your template you can add {{ "\U0001F6A8" }} to have a siren. You can use this site to find the hex code for any emoji and just replace the hex in the example.

I am trying to get this working, but not sure exactly which sections i need to change to match my environment. Obviously i need to change the server address and API key, but the rest i am not sure about.

I am getting this error in the logs in HA. I think it is because I don’t have a camera named entryway?

Running Unifi Video version 3.9.9 and HA 0.79

2018-10-11 01:23:50 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: list object has no element -1 (value: {“data”:[],“meta”:{“totalCount”:0,“filteredCount”:0}}, t
emplate: {% set last = (value_json.data | count) - 1 %} {{ value_json.data[last][’_id’] }}
)

It looks like your Unifi NVR API is not returning any recordings. You should be able to paste https://<Unifi NVR domain name>/api/2.0/recording?apiKey=<Unifi NVR API KEY> into your browser and see all of the recording IDs in JSON format.

So i did that, ended up using
http://:7080/api/2.0/recording?apiKey=

at which pointed I realized i was using a view only API key. once i change it to the key with recording permissions, it is returning data.
Now I am getting this error.

[homeassistant.helpers.template] Error parsing value: ‘value_json’ is undefined (value: , template: {% set last = (value_json.data | count) - 1 %} {

{ value_json.data[last][’_id’] }}

if i change it to use https://:7443 i get this error

2018-10-12 01:43:12 ERROR (SyncWorker_0) [homeassistant.components.sensor.rest] Error fetching data: <PreparedRequest [GET]> from https://10
.30.39:7443/api/2.0/recording?apiKey= failed with HTTPSConnectionPool(host=‘10.20.30.39’, port=7443): Max ret
s exceeded with url: /api/2.0/recording?apiKey= (Caused by SSLError(SSLError(1, ‘[SSL: CERTIFICATE_VERIFY_FAI
] certificate verify failed (_ssl.c:841)’),))
2018-10-12 01:43:12 WARNING (MainThread) [homeassistant.components.sensor] Platform rest not ready yet. Retrying in 60 seconds.

7080 is the correct port if you’re connecting over HTTP. 7443 is returning an untrusted certificate that Home Assistant is not going to connect to unless you somehow turn off certificate checking (which you should not do).

I’m not sure where you’re getting the API key from since in my Unifi NVR user setting page all I see is an option to enable/disable the API or regenerate the key. All the API is being used for is to grab the latest recording ID in order to build a URL to send to your iOS device.

I have two users, one is view only permissions, that is the key I was using the first time.

So when using the correct API and the port 7080 configuration, I am still getting this error. Sorry, I am not great with yaml and json yet.

Can you see the JSON recording IDs in your browser like I mentioned earlier?

yes, this is the first 4 lines it returns

{“data”:[{“eventType”:“motionRecording”,“startTime”:1531454672565,“endTime”:1531454678565,“cameras”:[“5afa28534f0cbbc330bc541f”],“locked”:false,“inProgress”:false,“markedForDeletion”:false,“meta”:{“cameraName”:“FrontDoor”,“key”:“ilcRsUZy”,“recordingPathId”:“5afa885a1f0f74426971e501”},"_id":“5b4824d14f0cde1e423a2fbf”},{“eventType”:“motionRecording”,“startTime”:1531454452576,“endTime”:1531454458576,“cameras”:[“5afa28534f0cbbc330bc541f”],“locked”:false,“inProgress”:false,“markedForDeletion”:false,“meta”:{“cameraName”:“FrontDoor”,“key”:“iFtCyQAF”,“recordingPathId”:“5afa885a1f0f74426971e501”},"_id":“5b4823f54f0cde1e423a2fbb”},{“eventType”:“motionRecording”,“startTime”:1531454424576,“endTime”:1531454430576,“cameras”:[“5afa28534f0cbbc330bc541f”],“locked”:false,“inProgress”:false,“markedForDeletion”:false,“meta”:

Sorry, I’m out of ideas. Seems like the REST sensor is returning valid JSON but value_json is not working for some reason.

got the sensor portion working.

image

I am getting a config error on the automation section, trying to work through that now.

This is the part I am not sure about.

automation:

  • alias: Unifi Video Notify on motion with attachment
    condition:
    entity_id: group.primary_devices

group.primary_devices is specific to my setup. It just prevents notifications from being sent when somebody is home.

ah.

so if i want it to send whenever, can i just remove the whole conditions section?

Yes. Just remove the entire condition.

I finally got it all working.

does your’s ever not display the video, only the notification?

Yes, that happens sometimes. I’m not sure why.

I have everything almost working, I can see the current recording in states okay. I am just having issues with the JSON in the automation it is showing the single quote with the current recording. If I remove the single quotes I am able to get the recording downloaded with no problems. Is there any way to format the JSON to remove the extra single quotes to have a clean url.