Looking for help parsing a State

This might not work because I suspect that if there was 2 trucks, the string “truck:” will show up 3 times. There might be a split function that can take the last string instead of the 2nd.

this should work, -1 is the last string

{{ states.image_processing.tensorflow_back_gate.state.split(’“truck”:’)[-1 ][:2] | int + states.image_processing.tensorflow_back_gate.state.split(’“car”:’)[-1 ][:2] | int }}

Close, but with this

matches: {
  "car": [
    {
      "score": 95.04094123840332,
      "box": [
        0.20254577696323395,
        0.5886361002922058,
        0.32794591784477234,
        0.6868645548820496
      ]
    },
    {
      "score": 93.7804102897644,
      "box": [
        0.18755415081977844,
        0.46359583735466003,
        0.3254301846027374,
        0.5525462031364441
      ]
    }
  ]
}
summary: {
  "car": 2
}
total_matches: 2
friendly_name: TensorFlow back_gate

this

{{ states.image_processing.tensorflow_back_gate.state.split(’“truck”:’)[-1 ][:2] | int + states.image_processing.tensorflow_back_gate.state.split(’“car”:’)[-1 ][:2] | int }}

returned a value of 4

It’s easier to count the matches then to add the summery.

{{ state_attr('image_processing.tensorflow_back_gate', 'matches') | select('in', ['car','bicycle','truck','van','motorcycle', 'person','dog','cat']) | select('eq','score') | list | length }}

EDIT: That first template is for a single level dictionary. You have a multi-level. This will take some finagling.

{%- macro do(d) %}
{%- for k, v in d.items() %}
{%- for v2 in v %}
{%- set k2, v2 = v2 %}
{{- k }}{{ ',' if not loop.last else '' }}
{%- endfor %}
{{- ',' if not loop.last else '' }}
{%- endfor %}
{%- endmacro %}
{{ do(state_attr('image_processing.tensorflow_back_gate', 'matches')).split(',') | length }}
1 Like

I was just about to come here and comment that your first line seems to work, but it’s a bit hard to test since I dont always have cars, trucks, people, or dogs in the camera. haha. Thank you for taking the time to help, this looks great.

Thanks again, greatly appreciate you taking the time to help me. So the current loop you have, should get a count, but no longer filters based on objects, right?

Shouldn’t it be possible to get the value from the summary section?

summary: {
  "car": 2
}

Then to scan the summary for matches, and then add those #'s?

You can’t count while looping. Jinja limitation. That’s why this ass backwards macro way is required.

Thanks for the info. Sorry for asking the extra questions, so I did use the macro in my template test area and it returned a value, but I’m not understanding how it would only count cars, trucks, etc. but not people. Do I use the macro and the template together somehow?

Could a namespace be used to hold the counter variable for this application? You discussed its use in this thread and I was wondering why it it wouldn’t work here.

I tried that first but it wouldn’t increment. I’m not sure if namespace actually works. :man_shrugging:

Just got an idea. Try this:

{{ state_attr('image_processing.tensorflow_back_gate', 'summary').values() | sum }}

EDIT:

I didn’t realize that was a stipulation. My b.

try this:

{% set summary = state_attr('image_processing.tensorflow_back_gate', 'summary') %}
{{ dict(summary.items() | selectattr(0, 'in', ['car','truck'])).values() | sum }}
1 Like

Thanks, that worked perfectly

I’m doing this

  - service: input_number.set_value
    data_template:
      entity_id: input_number.vehicles_in_driveway
      value:  >-
        {% if states('image_processing.back_yard')| int  > 0  %}
          {% set summary = state_attr('image_processing.back_yard', 'summary') %}
          {{ dict(summary.items() | selectattr(0, 'in', ['car','truck', 'van', 'motorcycle'])).values() | sum }}
        {% else %}
          0
        {% endif %}

When everyone leaves, count the number of vehicles and then I get notified if the number increases.

Thanks!

Let me tell you… that template was finagled.

EDIT: what camera are you using?

Ha ha. I just appreciate the help. I’m using a LaView camera. It’s basically a rebranded Hikvision. Then I’m using the recent tensorflow component. It’s working prettty great. The best part is that I now only get valid alerts for things I care about.

does the camera handle the image processing for item recognition? Or is that software?

Tensorflow does the image analysis. It should work with any HA camera

1 Like

So your post ‘forced me’ into integrating my cameras in to HA, I’ve been procrastinating on that. All that is working great. Now, I’m in the process of implementing Tesnorflow, but before I start I have a few questions for you. Are you running hassio? If so, how did you create a separate docker container for tensorflow? Or how did you integrate it?

I just integrated it. I’m running HA in a python VENV. I followed the step by step instructions that someone posted on here a few months back. Super straightforward.

ah ok, I’ll try and get this docker route working then. I don’t think that method works with hassio.

Got it set up, this stuff is pretty cool. I’m now going to count random things in my house

1 Like