I did some digging. To change notifications on Unifi Protect using just the REST API, this is what you would do:
Login
POST {NVR_IP}/login
with JSON body:
{
"username":"YOUR_USERNAME",
"password":"YOUR_PASSWORD",
"token":"",
"rememberMe":false
}
The response will give you 2 important headers:
- set-cookie header with a TOKEN cookie that future requests will need to provide
- Note: This token appears to be good for 2 hours
x-csrf-token
header that you’ll need to remember and use for all future non-GET requests
Get Info
GET {NVR_IP}/proxy/protect/api/bootstrap
As long as your request has the TOKEN cookie, you should get a response with info about all your cameras, users, NVR, etc.
Change Notifications
PATCH {NVR_IP}/proxy/protect/api/users/{USER_ID}/notifications
Where {USER_ID}
is the ID of the user whose notification settings you want to change.
You can get this info from the bootstrap endpoint above. Look under the users
key for the id
field of each user.
Body:
{
"state":"on",
"detectionNotifications":{
"cameras":[
{
"inheritFromParent":true, # True if you want to use Global Notification Settings on this camera. False if you don't
"camera":"...", # The Camera ID
"trigger":{
"when":"always",
"location":"away",
"sendAnyway":false,
"schedules":[
]
},
"motion":[
],
"alarmSmoke":[
],
"alarmCmonx":[
],
"alarmBabyCry":[
],
"person":[
"push", # For push notifications
"email" # For email notifications
],
"vehicle":[
],
"animal":[
]
}
]
}
}
Any of the categories can have either a “push”, or “email” notification. Not sure if others are supported. In the above config, I have no notifications if the camera detects a vehicle for example, but both push and email notifications if it detects a person.
All that’s left is to make a home-assistant service that uses something like the Python requests
library to do this automatically, and that should work.
I’ll look into that sometime.