Help configuring MQTT Sensor

Hi,

I’m trying to read a value from the events in Frigate. I only want to read the score when a certain object is detected.

I know I need an if statement, but unfortunately my limited knowledge means I’m stuck on the code for pulling the score.

I would like to read the score when the label is car:

image

{
  "after": {
    "id": "1723146376.506911-uxeqh0",
    "camera": "dahua",
    "frame_time": 1723147455.127638,
    "snapshot": {
      "frame_time": 1723146972.418351,
      "box": [
        1028,
        3,
        1277,
        196
      ],
      "area": 48057,
      "region": [
        484,
        0,
        1280,
        796
      ],
      "score": 0.76953125,
      "attributes": []
    },
    "label": "car",
    "sub_label": null,
    "top_score": 0.859375,
    "false_positive": false,
    "start_time": 1723146376.506911,
    "end_time": null,
    "score": 0.6171875,
    "box": [
      1050,
      4,
      1279,
      212
    ],
    "area": 47632,
    "ratio": 1.1009615384615385,
    "region": [
      960,
      0,
      1280,
      320
    ],
    "stationary": true,
    "motionless_count": 5368,
    "position_changes": 0,
    "current_zones": [],
    "entered_zones": [],
    "has_clip": false,
    "has_snapshot": false,
    "attributes": {},
    "current_attributes": []
  },
  "type": "update"
}

I managed to work it out

mqtt:
  - sensor:
      name: Dahua Car Score
      state_topic: "frigate/events"
      unit_of_measurement: "%"
      value_template: |-
        {% if value_json['after']['label'] == 'car' and value_json['after']['camera'] == 'dahua' %}
          {{ (value_json['after']['score'] | float * 100) | round (1) }}
        {% else %}
          {{ '0' }}  
        {% endif %}
1 Like