Help needed for a binary sensor

Hi all,

I am trying to create a binary sensor from a mqtt topic
the topic message is a json formated text.

What am i doing wrong here? Please advice

    {
    "fileName": "e:\\my\\desktop\\default_123.jpg",
    "baseName": "default_123.jpg",
    "summary": "Person (90%); Person (84%); Person (72%); Eve (72%); Nick (68%); Mitchell (74%); Sander (67%); Potted Plant (79%)",
    "state": "on",
    "analysisDurationMS": 1658,
    "time": "2021-04-06T20:53:20.6519257+02:00",
    "camera": "Default",
    "predictions": [
        {
            "label": "Person",
            "detail": "",
            "userID": "",
            "confidence": 90.498084,
            "y_min": 196,
            "x_min": 698,
            "y_max": 869,
            "x_max": 1174,
            "server": "Deepstack_Objects:192.168.178.11:80"
        },
        {
            "label": "Person",
            "detail": "",
            "userID": "",
            "confidence": 83.931416,
            "y_min": 422,
            "x_min": 343,
            "y_max": 874,
            "x_max": 622,
            "server": "Deepstack_Objects:192.168.178.11:80"
        },
        {
            "label": "Person",
            "detail": "",
            "userID": "",
            "confidence": 71.93728700000001,
            "y_min": 223,
            "x_min": 542,
            "y_max": 871,
            "x_max": 869,
            "server": "Deepstack_Objects:192.168.178.11:80"
        },
        {
            "label": "Eve",
            "detail": "",
            "userID": "",
            "confidence": 72.01924,
            "y_min": 159,
            "x_min": 462,
            "y_max": 296,
            "x_max": 561,
            "server": "Deepstack_Faces:192.168.178.11:80"
        },
        {
            "label": "Nick",
            "detail": "",
            "userID": "",
            "confidence": 68.0404,
            "y_min": 463,
            "x_min": 386,
            "y_max": 583,
            "x_max": 489,
            "server": "Deepstack_Faces:192.168.178.11:80"
        },
        {
            "label": "Mitchel",
            "detail": "",
            "userID": "",
            "confidence": 74.34667,
            "y_min": 328,
            "x_min": 644,
            "y_max": 461,
            "x_max": 749,
            "server": "Deepstack_Faces:192.168.178.11:80"
        },
        {
            "label": "Sander",
            "detail": "",
            "userID": "",
            "confidence": 67.338735,
            "y_min": 341,
            "x_min": 903,
            "y_max": 554,
            "x_max": 1062,
            "server": "Deepstack_Faces:192.168.178.11:80"
        },
        {
            "label": "Potted Plant",
            "detail": "",
            "userID": "",
            "confidence": 79.16054,
            "y_min": 1,
            "x_min": 0,
            "y_max": 603,
            "x_max": 400,
            "server": "Deepstack_Objects:192.168.178.11:80"
        }
    ]
          } %}
- platform: mqtt
  name: "Sander is Home"
  state_topic: "ai/Default/motion/"
  value_template: >
    {% set ns = namespace(found = false) %}
    {%  for match in camera_json.predictions %}
    {% if match.label == 'Sander' and match.confidence >=10 %}
        {% set ns.found = true %}
        ON
    {% endif %}
    {% endfor %}
  off_delay: 5
  device_class: motion
  payload_on: "ON"

Is anything actually going to your MQTT broker?
You didn’t give any information as to what happens or does not.
Try MQTT Explorer to see if you are actually publishing to your broker.
What you included looks like what you expect for JSON to MQTT but is that input or actual output.
What is the code to publish to MQTT? (not needed if you actually have data on Broker)
I do see there is a spacing problem with your ‘for’ statement. Your indentions don’t seen right either.

Try this version:

- platform: mqtt
  name: Sander is Home
  state_topic: ai/Default/motion/
  value_template: >
    {% set x = value_json.predictions | selectattr('label', 'eq', 'Sander')
               | selectattr('confidence', 'gt', 10) | list | count %}
    {{ 'ON' if x > 0 else 'OFF' }}
  off_delay: 5
  device_class: motion

EDIT

Correction. Fixed typo. Changed prediction to predictions.

With a little adjustment on value_json.prediction → value_json.predictions it worked
thx @123

1 Like