Amcrest IP Camera Component Enhancements - PTZ control and audio streaming

Hi @Bartem,

The intermediary API that this Amcrest component relies on (python-amcrest) does not currently include the capability to select an NVR channel. There is a recently open feature request on github for this though.

I too was using ffmpeg for my cameras connected through an NVR and was missing motion detection. I’ve recently started using this Amcrest component and had to connect to each camera directly instead, but have gained motion detection, PTZ, and presets.

In my early testing, motion detection events are being reliably triggered to fire a camera.snapshot.

See post 44 above for how @pnbruckner is handling motion detection.

1 Like

Is that what you’re getting when you send the following to the NVR?

http://<server>/cgi-bin/eventManager.cgi?action=getEventIndexes&code=VideoMotion

If so, that’s weird, because that doesn’t really jive with what the API doc says. (But, then again, unfortunately, it’s not all that uncommon for the camera to work a bit differently than the doc says. What would be the challenge if it didn’t?! :wink:)

Do you happen to know, when you got that output, which “channel” (i.e., camera) was actually sensing video motion at the time?

FWIW, when communicating directly to a camera that has sensed video motion, the output is:

channels[0]=0

And when no motion:

Error: No Events

Yes that is what I get when using that with the NVR . It seems like I get 0 for no motion 1 for motion (it was snowing so they all picked up motion except the one that hadn’t gone to night vision) . I can only access each camera individually using the “link to web” link because I’m using the nvrs built in switch but I tried using the “link to web” port that comes up for other commands / streams and it won’t connect

… Ill have to make sure that’s the exact command I used when I get home tonight I know I saw a few different ways to get the events im not sure if that’s what I was using

1 Like

@sea3pea0

Released 1.2.1 which should fix the problem created by the change in 0.90.0.

1 Like

Released 1.2.2 which incorporates some changes from the review of HA PR #22418. Basically entities should not have any “static” attributes - i.e., attributes that don’t change and, therefore, are not useful for automations. So corresponding attributes were removed (except for brand and model_name, which are somehow “special.”)

1 Like

@pnbruckner Yes that is the same line I am using to get the events and it gives me this response: 0 is definitely no motion and and 1 is motion for me (I watched it on my tv and it turned to a 1 as soon as the motion icon was displayed. Not sure if you guys have the same thing but with the NVR plugged directly into a TV/Monitor you get a little person icon in the corner of the camera when motion is detected.

channels[0]=0
channels[1]=0
channels[2]=0
channels[3]=0
EDIT:
Making some progress after staring at all the different Amcrest python files I figured out how I can modify them to look for each channel line (btw I’m no programmer by any means so teaching myself as I go) the only thing I’m trying to figure out now is the proper way to get HA to create 4 separate sensors from the one sensor.py file.  back to reading the dev docs... if I get anywhere I’ll fork it on git so maybe you guys can tell me how horribly I did :smile:

Do you have four cameras connected to the NVR, corresponding to channel numbers 0 through 3? Is channel 0 “special”, or just one of the cameras?

In your last post you show all values being 0. Is this when no camera indicated motion?

So this seems to definitely be different than the way the camera itself behaves, showing (in the case of my two models) 0 when motion is detected, and an error when no motion. Hmm.

BTW, I don’t have an NVR. But I’m considering attempting to add support for it, that is, after I get the current set of changes I’m working on completed and submitted.

@pnbruckner Yes I have 4 cameras connected all had no motion at the time. My plan was to add this to the event.py file…

    @property
    def is_motion_detected_1(self):
        event = self.event_channels_happened('VideoMotion')
        if 'channels[0]=1' not in event:
            return False
        return True

and do that for each channel? am I far off or are the brackets going to cause issues?
then in your sensor.py I was going to make a sensor entity that looked for each is motion detected_1, _2, etc… Am I way off?
(All this obviously just resolves the motion sensors, as ffmpeg motion just isn’t consistent enough and uses a little more processing power than I’d like to, I realize getting all 4 cameras to work directly from the component is a whole different issue, I currently just use ffmpeg RTSP and I’m content with that for now)

Not sure how it will ultimately evolve to support NVRs, but what you suggest is a reasonable quick fix for your needs. Although I’d write it something like this (specifically removing the property decorator):

    def is_motion_detected_in_ch(self, channel):
        return (
            'channels[{}]=1'.format(channel)
            in self.event_channels_happened('VideoMotion'))

And then call it this way:

            self._state = self._camera.is_motion_detected_in_ch(0)

etc.

Ok, I forked it on git, I’ll keep working at it. maybe keep an eye on me and see how I do… as I said I’m no programmer, just did some html in that late 90’s lol…

1 Like

@pnbruckner So I have the 4 motion sensors showing in HA… Im sure the code is really ugly but it works but this made me question something else… the settings on the camera motion reset can only go down to 10 seconds… in reading the API docs I see that you can “subscribe” to events… is that how the mobile app generates its push notifications? And is there I way we could “subscribe” to these alerts so the notification is almost instant? Thanks for the help

Great question @Bartem! I agree with you that this 10 second polling or delay is not very effective, especially for capturing fast moving objects.

Before I started using this Amcrest component, I was trying to get this Dahua event listener python code to work under Hassio. I just ran into too many hurdles with Hassio Docker installations to get one of the dependencies (pycurl) to install.

Other users have it working in conjunction with MQTT for event-driven motion detection triggers.

When motion is detected the HA sensor should go on when the next poll occurs. If you look at SCAN_INTERVAL in my binary_sensor.py file, you’ll see the poll period is set to 5 seconds, which will be the worst case delay from the time motion is detected by the camera until the binary_sensor in HA changes state to ‘on’.

The setting in the camera that you’re talking about, as I understand it, has no effect on the worst case delay. What it controls is, once the camera detects motion, how long it will continue to indicate motion after motion is no longer detected. This is an “anti-dither” parameter that prevents “glitchy” motion detection (i.e., the indication going on and off really fast.) So, e.g., let’s say you set it to 10 secs. If motion is detected for 5 secs, then no motion for 9 secs, then motion is detected again, the motion indication will go on immediately, then stay on during that whole time, even though there was a span of 9 secs where there was no motion being detected. (Clear as mud yet?)

Since this parameter can’t go below 10, that means that the HA binary_sensor will always capture a motion event because it is polling every 5 seconds, and the indicator will never be on for less than 10 secs.

Again, it has nothing to do with the delay. That is purely controlled by the scanning period which is fixed at 5 seconds (at least for now. Of course, this is a custom component, so you can always just edit that value to whatever you want. :slight_smile:)

Thanks I think that makes a little more sense… I was reading the Amcrest manual (which is horribly vague in some cases… after having my new 4 channel binary sensors :wink: working for a little bit it does seem to be pretty consistently picking up the motion. I’m gonna watch it for a couple days then maybe move my snapshot trigger to that motion sensor and see how it does compared to ffmpeg motion.
59%20PM
Thanks for all your help!

Are you using my custom component? It seems like you’re using the sensor, not the binary_sensor. If you’re using the sensor, then realize the polling period is 10 seconds, not 5 like the binary_sensor.

Well what I hacked from your sensor lol… where did I go wrong?

Edit let me check and make sure I have the right version on there when I get back to my laptop

…yep I forked up and forked the wrong one… :slight_smile:

1 Like

Released 1.3.0

This was mostly an internal cleanup (in preparation for an upcoming PR to the standard component), but it also added a service (and associated attribute) to enable/disable motion recording. (Preparing to hopefully remove the amcrest switches.) Also, when enabling recording make sure video stream is turned on.

I am indeed using your Amcrest custom component, just updated to 1.3. I was also using sensor.X.camera_motion_detected because that was the only option available. I’m not seeing any equivalent binary_sensor entity created for camera motion.

Something in my configuration appears to be causing binary_sensor.py to not load. I do not get a custom component warning for it like I do the others…

2019-04-03 08:51:12 WARNING (MainThread) [homeassistant.loader] You are using a custom component for amcrest which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2019-04-03 08:51:13 WARNING (MainThread) [homeassistant.loader] You are using a custom component for custom_updater which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2019-04-03 08:51:17 WARNING (MainThread) [homeassistant.loader] You are using a custom component for life360.device_tracker which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2019-04-03 08:51:17 WARNING (MainThread) [homeassistant.loader] You are using a custom component for composite.device_tracker which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2019-04-03 08:51:20 WARNING (MainThread) [homeassistant.loader] You are using a custom component for amcrest.sensor which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2019-04-03 08:51:21 WARNING (MainThread) [homeassistant.loader] You are using a custom component for amcrest.camera which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2019-04-03 08:51:21 WARNING (MainThread) [homeassistant.loader] You are using a custom component for amcrest.switch which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.

The code seems to be installed properly via custom_updater (HASSIO 90.2)…

/root/config/custom_components/amcrest

-rw-r--r--    1 root     root          6378 Apr  3 08:49 __init__.py
drwxr-xr-x    2 root     root          4096 Apr  3 08:51 __pycache__
-rw-r--r--    1 root     root          2219 Apr  3 08:49 binary_sensor.py
-rw-r--r--    1 root     root         18751 Apr  3 08:49 camera.py
-rw-r--r--    1 root     root           107 Apr  3 08:49 sensor.py
-rw-r--r--    1 root     root           101 Apr  3 08:49 switch.py

I’ve enabled debug (homeassistant.custom_components.amcrest: debug), but that didn’t show anything extra. What else can I do to track down why it’s not being loaded?

Do you have:

    binary_sensors:
      - motion_detected

under your amcrest configuration?

FWIW, there’s no documentation for this custom_component since I wasn’t really intending to support it much (for several reasons.) But there will be as this gets folded into the standard component. In fact, my next PR will be adding the binary_sensor.

Question: When I submit that PR, should I:

  1. Just leave the motion detector sensor as-is (and keep it forever),
  2. deprecate the motion detector sensor (removing it in another release down the road), or
  3. remove the motion detector sensor (making it a breaking change)?

Opinions appreciated.

Hello, i’m trying to use this component with a dahua camera that shares the same api as Amcrest. But it looks i have some problems with authentication

Query failed due to error 401 Client Error: Unauthorized for url: http://192.168.XXX.XXX:80/cgi-bin/snapshot.cgi?channel=0