Filtering Grocy Stock Sensor by Product Name

I’m trying to filter my Grocy Stock sensor by the product name. I’ve managed to do this in a loop, but I don’t seem to be able to do it in one line.

This works;

{% for product in state_attr('sensor.grocy_stock', 'products') | selectattr('name', 'match', 'Bagel') -%}
  Product ID: {{ }}
{%- endfor %}

But this doesn’t;

{{ state_attr('sensor.grocy_stock', 'products') | selectattr('', 'match', 'Bagel') }}

I sort of understand why, but is what I am trying to do possible?

You need to get rid of the .attributes and tell it how to output the generator object

{{state_attr('sensor.grocy_stock', 'products') 
| selectattr('name', 'match', 'Bagel') 
| list }}

FWIW, if it’s possible that you have items with bagel uncapitalized or somewhere other than the first word of the name field, you should use | selectattr('name', 'match', '.*bagel*.', 'i')

Fantastic! Thank you, I had tried the above but without the ‘list’ part… that was the missing ingredient.