Unifi NVR Video - control recording

Moderator: leecollings

Post Reply
vrmp
Posts: 18
Joined: Friday 21 July 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8100
Location: Netherlands
Contact:

Unifi NVR Video - control recording

Post by vrmp »

I have used the python plugin created by gysmo38 (thanks for that), but somehow my installation keeps having issues from time to time with python plugins. Because of that reason I recreated the functionality in LUA code, which works without an issue for me.
I'm using the function below to turn recording on for my three camera's when I activate my alarm.
Perhaps some other people are looking for the same solution and that is why I'm sharing this with you all.

The variables:

Code: Select all

--Cameras
Device_UVC_IP = "192.168.0.2" -- NVR IP for example 192.168.0.2
Device_UVC_Port = "7080" -- NVR port, default is 7080
Device_UVC_API = "API12345678" -- API key get this from the NVR (Listed as a tab under the user account)
Device_UVC_UVCG3Dome = "UVC G3 Dome" -- Camera name
Device_UVC_UVCG3Dome_ID = "3284710471094891084" -- Camera _id (You can get this from this url: http://192.168.0.2:7080/api/2.0/camera/?apiKey=12345678 . Fill in your own IP address and apiKey
The Function:

Code: Select all

-- Device is the virtual switch with for example the camera attached
-- Camera is the camera name
-- ID is the camera ID
-- Status is to turn recording Always, Motion or Off
function CameraSetRecordingMode(Device,Camera,ID,Status)
	if (Status == "Always")
	then
		url = 'curl -X PUT --data \'{"name":"' .. Camera .. '","recordingSettings":{"motionRecordEnabled":"false","fullTimeRecordEnabled":"true","channel":"0"}}\' http://' .. Device_UVC_IP .. ':' .. Device_UVC_Port .. '/api/2.0/camera/' .. ID .. '?apiKey=' .. Device_UVC_API .. ' --header "Content-Type:application/json"'
		io.popen(url .. ' &')
		commandArray[#commandArray +1]={[Device] = 'On'}
	elseif (Status == "Motion")
	then
		url = 'curl -X PUT --data \'{"name":"' .. Camera .. '","recordingSettings":{"motionRecordEnabled":"true","fullTimeRecordEnabled":"false","channel":"0"}}\' http://' .. Device_UVC_IP .. ':' .. Device_UVC_Port .. '/api/2.0/camera/' .. ID .. '?apiKey=' .. Device_UVC_API .. ' --header "Content-Type:application/json"'
		io.popen(url .. ' &')
		commandArray[#commandArray +1]={[Device] = 'On'}
	elseif (Status == "Off")
	then
		url = 'curl -X PUT --data \'{"name":"' .. Camera .. '","recordingSettings":{"motionRecordEnabled":"false","fullTimeRecordEnabled":"false"}}\' http://' .. Device_UVC_IP .. ':' .. Device_UVC_Port .. '/api/2.0/camera/' .. ID .. '?apiKey=' .. Device_UVC_API .. ' --header "Content-Type:application/json"'
		io.popen(url .. ' &')
		commandArray[#commandArray +1]={[Device] = 'Off'}
	end
end
An Example:

Code: Select all

--This will turn on recording on the Unifi NVR/Camera and set a virtual switch to On. Use "Off" to turn this off again
CameraSetRecordingMode("Camera",Device_UVC_UVCG3Dome,Device_UVC_UVCG3Dome_ID,"On")
I'm using a virtual device which has the Unifi camera attached to it. This way I can quickly open the camera preview and can see if they are in recording.
maomanna
Posts: 94
Joined: Monday 30 November 2015 16:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Unifi NVR Video - control recording

Post by maomanna »

looks great!

Is every part a piece of a lua script, or do i need to make several lua's
If so, what type??
vrmp
Posts: 18
Joined: Friday 21 July 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8100
Location: Netherlands
Contact:

Re: Unifi NVR Video - control recording

Post by vrmp »

It's all part of the same lua script.
You have to set your own settings/variables in the first part so you connect to the correct device/camera.
The function is called from the code used in the example, so that also has to be available in the same script. That part uses the same variables you defined earlier. The only extra part is you have to create a virtual switch, which I named "Camera" in the example. You can please that line anywhere within your current code. For example when a device changes or from a different trigger, that is up to you :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest