Dahua IP cam IVS event as binary_sensor equals smarter camera motion detection

The Dahua Starlight 5231 IP cam is one of the best bang for the buck POE IP cameras on the market. See extensive review and discussion here: https://ipcamtalk.com/threads/dahua-starlight-varifocal-turret-ipc-hdw5231r-z.14683/

Camera-based motion detection is pretty inaccurate and ends in lots of false positives. New cameras from Hikvision and Dahua and other manufacturers include smarter motion detection with virtual tripwires, zone intrusion, etc. that is significantly more accurate.

User cor35vet on ipcamtalk wrote a small python script to monitor Dahua IVS events. See here: https://ipcamtalk.com/threads/trigger-blue-iris-with-ivs.17303/#post-163196. I modified it to send MQTT messages so I could incorporate my cameras into HomeAssistant. See here: https://github.com/johnnyletrois/dahua-watch/blob/master/watch.py. It would be awesome if someone smarter than me would make a native component for HomeAssistant.

I use the following binary sensors to monitor the MQTT topics:

binary_sensor:
  - platform: mqtt
    state_topic: "home-assistant/cameras/0/IVS"
    name: "Garage Tripwire"
  - platform: mqtt
    state_topic: "home-assistant/cameras/1/IVS"
    name: "NWhouse Tripwire"
  - platform: mqtt
   state_topic: "home-assistant/cameras/2/IVS"
   name: "SWhouse Tripwire"

One of my automations using the above sensors is to play a dog barking on my patio speakers and turn my office light on if the garage sensor is tripped:

- alias: Garage cam tripwire
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.garage_tripwire
    to: 'on'
  condition:
    condition: and
    conditions:
      condition: time
      after: '22:30:00'
      before: '06:00:00'
  action:
    - service: notify.ios_john_iphone_app
      data:
         message: "Garage tripwire activated"
        data:
          attachment:
            url: !secret ip_cam_garage_image_login_url
            content-type: jpeg
            hide-thumbnail: false

    - service: light.turn_on
      data:
        entity_id: light.office_ceiling_level
        brightness: 255

    - service: script.dog_bark

    - delay: '00:00:{{ (range(30, 55)|random|int) }}'

    - service: light.turn_off
      entity_id: light.office_ceiling_level
2 Likes

Will this work with Hikvision camera?

1 Like

I don’t know.

IVS events aren’t part of the ONVIF standard.

Would be nice if someone can make a component for Dahua cameras.
Really want to make more use of the PTZ function of my camera.

2 Likes

Vote for my component request!

1 Like

Hi

Thanks for the good start.
I tried to transform it into a component, available here:

6 Likes

Hi!

It works well? Can you put screenshots of how it is integrated into home assistant?

Thank you very much!

Getting the following error:

2018-03-23 12:21:08 ERROR (MainThread) [homeassistant.setup] Error during setup of component dahua_event
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py", line 145, in _async_setup_component
    component.setup, hass, processed_config)
AttributeError: module 'custom_components.dahua_event' has no attribute 'setup'

Would love to use this, but i don’t get any further than a component not found error. i’m on 0.65.5

Same here, nothing happen in the frontend after the Sawey’s component setup. I’m connecting to a Dahua NVR and in the log I see:

2018-04-04 07:31:46 INFO (MainThread) [homeassistant.setup] Setting up dahua_event
2018-04-04 07:31:46 DEBUG (Thread-5) [custom_components.dahua_event] Added Dahua device at: http://mylocalip.address:port/cgi-bin/eventManager.cgi?action=attach&channel=1&codes=%5BVideoMotion%5D
2018-04-04 07:31:46 INFO (MainThread) [homeassistant.setup] Setup of domain dahua_event took 0.0 seconds.
2018-04-04 07:31:46 INFO (MainThread) [homeassistant.core] Bus:Handling <Event component_loaded[L]: component=dahua_event>

After this, there are a lot of:

2018-04-04 07:34:30 DEBUG (Thread-23) [custom_components.dahua_event] [dahua] OnDisconnect(Success)

I’m sure that user/psw are corrects. If anyone more able than me could take a while to try to see the SaWey’s code on github (link here) all Dahua costumers could be very happy :heart:

I tried to exclude the “&channel=1” from the url_template and to exclude also the port (80 is default’s one) according to the Dahua’s api, but nothing changes.
Thank you very much!

Continue to trying, the Sawey’s component start working for me changing the authentication method (from pycurl.HTTPAUTH_DIGEST to pycurl.HTTPAUTH_BASIC). Now in debug I see all the NVR events.

2018-04-05 09:22:45 DEBUG (Thread-24) [custom_components.dahua_event] [dahua]: – myboundary
Content-Type: text/plain
Content-Length:36
Code=VideoMotion;action=Stop;index=0

Sadly there are also errors:

2018-04-05 09:24:32 DEBUG (Thread-24) [custom_components.dahua_event] [dahua] OnDisconnect(Failed writing body (0 != 100) (23))

The only write function in the script is this one:

CurlObj.setopt(pycurl.WRITEFUNCTION, device.OnReceive)

Anyone know how to solve this problem?

Any one know if will be soon any components like this for Dahua?

I’m getting this error, any ideas what’s wrong? Would love to get this working.

2018-05-20 16:18:15 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.6/site-packages/homeassistant/setup.py”, line 56, in async_setup_component
return await task
File “/usr/lib/python3.6/site-packages/homeassistant/setup.py”, line 101, in _async_setup_component
component = loader.get_component(hass, domain)
File “/usr/lib/python3.6/site-packages/homeassistant/loader.py”, line 86, in get_component
module = importlib.import_module(path)
File “/usr/lib/python3.6/importlib/init.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 994, in _gcd_import
File “”, line 971, in _find_and_load
File “”, line 955, in _find_and_load_unlocked
File “”, line 665, in _load_unlocked
File “”, line 674, in exec_module
File “”, line 781, in get_code
File “”, line 741, in source_to_code
File “”, line 219, in _call_with_frames_removed
File “/config/custom_components/dahua_event.py”, line 7

Can anyone point me to the docs on how to add this custom component? I am new to hass.io and would like to play with this.

Thanks

Would you still recommend this camera?

I want to buy 1-4 cameras probably one 1-2 outdoor and 1x indoor. Must be poe. Would prefer to hook up to an Hikvision / Dahua NVR with 4-8 port POE, rather than running blue iris.

Is there any discussion on dahua vs hikvision nvr when it comes to home assistant integration?

I would absolutely buy them again.

Thanks.

Had some more questions, to make sure i don’t purchase the wrong stuff. Do you use blue iris or a standalone dahua nvr?

Can the script monitor ivs events from the nvr directly, if the cameras are connected directly to an nvr with built in poe?

If my cameras are directly connected to the nvr via the nvr’s built in poe switch then can h.a. access the cameras directly to check for ivs events?

My current setup is.

Unifi usg router -> Unifi 8 port poe switch -> Unifi wireless AP.

I was hoping to use only 1 port of the switch and run that to the nvr then have the nvr connect to 2-3x cameras.

I’m not sure. I can only tell you my setup. My cameras connect to my unifi 24 port poe switch. My qnap NAS records the video streams. The Python script monitors each camera for IVS events. As long as you have network access to each cameras ip address I don’t see why you couldn’t connect them to an NVR.

1 Like

That’s great. Thank you.