Getting a Unifi protect video link in a notification

I am building an alarm system in node-red with multiple cameras in Unifi Protect. When a camera detects a person, I want to have a notification on my phone (iOS) with a snapshot of the detection, and when I click on the notification I want to see the video of the recorded event which triggered the notification.

I have the notification working with the snapshot, but I am struggling to get a link to the video. The cameras are all set to record only on motion, not continually.

The Unifi Protect documentation seems to imply that I should be able to create a link to a media-source with the video in question. I believe such a link probably should look something like this:
media-source://unifiprotect/606e5cff028cea02870003e9:browse:6228bd440089b2138701174a:smart:recent:1

I derived the link above from what the Unifi Protect integration creates inside the Media tab in the HA GUI, and I’ve tried various combinations including links that look more like what the Unifi Protect documentation states such as {nvr_id}:event:{event_id}, all to no avail.

I am including the link as the msg.video variable in the node-red function node below which builds the json for the notification to my phone.

let nvr_id = "25c342debc987901fd51b73c30234c49"
let image_url = `/api/unifiprotect/thumbnail/${nvr_id}/${msg.event_id}`

msg.payload = 
{
  "data": 
  {
    "message": msg.deviceName,
    "data": 
    {
      "time-sensitive": 1,
      "image": image_url,
      "url": msg.video
    }
  }
}
return msg;

I have had inconsistent success using the method:
/api/unifiprotect/video/{nvr_id}/{camera_id}/{start}/{end}
I believe primarily because I cannot find a programmatic way to get the end time for the video (and possibly the start time is not always correct either). By “inconsistent”, I mean more than half of the time the link is a 404. (Actually, I’m surprised it’s not pretty much always a 404).

I have looked at the blueprint from AngellusMortis but I have not actually installed it (maybe I need to try that).

I also found this helpful post from clin248 to extract the nvr_id, event_id and other necessary information.

If anyone has further tips what to try please let me know!

All of the unifiprotect API endpoints require authentication. You cannot access them outside of Home Assistant unless the URL is signed.

Home Assistant’s mobile app / HTML 5 notify platforms do this automatically.

Took me a while to realise that the start and end timestamps are from the events when the sensor starts detecting motion, and stops detecting motion.

Here is the function node that I am using now in node-red:

let nvr_id = "25c342debc987901fd51b73c30234c49"
let image_url = `/api/unifiprotect/thumbnail/${nvr_id}/${msg.event_id}`

let start_time = new Date(msg.video_start)
let end_time = new Date(msg.video_end)
let video_url = `/api/unifiprotect/video/${nvr_id}/${msg.entity_id}/${start_time.toISOString()}/${end_time.toISOString()}`
let click_url = "/dashboard-security/0"

let home_occupied = global.get("homeassistant.homeAssistant.states['input_boolean.home_occupied'].state");
if (home_occupied === "off") {
  msg.payload = 
  {
    "data": 
    {
      "message": msg.deviceName,
      "data": 
      {
        "time-sensitive": 1,
        "image": image_url,
        "video": video_url,
        "url": click_url
      }
    }
  }
  return msg;
}

And here is a snippet of the flow how I listen to the start and end events, join the outputs together to pass all the information to the function node and finally send a notification to my phone if my home is unoccupied:

Garage video camera (state node config):

Person detected event start (change node config)

Person detected event end (change node config)

Join node config

The image_url in the notification is a tiny thumbnail which you can’t see anything, but if you long press the notification the video will start streaming quite quickly (within 1 second I would say is about average). Overall I’m pretty happy with the performance, even though I would still prefer to be able to click on the notification to take me to the actual video of the motion, going to my lovelace security-dashboard also works pretty well.

I also updated the UniFi Protect documentation here to explain how to retrieve all the relevant variables: