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.
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.