Configuring template sensors for Grocy chores

I’m using the Grocy integration (not the add-in), and so I have an entity with attributes that contain all of the data for all of the chores I’ve set up (19 so far):

- id: 6
  name: Change oil in Generac generator
  description: |-
    1.5 quarts of Valvoline 5W-30 full synthetic
    Generac 070185BS or Purolator PL14459 oil filter
  period_type: yearly
  period_config: null
  period_days: 1
  track_date_only: true
  rollover: true
  assignment_type: in-alphabetical-order
  assignment_config: '1'
  next_execution_assigned_to_user_id: 1
  userfields: null
  last_tracked_time: null
  next_estimated_execution_time: '2025-03-24T23:59:59'
  last_done_by: null
  track_count: 0
  next_execution_assigned_user:
    id: 1
    username: xxxxxxx
    first_name: xxxxxxx
    last_name: xxxxxxx
    display_name: xxxxxxx

I have set up a template sensor with a state of true when I have a chore coming due in the next x days (x is fixed at 3, currently):

  - binary_sensor:
    - unique_id: grocy_chores_due_soon
      name: Grocy Chores Due Soon
      state: >
        {% set days = 3 %}
          {{ state_attr('sensor.grocy_chores', 'chores') | map(attribute ='next_estimated_execution_time') | sort | reject('gt', (today_at()+timedelta(days=(days+1)))|string) | list | count > 0 }}

I am looking to set up template sensors that will tell me:

  1. How many chores are due in the next x days
  2. What the names of the chores due in the next x days are (concatenated string, for lack of a better idea)
  3. What the due dates of each of those chores is

I suppose a single template sensor with attributes containing the chore data for only those chores due in the next x days would work. Ultimately, I plan to use these in notifications, so I’m only needing attributes for the three items above. Has anyone done something like this? It would have to be dynamic in nature to ensure new chores added later on aren’t left out.

Thanks :+1:

EDIT: I’ve used another bit of code to get the relevant attributes into JSON… but that’s not what I need:

        {% set ns = namespace(chores=[])%} {% set today = now().date() -%}  
        {% set my_sensor ='sensor.grocy_chores' -%}
        {%- for attr in states[my_sensor].attributes.chores -%}
        {%- set date = strptime(attr.next_estimated_execution_time, '%Y-%m-%dT%H:%M:%S').date()-%}
        {%- if date <= (today) -%}
        {% set ns.chores = ns.chores + [ 
          { 
          'name': attr.name
          }
        ] %} 
        {% endif %}
        {%- endfor %}
          {{ns.chores}}

…which returns the two valid chores:

[
  {
    "name": "Change oil in Generac generator"
  },
  {
    "name": "Renew inspection for xxxxxxxx"
  }
]

…I’m just not sure how to set it as the valid state of a template sensor rather than JSON.

I’ve scrapped this idea in favor of just assigning the count of due/overdue tasks to a template sensor. It’ll be something like this, but because next_estimated_execution_time is a string, it’s not working yet:

{% set today = now().date %}
{{ state_attr('sensor.grocy_chores','chores')
|selectattr('next_estimated_execution_time', '<=', today)
|map(attribute='name')
|list }}