MQTT Sensor Json Config Issue

Hi,

I’ve successfully got my Dahua IP Cameras sending MQTT messages when it detects a tripline is crossed. I can make it so my Google Home says “Front gate trigger” when the tripline is crossed.

My next step is what I’m struggling with. I want a sensor to pull the value from the payload that says “RightToLeft” or “LeftToRight” when the tripline is crossed. That way I’ll know whether someone is coming or going.

This is what the payload looks like (there’s a few messages it sends):

2019-11-28 11:47:44.540741 INFO DahuaMQTT: [192.168.1.201] Publishing MQTT. topic=cameras/1, payload={"code": "CrossLineDetection", "action": "Start", "index": "0", "data": {"Class": "Normal", "DetectLine": [[3225, 7267], [1209, 4171]], "Direction": "LeftToRight", "EventSeq": 158, "FrameSequence": 11937152, "GroupID": 158, "Mark": 0, "Name": "Rule1", "Object": {"Action": "Appear", "BoundingBox": [512, 5472, 4512, 7312], "Center": [2512, 6392], "Confidence": 0, "FrameSequence": 0, "LowerBodyColor": [0, 0, 0, 0], "MainColor": [0, 0, 0, 0], "ObjectID": 18902, "ObjectType": "Human", "RelativeID": 0, "Source": 0.0, "Speed": 0, "SpeedTypeInternal": 0}, "PTS": 43546257240.0, "RuleId": 0, "Source": 42789168.0, "Track": null, "UTC": 1574941632, "UTCMS": 246}}
2019-11-28 11:47:45.618062 INFO DahuaMQTT: [192.168.1.201] Publishing MQTT. topic=cameras/1CrossLineDetection, payload=Start
2019-11-28 11:47:47.147660 INFO DahuaMQTT: [192.168.1.201] Publishing MQTT. topic=cameras/1, payload={"code": "CrossLineDetection", "action": "Stop", "index": "0", "data": {"Class": "Normal", "DetectLine": [[3225, 7267], [1209, 4171]], "Direction": "LeftToRight", "EventSeq": 158, "FrameSequence": 11937152, "GroupID": 158, "Mark": 0, "Name": "Rule1", "Object": {"Action": "Appear", "BoundingBox": [512, 5472, 4512, 7312], "Center": [2512, 6392], "Confidence": 0, "FrameSequence": 0, "LowerBodyColor": [0, 0, 0, 0], "MainColor": [0, 0, 0, 0], "ObjectID": 18902, "ObjectType": "Human", "RelativeID": 0, "Source": 0.0, "Speed": 0, "SpeedTypeInternal": 0}, "PTS": 43546257240.0, "RuleId": 0, "Source": 42789168.0, "Track": null, "UTC": 1574941632, "UTCMS": 246}}
2019-11-28 11:47:48.146845 INFO DahuaMQTT: [192.168.1.201] Publishing MQTT. topic=cameras/1CrossLineDetection, payload=Stop

I have a working MQTT Sensor called GateTrip:

binary_sensor:
  - platform: mqtt
    name: "GateTrip"
    state_topic: "cameras/1"
    payload_on: "Start"
    payload_off: "Stop"
    value_template: '{{ value_json.action }}'
    device_class: motion

I’ve tried to use the same logic and change ‘action’ to ‘Direction’ but it does not yield the same result:

  - platform: mqtt
    name: "Trip_Direction_Incoming"
    state_topic: "cameras/1"
    payload_on: "RightToLeft"
    off_delay: 3
    value_template: '{{ value_json.Direction }}'

It just simply doesn’t trigger.
The log says this:

2019-11-27 06:32:30 WARNING (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Trip_Direction_Outgoing with state_topic: cameras/1
2019-11-27 06:32:30 WARNING (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Trip_Direction_Incoming with state_topic: cameras/1
2019-11-27 06:32:32 ERROR (MainThread) [homeassistant.util.logging] Exception in message_received when handling msg on 'cameras/1': '{"code": "CrossLineDetection", "action": "Stop", "index": "0", "data": {"Class": "Normal", "DetectLine": [[3225, 7267], [1209, 4171]], "Direction": "RightToLeft", "EventSeq": 120, "FrameSequence": 10586864, "GroupID": 120, "Mark": 0, "Name": "Rule1", "Object": {"Action": "Appear", "BoundingBox": [296, 3016, 3704, 8024], "Center": [2000, 5520], "Confidence": 0, "FrameSequence": 0, "LowerBodyColor": [0, 0, 0, 0], "MainColor": [0, 0, 0, 0], "ObjectID": 10342, "ObjectType": "Human", "RelativeID": 0, "Source": 0.0, "Speed": 0, "SpeedTypeInternal": 0}, "PTS": 43478742670.0, "RuleId": 0, "Source": 42789168.0, "Track": null, "UTC": 1574874117, "UTCMS": 670}}'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 190, in message_received
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 288, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 394, in _async_write_ha_state
    self.entity_id, state, attr, self.force_update, self._context
  File "/usr/src/homeassistant/homeassistant/core.py", line 988, in async_set
    state = State(entity_id, new_state, attributes, last_changed, None, context)
  File "/usr/src/homeassistant/homeassistant/core.py", line 733, in __init__
    ).format(entity_id)
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.mqtt_sensor. State max length is 255 characters.

So, is it saying that the MQTT payload is too long for it to interpret? Because it doesn’t seem to have an issue with GateTrip.

I also created an MQTT Sensor (Non Binary) hoping it would display the state “RightToLeft” or “LeftToRight” but it always says “unknown”.

sensor:
  - platform: mqtt
    name: "Trip"
    state_topic: "cameras/1"
    value_template: '{{ value_json.direction }}'

I’ve tried both 'Direction" and “direction” in case it was a case issue but I’m not overcoming this.

I’m not particularly handy with this type of thing but I was hoping that it would be a simple matter of changing ‘action’ to ‘direction’ but apparently not.

Any advice would be appreciated.

Thanks

I assume you wish to define an MQTT BInary Sensor whose state is on when Direction is RightToLeft otherwise it is off.

  - platform: mqtt
    name: "Trip_Direction_Incoming"
    state_topic: "cameras/1"
    off_delay: 3
    value_template: "{{ 'ON' if value_json.data.Direction == 'RightToLeft' else 'OFF' }}"

If you wish to define an MQTT Sensor, that reports the two states RightToLeft and LeftToRight then this will do it:

  - platform: mqtt
    name: "Trip_Direction_Incoming"
    state_topic: "cameras/1"
    off_delay: 3
    value_template: "{{ value_json.data.Direction }}"
1 Like