Trigger on Event Data Represented as an Array

To simplify my question, I am going to reference this example from the Dahua integration github page:

Given the following event data:

{
    "event_type": "dahua_event_received",
    "data": {
        "name": "Cam13",
        "Code": "VideoMotion",
        "action": "Start",
        "index": "0",
        "data": {
            "Id": [
                0
            ],
            "RegionName": [
                "Region1"
            ],
            "SmartMotionEnable": false
        },
        "DeviceName": "Cam13"
    },
    "origin": "LOCAL",
    "time_fired": "2021-06-30T04:00:28.605290+00:00",
    "context": {
        "id": "199542fe3f404f2a0a81031ee495bdd1",
        "parent_id": null,
        "user_id": null
    }
}

… this event trigger works perfectly fine.

platform: event
event_type: dahua_event_received
event_data:
  name: Cam13
  Code: VideoMotion
  action: Start

However, taking this to the next step by including the RegionName as a component of the trigger does not seem to work. I have similar event triggers in other automations, the only difference I can see in this example is that the RegionName data is represented as an array. I assumed this would work, but it does not trigger when ‘Region1’ is represented in the array during a dahua_event_received event. Here is what I am using:

platform: event
event_type: dahua_event_received
event_data:
  name: Cam13
  Code: VideoMotion
  action: Start
  data:
    RegionName: 'Region1'

Regardless if Region1 is the only item in the array, or if the array contains other items in the list, i.e., Region0, Region1, Region2… the event does not trigger.

I have attempted several different ways to format, but nothing seems to work. Any thoughts?

For an event trigger to work like that you would need to supply the entire value for data.

Instead, use the broader trigger and add template conditions to check the other data.

trigger:
  - platform: event
    event_type: dahua_event_received
    event_data:
      name: Cam13
      Code: VideoMotion
      action: Start
condition:
  - "{{ trigger.event.data.data.RegionName | contains('Region1') }}"

I am using this in a trigger-based template sensor to create a binary sensor, and unfortunately it does not seem I can use conditions in that process?

You can use this to capture the existing state information and then use if/then in the state template so a new state is only set when the trigger’s extended data is correct. Post your sensor config and we can propose options.

I have a general sense of what you’re saying, but I’m not sure I completely follow:

My current attempt at a trigger sensor looks like this:

  - trigger:
      - platform: event
        event_type: dahua_event_received
        event_data:
          DeviceName: "West Camera"
          Code: "VideoMotion"
          action: "Start"
          data:
            RegionName: "Zone 1"
    binary_sensor:
      - name: West Camera VMD Zone 1
        device_class: motion
        state: "{{ trigger.event.data.action == 'Start' }}"
        auto_off: 3
        attributes:
          last_triggered: "{{ trigger.event.time_fired.isoformat() }}"

I have used this same trigger-based template sensor in the past, and as a matter of fact, it works fine in this instance when I remove the RegionName component. But then I get a trigger when every single zone is activated.

  - trigger:
      - platform: event
        event_type: dahua_event_received
        event_data:
          DeviceName: "West Camera"
          Code: "VideoMotion"
          action: "Start"
        variables:
          zone_1: "{{ trigger.event.data.data.RegionName | contains('Zone 1') }}"
    binary_sensor:
      - name: West Camera VMD Zone 1
        device_class: motion
        state: "{{ trigger.event.data.action == 'Start' and zone_1 }}"
        auto_off: 3
        attributes:
          last_triggered: |
            {{ trigger.event.time_fired.isoformat() if zone_1 
            else this.attributes.last_triggered | default(now()) }}
2 Likes

Oh my ! Now that looks interesting. I’ll take some time and play with it and let you know soon.

Thank you @Didgeridrew, this was a very elegant solution. I’ve plugged in all 4 zones and they seem to be registering perfectly fine. Thanks so much for your time. Hope you have a terrific week.

1 Like