Template assistance for changing attributes

I’m trying to work a template sensor for tensorflow. Bottom line, when an object is detected it can have car, truck, person, etc in the attributes, but when nothing is detected there are no attributes. This causes the template to fail. I could create a template for each situation, but that ends up being a lot of templates.

Anyway
This template works (still errors when nothing is detected, but turns on when a person is detected)

     person_in_driveway:
       friendly_name: Person in Drivway
       value_template: "{{ (states.image_processing.tensorflow_front_door.attributes.matches.person[0].score) | float >= 80 }}"
       entity_id: image_processing.tensorflow_front_door

This one does not work (unless a truck is detected)

     vehicle_in_driveway:
       friendly_name: Vehicle in Driveway
       value_template: "{{ (states.image_processing.tensorflow_front_door.attributes.matches.truck[0].score) | float >= 80 or (states.image_processing.tensorflow_front_door.attributes.matches.car[0].score) | float >= 80 or (states.image_processing.tensorflow_front_door.attributes.matches.bike[0].score) | float >= 80  }}"
       entity_id: image_processing.tensorflow_front_door

I guess I need to parse, but not sure how to do so.

What does states.image_processing.tensorflow_front_door.attributes look like when a person, truck, etc. are not detected? Does states.image_processing.tensorflow_front_door.attributes.matches still exist? Do states.image_processing.tensorflow_front_door.attributes.matches.person, states.image_processing.tensorflow_front_door.attributes.matches.truck still exist, and if so, are they still lists, etc.???

When nothing is detected it looks like this

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

And here is an example with a car detected

matches: {
  "car": [
    {
      "score": 94.47455406188965,
      "box": [
        0.21317942440509796,
        0.5704055428504944,
        0.33125826716423035,
        0.6765897870063782
      ]
    }
  ]
}
summary: {
  "car": 1
}
total_matches: 1
friendly_name: TensorFlow back_gate

If there had been a person and a car, or a truck and a car, those would also show up under matches

Edit: Here is the error it throws when nothing (person in this case) is detected

Could not render template Person in Backyard: UndefinedError: 'dict object' has no attribute 'person'

Ok, then try this:

     person_in_driveway:
       friendly_name: Person in Drivway
       value_template: >
         {% set m = state_attr('image_processing.tensorflow_front_door', 'matches') %}
         {{ m.person is defined and (m.person[0].score) | float >= 80 }}
       entity_id: image_processing.tensorflow_front_door

Nice, that does work. thanks. Using your template I built this one too

     vehicle_in_driveway:
       friendly_name: Vehicle in Driveway
       value_template: >
         {% set m = state_attr('image_processing.tensorflow_front_door', 'matches') %}
         {{ m.truck is defined and (m.truck[0].score) | float >= 80 or m.car is defined and (m.car[0].score) | float >= 80 or m.bike is defined and (m.bike[0].score) | float >= 80 }}
       entity_id: image_processing.tensorflow_front_door

Thanks Again, awesome stuff

1 Like