How can I auto populated list from sensor attributes in a markup card?

Hello Guys,

I wonder if someone could help me, i’m looking to create a markup card with a bulleted list of food that expires within the next 3 days. I am using an integration from Grocy to obtain this information, however, rather than creating sensors for ever food group the integration appears to put the information under the attributes section of one sensor (sensor.grocy_stock)

Is there anyway I can get Home Assistant to scan through all the attributes and return the “name” and “available_amount” of any item with a “best_before_date” of 3 days or less?

hoping to achieve the following:

- <available_amount>x <name>
  • 3x Chicken Breast
  • 2x Protein Bar
  • 4x Onions

Thanks

{% set due_list = state_attr('sensor.grocy_stock', 'products') 
| rejectattr('available_amount', 'eq', 0)
| rejectattr('best_before_date', 'le', (now())|string )
| selectattr('best_before_date', 'le', (now()+timedelta(days=3))|string )
| list %}

{%- if due_list|count > 0 %}
{%- for item in due_list %}
- {{ item.available_amount|int}}x {{item.name}}
{%- endfor %}
{% else %} No products near expiration {% endif %}

Hey,

thanks for the help that has gotten me so close to what I need. 2 questions:

1.) Is there a way to remove the decimal place from the stock count.
2.) The tomato soup listed has actually expired, is there a way to exclude items with an expiry date in the past?

thanks again.

  1. Yes
  2. Yes

I have updated the template in the post above.