Automation expert challenge - "Complex sensor" based on json attributes

i’m getting this json from camera events,
I’d like to build a sensor which later can use for automation.
the event publish in camera{id}/topic with below json:

  "code": "CrossLineDetection",
  "action": "Stop",
  "index": "0",
  "data": {
    "Class": "Normal",
    "CountInGroup": 1,
    "DetectLine": [
      [
        8023,
        7872
      ],
      [
        3530,
        7872
      ]
    ],
    "Direction": "RightToLeft",
    "EventSeq": 41,
    "Extension": {
      "EventLongID": "340200000013200000010220190922233606015959900000"
    },
    "FrameSequence": 49271442,
    "GroupID": 41,
    "IndexInGroup": 0,
    "Mark": 0,
    "Name": "Rule 3",
    "Object": {
      "Action": "Appear",
      "BoundingBox": [
        4560,
        7872,
        5504,
        8000
      ],
      "Center": [
        5032,
        7936
      ],
      "Confidence": 0,
      "FrameSequence": 0,
      "LowerBodyColor": [
        0,
        0,
        0,
        0
      ],
      "MainColor": [
        0,
        0,
        0,
        0
      ],
      "ObjectID": 3953,
      "ObjectType": "Human",
      "RelativeID": 0,
      "Source": 0,
      "Speed": 0,
      "SpeedTypeInternal": 0
    },
    "PTS": 43933175900,
    "RuleId": 3,
    "Sequence": 0,
    "Source": 27304392,
    "Track": null,
    "UTC": 1569195366,
    "UTCMS": 671
  }
}

Im not sure this can done through templates/external py script,
But i guess there are many creative people here ,
so can I create generic sensor or multiple sensor which based on
the values from above json?

for attribute --> Name : Rule 2 (it can be Rule 1/2…4)
And **Direction : LeftToRight ** (it can be also RightToLeft)
It also can be combination of those 2…

it could be awesome also using the ObjectType (which can if this event trigger by human or something else ) - but for now it less important .

What would be best approach for implementing this without
adding 4 different sensors?
case it possible ,
How can i extract those values and assign logic which describe the sensor state?
i.e if rule 1 trigger with RightToLeft - can ignore it ,
but if it would be LeftToRight - it trigger the alarm.

Thanks for the helpers!

Is this coming in on an mqtt topic?

Yes , this json publish in camera{id}/event .

Then the docs cover it don’t they? https://www.home-assistant.io/components/sensor.mqtt/

Well, first not sure what would be the right way,
I guess i can create a binary_sensor per rule,
But not sure how to implement this,
i manage to extract the value by
‘{{ value_json.data.Name }}’

So if i want to create sensor OR binary_sensor,
how it should look like?
I tried :

 - platform: mqtt
    name: window_cross
    state_topic: cameras/cam01
    value_template: '{{ value_json.data.Name == "Rule 1" }}'
    payload_on: Start
    payload_off: Stop
    device_class: motion

but this doesn’t seem to work for me (not sure how to do it actually),
Note that i need to set the sensor based on 2-3 values extracted from the json,

in example (not sure for the syntax):

'{{ value_json.data.Name == "Rule 1" }}' AND '{{ value_json.action == "Start" }}' and '{{ value_json.data.Object.ObjectType== "Humnen" }}'

Would appreciate suggestion/example that help solving this.

A sensor can have one state, and as many entities as you want. With json data you can make one sensor with a state (usually the primary thing you want to track) and put any other interesting/valuable info in as attributes, or you can make multiple sensors each with a state you wish to track.

Try this:

binary_sensor:
  - platform: mqtt
    name: Cam01
    state_topic: camera/cam01
    value_template: "{{ 'ON' if value_json.action == 'Start' else 'OFF' }}"
    json_attributes_topic: camera/cam01
    json_attributes_template: >-
      {"direction":"{{value_json.data.Direction}}",
       "name":"{{value_json.data.Name}}",
       "object_type":"{{value_json.data.Object.ObjectType}}"}

Like your binary_sensor, it uses the JSON payload’s action key to set the state (Start = ON and Stop = OFF). It also extracts three other keys from the payload (Direction, Name, and ObjectType) and stores them as attributes (direction, name, object_type).

Screenshot%20from%202019-09-23%2020-18-02

If you prefer it was a sensor (as opposed to binary_sensor) the configuration is very similar:

sensor:
  - platform: mqtt
    name: Cam01
    state_topic: camera/cam01
    value_template: '{{ value_json.action | lower }}'
    json_attributes_topic: camera/cam01
    json_attributes_template: >-
      {"direction":"{{value_json.data.Direction}}",
       "name":"{{value_json.data.Name}}",
       "object_type":"{{value_json.data.Object.ObjectType}}"}
1 Like

Wow… amazing ,
That exactly what i need/asked …
thanks!

I added the binary_sensor as you mention ,
i do see it getting the expected attribute on event .

I added below automation
which basically i want an automation based on the binary_sensor attribute values (in this case string ) :

 - alias: Cam2 test 
   trigger:
     platform: template
     value_template: "{% if is_state('binary_sensor.cam02_desc.attributes.name', 'Rule 1') %}true{% endif %}" 
   action:
     service: notify.avic
     data:
       message: "Rule 1 trigger:"

it pass code validation but it’s not working/triggered…
Any idea why?
Any suggested in case automation based on binary_sensor attributes?

Thanks again!

Try this:

 - alias: Cam2 test 
   trigger:
     platform: template
     value_template: "{{ state_attr('binary_sensor.cam02_desc', 'name') == 'Rule 1' }}" 
   action:
     service: notify.avic
     data:
       message: "Rule 1 trigger:"

or this (compliments of pnbruckner):

  - alias: Cam2 test
    trigger:
      platform: state
      entity_id: binary_sensor.cam02_desc
    condition:
      condition: template
      value_template: >
        {{ trigger.from_state and
           trigger.to_state.attributes.name !=
           trigger.from_state.attributes.name }}
   action:
     service: notify.avic
     data:
       message: "Rule 1 trigger:"