Deep Stack value template

Hello,

can someone help me how to make a sensor which detects if a person has been detected from the deep stack image state?

I am trying to make an automation which will notify me of detection in particular hours.

So far:

{{is_state_attr('image_processing.deepstack_object_network_video_recorder_profilename010', 'targets_found', 'person', 1 ) }}

which gives the below error:

TypeError: is_state_attr() takes 4 positional arguments but 5 were given

even if this didn’t error it still wouldn’t achieve the desired outcome of wanting to know if the value is above zero so i’m stuck :roll_eyes:

below is the state attributes for the entity I want to use.

> targets:
>   - target: person
>     confidence: 25
>   - target: vehicle
>     confidence: 40
>   - target: car
>     confidence: 10
>   - target: dog
>     confidence: 40
>   - target: cat
>     confidence: 40
>   - target: bottle
>     confidence: 10
>   - target: potted plant
>     confidence: 20
>   - target: cell phone
>     confidence: 20
> targets_found:
>   - car: 11.951
>   - car: 50.019
>   - bus: 52.972
>   - person: 82.704
> summary:
>   car: 2
>   bus: 1
>   person: 1
> last_target_detection: 2021-04-25_22-34-34
> all_objects:
>   - truck: 11.449
>   - car: 11.951
>   - person: 18.432
>   - boat: 19.095
>   - train: 38.718
>   - boat: 40.049
>   - car: 50.019
>   - bus: 52.972
>   - boat: 71.606
>   - person: 82.704
> save_file_folder: /config/snapshots
> save_file_format: png
> save_timestamped_file: true
> always_save_latest_file: true
> unit_of_measurement: targets
> friendly_name: deepstack_object_network_video_recorder_profilename010

Thanks

That’s kind of a weird set of attributes. But maybe something like this will do what you’re looking for:

{% set targets_found =
     state_attr('image_processing.deepstack_object_network_video_recorder_profilename010',
                'targets_found') %}
{% set ns = namespace(person = 0) %}
{% for item in targets_found if (item.keys()|list)[0] == 'person' %}
  {% set person = (item.values()|list)[0] %}
  {% if person > ns.person %}
    {% set ns.person = person %}
  {% endif %}
{% endfor %}
{{ ns.person > 0 }}

Thanks Phil, that works great and returns a true or false letting me know if a person is detected in the image.

I have edited the number in the last line to only return true if the confidence level is over 70%.

{% set targets_found =
     state_attr('image_processing.deepstack_object_network_video_recorder_profilename010',
                'targets_found') %}
{% set ns = namespace(person = 0) %}
{% for item in targets_found if (item.keys()|list)[0] == 'person' %}
  {% set person = (item.values()|list)[0] %}
  {% if person > ns.person %}
    {% set ns.person = person %}
  {% endif %}
{% endfor %}
{{ ns.person > 70 }}

As a further step - how would I manipulate this to read the ‘summary’ attribute and return the number of persons? It seems to be formatted slightly differently to the other attributes.

First, when you reply, if you want the person you’re replying to to be notified, use the Reply button under their post, not the one at the bottom of the page.

That one is much simpler and you don’t need anything as complicated as above. It’s simply:

{{ state_attr('image_processing.deepstack_object_network_video_recorder_profilename010',
              'summary').person }}

I wonder if someone could help with creating a value template to determine whether a target was detected within the previous 30s. I’m hoping to use this as a condition to throttle notifications when a target is detected. So far i have the following:

  {{ (as_timestamp(now()) -
  as_timestamp(state_attr('image_processing.deepstack_object_ip_camera_north',
              'last_target_detection')
  | default(0)) | int > 30)}}

however this generates the following error in the template editor:

ValueError: Template error: as_timestamp got invalid input '2022-07-06_20-31-46-892361' when rendering template '{{ (as_timestamp(now()) -
  as_timestamp(state_attr('image_processing.deepstack_object_ip_camera_north',
              'last_target_detection')
  | default(0)) | int > 30)}}' but no default was specified

Sharing the link to a post which resolves my above issue. There’s some interesting stuff around utilising the Deepstack entity attributes and creating a value template. Worth a read.

https://community.home-assistant.io/t/value-template-using-time-from-entity-attribute/438225?u=cergon

any ideas how to make this return a count of the people?

If your question is, how to retrieve the value for person under summary in the original post above, then:

{% set summary = state_attr('image_processing.XXX', 'summary') %}
{% if summary is not none %}
  {{ summary.person }}
{% else %}
  unknown
{% endif %}
1 Like