In another episode of "Guess That Jinja Template...."

Been butting my head against a wall on this one for the better part of a couple of hours.

I need to match the word “car” in the sensor attribute “targets_found”:

From an answer a community member gave me to previous similar post I thought this might work:

{{ states('image_processing.deepstack_object_driveway') |selectattr('targets_found', 'match', 'car') |list|length > 0}} 

But it still comes back as false and I haven’t been able to crack the code (so punny!).

Anyone have a suggestoin?

Does this help?

{% set sensor = {
  "targets_found": [{"car": 1.1}, {"truck": 2.2}]
}
%}

{{ sensor }}

{{ sensor.targets_found | selectattr('car') | list }} 

{{ sensor.targets_found | selectattr('car') | list | length > 0 }}
{'targets_found': [{'car': 1.1}, {'truck': 2.2}]}

[{'car': 1.1}] 

True

(Sorry for brevity. On phone.)

Why not:

{{ 'car' in state_attr('image_processing.deepstack_objects_driveway','targets_found') }}

{{ state_attr('image_processing.deepstack_object_driveway','targets_found') is search('car') }}


EDIT

Correction. Changed ‘objects’ to ‘object’.

I get:

TypeError: argument of type ‘NoneType’ is not iterable

I still get a false match with that one.

Paste this into the Template Editor and post the result:

{{ state_attr('image_processing.deepstack_objects_driveway','targets_found') }}

It should look something like this (with appropriate values, of course):

[{'car': 1.1}, {'truck': 2.2}]

{{ state_attr('image_processing.deepstack_object_driveway','targets_found') is search('car') }}

Actually this DID work. I just noticed you had an ‘s’ in objects where there isn’t one in the sensor.

Cheers!

Sorry, my mistake. Thanks for pointing it out. I have corrected it in my post.

TIL: It’s the first time I see a search function and I can’t find it in the jinja2 docs. Where is this documented?

In Home Assistant’s documentation, in the Templating under Regular Expressions. Both search and match are recent additions (in 2021.5.0).

1 Like