Amcrest IP Camera Motion Detect

I have an Amcrest IP4M-956E dome camera I am using to monitor the front of my house. The camera does not have a builtin feature to allow HTTP calls when motion is detected, but I found a different way to get openHab to respond to motion events. I thought I would share the details with the community.

Item

String  Amcrest_Motion  "Motion detected [%S]"  <camera>        (Group)         { http="<[http://user:pass@x.x.x.x/cgi-bin/eventManager.cgi?action=getEventIndexes&code=VideoMotion:2000:JS(motiondetect.js)]" }

I am using the http binding with the Amcrest API to query the events info from the camera. After motion is detected, the camera will report the motion for as long as it is occurring and for up to 60 seconds after the motion stops. If no motion is detected, the camera returns “Error: No Events”. If motion is detected, it simply returns “channels[0]=0”. I am using motiondetect.js to translate the results into string TRUE or FALSE.

motiondetect.js

// Wrap everything in a function
(function(i) {
    var response = i.replace(/(\r\n|\n|\r)/gm,"");
    if(response == 'channels[0]=0'){return "TRUE"}else{return "FALSE"};
})(input)
// input variable contains data passed by openhab

Rule

var Timer tOutdoorMotionLightsOff = null
var DateTime dtOutdoorMotionTriggerTime = new DateTime(now)
var DateTime motionSunset = null

rule "Driveway Motion"
when
        Item Amcrest_Motion changed
then
        logInfo("Timer", "Motion Detected!")
        motionSunset = new DateTime(parse(now.getYear()+"-"+now.getMonthOfYear()+"-"+now.getDayOfMonth()+"T"+strSunset.state))
        if ((Amcrest_Motion.state == "TRUE")
                && ((motionSunset.isBeforeNow()) || (now.getHourOfDay() < 7))
                && (Light_Outdoor_Front.state == OFF)
                && (dtOutdoorMotionTriggerTime.plusMinutes(6).isBeforeNow())) {
        logInfo("Timer","Driveway motion detected. Turning on light and creating off timer")
        sendCommand(Light_Outdoor_Front, ON)
        if(tOutdoorMotionLightsOff!=null) {
                logInfo("Timer","Timer tOutdoorMotionLightsOff cancelled")
                tOutdoorMotionLightsOff.cancel()
        }
        tOutdoorMotionLightsOff = createTimer(now.plusMinutes(5)) [|
                logInfo("Timer","Timer tOutdoorMotionLightsOff executed")
                dtOutdoorMotionTriggerTime = new DateTime(now)
                        sendCommand(Light_Outdoor_Front, OFF)
        ]
    }
end

I am using the rule to turn the front light on for 5 minutes, but only if the current time is after sunset or before 7am and it has been at least 6 minutes since the last motion detection. This is because the act of turning off the light causes a motion trigger in the camera. If this check wasn’t there, the light would turn off and on all night. The only other if clause is to check if the light isn’t already on. I wouldn’t want motion to turn off the light if i already had it turned on for some reason.

2 Likes

Brilliant, thanks for posting! I just picked up an Amcrest IP3M-943B and this is working as you’ve noted. Appreciate you sharing!

Just a heads up a couple of things.

  1. Amcrest have disabled basic auth which is what your links are using in newer firmwares. If you upgrade the firmware of the camera it may/will break this from working. Info on this is around if you search in google or the amcrest forum.
    https://amcrest.com/forum/technical-discussion-f3/cgi-sdk-no-longer-functioning-on-17r--t2401-s30.html

  2. I am writing a binding using an Amcrest camera and have to support digest auth to get my camera running. Binding is here and I am days away from having motion, audio, card full and heaps of other alarms working.
    New Binding: IpCamera

  3. Sending a password in plain text or with base64 with HTTP is not best practice. Try using HTTPS instead as that should be a simple change for you until the binding is ready to handles this.

If there are any features you wish to see in the binding feel free to post.

This is good,
Maybe changing the thread category to Tutorial&Examples / Solutions would highlight it’s usefulness.

I just released the binding with working motion detection in my binding here for amcrest. Many other features can be added as I have time.

There is also another binding FTP Upload, which could be used for camera motion detecting.

Most of the network cameras (also amcrest) can send image to FTP server when motion or sound is detected. This binding acts a FTP server, where camera can send a image.

1 Like

Just a heads up that the link in your post is broken, is it still around? In my binding I now have audio as well as motion alarms working for both Amcrest and Foscam, its worth checking out as I have done a lot of work on it the past few weeks.

Just fixed the link as during code reviews, we decided to rename the binding to more generic (FTP upload binding). Binding is now merged to master, so it’s available in snapshot builds.

1 Like