MQTT Message Data Count

I have cameras that are sending the following MQTT data…

{"outputs":[{"class":0,"id":1021,"location":[0.7,0.502,0.841,0.604],"score":0.954},{"class":0,"id":1022,"location":[0.451,0.527,0.605,0.615],"score":0.846},{"class":0,"id":1023,"location":[0.645,0.353,0.778,0.451],"score":0.763},{"class":0,"id":1024,"location":[0.412,0.597,0.547,0.69],"score":0.716},{"class":0,"id":1025,"location":[0.354,0.241,0.489,0.337],"score":0.624},{"class":0,"id":1026,"location":[0.273,0.641,0.433,0.735],"score":0.582}],"timestamp":1648405710865}

I would like to get the number of class 0 ID’s that are in that message. In the above example I would like to setup an mqtt sensor that displays / returns the number 6.

Here is what I have now in my configuration.yaml sensor but it is not returning anything

value_template: '{{ len(value_json.outputs) }}'
  unit_of_measurement: "class"

I would really appreciate some help or some advice on how to extract that data.

Thanks

value_template: "{{ value_json.outputs | selectattr('class', 'eq', 0) | list | count }}"

Thank you so much. This works great!

1 Like