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, 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 is 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:
sudo su
apt-get -y update
apt-get install git
apt install curl
curl -sL https://deb.nodesource.com/setup_8.x | 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:
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+https://github.com/ragingcomputer/node-hikvision-api.git
Test to see if your hikmotion.js script works from the /opt/cameramotion/ directory, which you should still be in:
nodejs hikmotion.js
If 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:
systemctl daemon-reload
systemctl start hikmotion
systemctl enable hikmotion
Check to see if service is running:
systemctl status hikmotion
Hit 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:
journalctl -u hikmotion -b
Hit 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:
systemctl stop hikmotion
Make a change your hikmotion.js
cd /home/USERNAME/
Open the hikmotion.js file and make changes as needed
sudo cp hikmotion.js /opt/cameramotion/hikmotion.js
You may want to stop and start the service again to make sure changes take effect.
Make a change to your hikmotion.service
cd /home/USERNAME/
Open the hikmotion.service file and make changes as needed
sudo cp hikmotion.service /etc/systemd/system/hikmotion.service
chmod 664 /etc/systemd/system/hikmotion.service
systemctl daemon-reload
systemctl start hikmotion