rroller / dahua

Dahua Camera and Doorbell Home Assistant Integration
MIT License
398 stars 80 forks source link

Local Alarm input to binary sensor. #193

Open Mark79864 opened 2 years ago

Mark79864 commented 2 years ago

Please could there be an option to intergrade an NVR's local alarm input ports as a binary sensor into Home Assistant.

'num' is the alarm input number, starting from 0. A 4 alarm input NVR would be 0 - 3.

http://ip_address/cgi-bin/alarm.cgi?action=getInState[num] Returns: return = 0(clear) or return = 1(alarm)

Configuration can be obtained from:

http://ip_address/cgi-bin/configManager.cgi?action=getConfig&name=Alarm[num] Returns:

table.Alarm[0].Enable=true
table.Alarm[0].Name=Motion_sens1
table.Alarm[0].SensorType=NO
.
.

There is also more to this response back if there is actions set to the alarm, such as PTZ linkage.

I can confirm this HTTP API request works on my NVR5X-I. I've had a look through the code and I'm very lost on how to implement this sorry.

calisro commented 2 years ago

You could use event triggers possibly. If you listen for the events in the developer tab when the event happens and clears, then you can use a template event in HA to trigger it for you. You'd have to change the event_type, and the data to match this integrations event as you see it.

Something like this trigger for motion:

template:
  - trigger:
      - platform: event
        event_type: amcrest
        id: start
        event_data:
          camera: "Front Door Bell"
          event: CrossRegionDetection
          payload:
            code: CrossRegionDetection
            action: Start
            data:
              Action: Appear
              object:
                ObjectType: Human  
      - platform: event
        event_type: amcrest
        id: stop
        event_data:
          camera: "Front Door Bell"
          event: CrossRegionDetection
          payload:
            code: CrossRegionDetection
            action: Stop
            data:
              Action: Appear
              object:
                ObjectType: Human  
    binary_sensor:
      - name: Front Door Bell Human
        icon: mdi:motion-sensor
        state: >-
          {{ 'on' if trigger.id == 'start' else 'off' }}

If no events are fired, you can create a REST one like this:

binary_sensor:
  - platform: rest
    name: Frontyard Wifi
    resource: !secret frontyard_wifi
    scan_interval: 120
    value_template: |
      {% if value_json[0].value.Wifi.ssid != 'bucs2-2' %}off{% else %}on{% endif %}

The resource looks like this:

frontyard_wifi: "http://10.100.1.141/cgi-bin/api.cgi?cmd=GetWifi&userName=admin&password=xxxxxx"

where you would use your own above.

Mark79864 commented 2 years ago

You could use event triggers possibly. If you listen for the events in the developer tab when the event happens and clears, then you can use a template event in HA to trigger it for you. You'd have to change the event_type, and the data to match this integrations event as you see it.

Thankyou for this. I have got this function working Node Red currently. Some day I hope someone with more skill than me can implement this into the plugin.