Help with template for sensor

Hi,

I want to define a sensor for count how many “big cars” are there from a camera using tensorflow (the reason of big car is because I have defined an specific area for tensorflow and I want to count how many cars are parked in this area).

At the moment I have this template:

       {% set value_json=states. image_processing.tensorflow_pruebas_calle.attributes.matches %}
            {% set coche_gordo = 0 %}
        {% for value in value_json %}
          {% if value == 'car' %}
            {% for car in value_json['car'] %}
              {% if (float(car['box'][3]) - float(car['box'][1])) > 0.4 %}
                Big Car
              {% endif %}
            {% endfor %}
          {% endif %}
        {% endfor %}

With this template, I get one “Big Car” string for each car, but I would like to have a count of this cars … I have tested with “set” using a count variable but doesn’t work like I want.

Thanks for your support!

You gotta show us what the structure looks like for us to help. Anyways, I’m going to assume it’s similar to my structure.

{%- macro bigCarList() %}
{%- set value_json = state_attr('image_processing.tensorflow_pruebas_calle', 'matches') %}
{%- for car in value_json.car %}
{{- (car.box[3] - car.box[1]) > 0.4 }}{{ '' if loop.last else ',' }}
{%- endfor %}
{%- endmacro %}
{{ bigCarList().split(',') | reject('eq','False') | list | count}}

This makes a macro that returns a string of true/falses. I.E. 'True,False,True,False,False'. Then it splits the string on the comma to get a list of strings. I.E. ['True','False','True','False','False']. After that, it rejects any string in the list that is equal to false. So we end up with a shorter list of just trues. I.E. ['True','True']. Lastly, we count the number of items in the list, which will be the number of times that this statement returned true (car.box[3] - car.box[1]) > 0.4.

2 Likes

Thanks for your support @petro!!! This template is working :slight_smile:

@petro testing the macro, always return “1” for example with this input:

matches: {
  "car": [
    {
      "score": 83.17613005638123,
      "box": [
        0.439372181892395,
        0.9287927746772766,
        0.5033652186393738,
        0.9712551236152649
      ]
    }
  ]
}
summary: {
  "car": 1
}
total_matches: 1
friendly_name: TensorFlow calle

Should return 0

It’s returning zero for me with that.

Are there errors in the logs?

Hi @petro,

I have found when is returning “1”, with this input:

matches: {}
summary: {}
total_matches: 0
friendly_name: TensorFlow calle

Thanks for your support!!

use this

{%- macro bigCarList() %}
{%- set value_json = state_attr('image_processing.tensorflow_pruebas_calle', 'matches') %}
{%- for car in value_json.car %}
{{- (car.box[3] - car.box[1]) > 0.4 }}{{ '' if loop.last else ',' }}
{%- endfor %}
{%- endmacro %}
{{ bigCarList().split(',') | reject('eq','False') | reject('eq','') | list | count}}

Thanks very much @petro for your support! u’re a template master!! :+1: