Vera - Smarter Home Control Forum

General => Video Camera Control => Topic started by: maks327 on January 12, 2018, 12:33:02 pm

Title: Working Hikvision Motion Sensor in Vera - alternative to Blue Iris
Post by: maks327 on January 12, 2018, 12:33:02 pm
TL;DR I have a light javascript app running on a local PC to intercept Hikvision camera motion alerts and line crossing detection, and then use that to trigger scenes/device actions on Vera.

The long version:
Like others in threads over the years, I've really wanted to use the motion alert or line crossing detection in my outdoor Hikvision cameras (none have PIR built-in) to trigger actions in Vera.  As others  have pointed out (,36786.0.html), you can use Blue Iris software running on a PC to send an HTTP request to Vera to trigger a scene or device action.  I tried that setup for one camera, and while it worked, it used quite a bit of resources on my 4 or 5 year old Intel i5 CPU.  The load would never drop below 25%.  I have a few other things also running, but Blue Iris was using the most resources by far.  Usual load while the PC is idling would be closer to 5-10%.  That's all for 1 camera, and ideally I'd like to use it for 4 or 5 cameras which I'm sure would kill my PC.

Other home automation systems have figured out how to use the alerts directly from the Hikvision camera, but as far as I know Vera has not yet.  I am no coding expert, linux guru, or anything of the sort, but I was able to mooch off of other people's code to finally get this working.  I relied most heavily on the work of  Raging Computer's work doing this for OpenHAB (  The end result - I have a linux distribution running on a Virtual Machine inside my Windows PC (on the same network as Vera) running a very small javascript in NodeJS that intercepts motion alerts or line crossing detections from as many Hikvision cameras as you want and then sending an HTTP request to Vera to do whatever action you want, using minimal resources.  I'll explain what I did and how I did it.  I'm certain there are more efficient ways of doing this, and I'm totally open to suggestions/improvements, but for now this is working and using very little resources compared to running Blue Iris.  Bonus - it's totally free if you have the equipment to run it on.

What didn't work:
- Tried running the script directly in Windows via Node.js for Windows.  Didn't work, many of the required packages weren't compatible with Windows.
- Tried installing Linux under Windows 10's new "Windows Subsystem for Linux" that lets you install Ubuntu natively inside Windows and run a linux command line.  This looked promising, but I kept encountering errors with one of the required packages.  Maybe this will improve in the future, I know MS is still improving the WSL compatibility, but for now this was a no-go.

What worked:
Install Ubuntu.  I'm sure this could probably be done on a Raspberry Pi or other device, but in my case I have Windows 10 Pro and a PC that already runs 24/7, so I created a new Virtual Machine in Hyper-V (which is included for free in Windows 10 Pro).  I'd think other Virtual Machine programs would also work, but I used Hyper-V.  I installed Ubuntu 16.04 from here. (  This Windows Central article  ( a pretty good overview of how to setup Hyper-V and install Ubuntu on a compatible Windows PC.  If you can't run Hyper-V, VMware, VirtualBox, or something similar may be a better option for you.  Or of course run a full blown Linux install on something if you're so inclined.

Once you've got linux installed somewhere, install NodeJS and other required packages.  Open a terminal window and run the following one line at a time.  You may be prompted to enter "y" or something similar on occasion:
Code: [Select]
sudo su
apt-get -y update
apt-get install git
apt install curl
curl -sL | sudo -E bash -
apt-get install -y nodejs
npm install http
npm install request

Copy the hikmotion.js, package.json, and hikmotion.service files onto your home directory.  The forums wouldn't accept a .js file, so I changed all 3 files to txt files.  Please just remove the ".txt" extension, so files should be named "hikmotion.js", "hikmotion.service", and "package.json".  You'll have to figure out the best way of transfering this for you.  For me, I was able to transfer over a network drive that my linux VM and Windows PC could both access.  You could also put the files on Dropbox, Onedrive, etc.

You'll also need to modify the hikmotion.js file in notepad (Windows) or gedit (Linux) to include the IP address of your Vera and change the Vera action.  Right now mine is setup for one hikvision camera to trigger a scene for each motion event, and a different scene for each line detection event.  There are lots of modifications you could make to this.  You could trigger a device action instead of a scene.  You could copy the relevant code to run this for several cameras instead of just one.

One other note.  I've included the hikmotion.service file which will run the script as a service everytime the VM boots.  The ".service" file should work on any system that uses systemd (Ubuntu 15.04 and later).  If you're running Ubuntu 14.04 or earlier, or some other linux distro, it may use "Upstart" instead of systemd.  You can google all about that, but hopefully I've saved you the headache.  If you need to run this on a system that uses Upstart, you should be able to use the "hikmotion.conf" file and instructions as found on the Raging Computer link I included at the beginning.

Now copy the files to where they need to go and install the script:
Code: [Select]
sudo mkdir /opt/cameramotion
sudo cp hikmotion.js /opt/cameramotion/hikmotion.js
sudo cp package.json /opt/cameramotion/package.json
sudo cp hikmotion.service /etc/systemd/system/hikmotion.service
chmod 664 /etc/systemd/system/hikmotion.service
cd /opt/cameramotion
sudo npm install git+

Test to see if your hikmotion.js script works from the /opt/cameramotion/ directory, which you should still be in:
Code: [Select]
nodejs hikmotion.jsIf it's working, you should see a blinking cursor.  Try walking in front of your camera and see if you get anything.  Once you're confident your hikmotion.js is OK, start the service so it runs all the time:
Code: [Select]
systemctl daemon-reload
systemctl start hikmotion
systemctl enable hikmotion

Check to see if service is running:
Code: [Select]
systemctl status hikmotionHit the Ctrl-C to exit out of this.

That's it.  If you've gotten this far without any errors then you should have a functioning motion event on Vera triggered by your Hikvision cameras.  Like I mentioned, there may be better ways of doing this, but this has been working for me for about a week now without too much issue.  Typically uses 0% CPU while the machine is just idling in the background.  Make sure you set your Hyper-V or other software to start the VM automatically on every reboot.

If you want to check your logs and see what events have occurred you can enter:
Code: [Select]
journalctl -u hikmotion -bHit Ctrl-C to exit out of this.  The -b shows only logs since the last reboot of the VM.  If you want to see everything then you can remove the "-b".

If you need to stop the service for any reason:
Code: [Select]
systemctl stop hikmotion
Make a change your hikmotion.js
Code: [Select]
cd /home/USERNAME/Open the hikmotion.js file and make changes as needed
Code: [Select]
sudo cp hikmotion.js /opt/cameramotion/hikmotion.jsYou may want to stop and start the service again to make sure changes take effect.

Make a change to your hikmotion.service
Code: [Select]
cd /home/USERNAME/Open the hikmotion.service file and make changes as needed
Code: [Select]
sudo cp hikmotion.service /etc/systemd/system/hikmotion.service
chmod 664 /etc/systemd/system/hikmotion.service
systemctl daemon-reload
systemctl start hikmotion
Title: Re: Working Hikvision Motion Sensor in Vera - alternative to Blue Iris
Post by: loukt on January 13, 2018, 05:05:36 pm
Hi Maks327

Look at my reply #13  to Sorin M. topic "Hv cameras with motion sensor enabled".
in fact the I_HivisionIPCamera.xml from Andrei is good but only for camera with PIR sensor (which are a few).
My solution works for all 2CD... cameras from Hikvison. (which are a lot)
Just alter function getStreamEvents and also change you HVmodel in this file.
I created a few I_HV....xml files with different eventTypes to figure out wich event does the best job for me.
Also the Outdoor Motionsensor works fine.
NB the VMD event is very sensitive, a leave passing by causes to fire the MotionSensor

Title: Re: Working Hikvision Motion Sensor in Vera - alternative to Blue Iris
Post by: maks327 on January 15, 2018, 02:35:26 pm
Thanks, Loukt.  I did try your instructions in the thread you referred to, but I never could get vera to create some sort of motion device related to the camera.  I may revisit it again and see if I can get it working.

Either way, I like that with the motion detection off-loaded from Vera, I can have different actions for different camera alerts (motion, line detection, etc.).  Certain times of day are really bad for false positives with motion detection, but line detection is more accurate.  So I've got one motion detection scene that only runs at certain times of day, and the line motion runs the rest.  Setting this up also set the foundation for playing around with OpenLUUP and some of the other linux based tools available.
Title: Re: Working Hikvision Motion Sensor in Vera - alternative to Blue Iris
Post by: loukt on January 16, 2018, 03:01:49 pm
In order to get a MotionSensor be sure you enter your CameraType on the right places in I_HikvisionIPCamera.xml file
Search for "model:match" in this file and replace one of the 3 present camera models with your camera model.
Then restart your Vera and look at the log what has happened.

In "Hikvision Isapi_2.0_IPMD Service", Chapter 8.11.30 describes an alertStream.
When you make a putty connection e.g. to you Vera (UserVera:root PasswVera:nnnn) run this command

curl -s  http://usernameCamera:passwCamera@CameraIP/ISAPI/Event/notification/alertStream 
(the awk part of the command in my first post filters the output)

You catch in the putty window a live stream from your camera.
When there is motion in the camera field you will see the description changes.
If you have defined a detection field and/or a crossing a line event you can follow what happens.
EventTypes so far for me : fielddetection ,linedetection, VMD, shelteralarm, PIR, videoloss
  'The eventTypes are counted (except videoloss), dated, etc. and you can catch them as i earlier described.
  'PIR is only on a few DS-2CD... types available. (DS-2CD24.. series)

Experiment with the line position /areas on your Camera to get an alert you want.
I worked with three VirtualSwitches to "follow" an moving object.
VS01 on by VMD ; VS02 on by FielDetection ;VS03 on by LineCrossing
The state of the VirtualSwitches together gives info over the AlarmWarning from "something is passing by" till "RedAlert"

I know the approach from OpenHab but you should give this a try.