Automation to notify of To-dos due in X days

Hi, I’m working on automation to send reminder notifications when to-dos are upcoming, due and overdue.

It’s bit long, so I won’t include he whole thing.

If I run this Action in Developer Tools:

action: todo.get_items
target:
  entity_id: todo.to_do_list
data:
  status: needs_action

I get this as a response:

todo.to_do_list:
  items:
    - summary: To-do item one
      uid: bfb3c77a-5a86-11ef-a949-e45f01e623b1
      status: needs_action
      due: "2024-09-16"
      description: To-do item one desc.
    - summary: To-do item two
      uid: c58dc326-5a86-11ef-a949-e45f01e623b1
      status: needs_action
      due: "2024-09-23"
      description: To-do item two desc.

Now here’s the action section of my automation:

action:

  - action: todo.get_items
    target:
      entity_id: todo.to_do_list
    data:
      status: needs_action
    response_variable: to_do_list_test

  - condition: '{{ to_do_list_test["todo.to_do_list"]["items"] | count != 0 }}'

  - repeat:
      for_each: '{{ to_do_list_test["todo.to_do_list"]["items"] }}'
      sequence:

        # Tests whether repeat.item due date is in X days
        - if:
            - >-
              {% set due_date = expand(repeat.item)[0].due %}
              {% set now_date = (now()).strftime ("%Y-%m-%d") %}
              {{ (due_date | as_datetime) > (now_date | as_datetime) and 
                 (due_date | as_datetime) - (now_date | as_datetime) == timedelta(days=10) }}

          then:
            - action: notify.all_ios_devices
              data:
                title: '{{ expand(repeat.item)[0].summary }}'
                message: Reminder, this to-do is due in X days.

I’m seeing an error in traces when I test this:

Error: In 'template' condition: UndefinedError: list object has no element 0

So am pretty sure the problem is here: expand(repeat.item)[0].due but I’m not sure how I should change this.

Any assistance offered appreciated, thank you!

repeat.item will receive each item one by one. ie, the first iteration will get

    - summary: To-do item one
      uid: bfb3c77a-5a86-11ef-a949-e45f01e623b1
      status: needs_action
      due: "2024-09-16"
      description: To-do item one desc.

so to get ‘due’ instead of expand(repeat.item)[0].due, i think you want

repeat.item['due']

same with

repeat.item['summary']

and so forth…

Thank you, I’ve tried that and I just get a similar error:

Error: In 'template' condition: UndefinedError: 'list object' has no attribute 'due'

could you go to the traces, download it from home assistant then upload it here?

also post the full verbatim code that you have

Sure, no problem:

Full automation:

- id: '9493688666'
  alias: 'Cat to-do reminders'
  trigger:
    - platform: state
      entity_id: sensor.person1_iphone_battery_state
      to: 'Not Charging'
      id: person1
      for:
        minutes: 5
    - platform: state
      entity_id: sensor.person2_iphone_battery_state
      to: 'Not Charging'
      id: person2
      for:
        minutes: 5
  mode: parallel
  condition:
    - condition: time
      after: '06:00:00'
      before: '11:00:00'
    - '{{ is_state("input_boolean." + trigger.id + "_good_morning", "off") }}'
    - '{{ is_state("person." + trigger.id, "home") }}'

  action:

    - action: todo.get_items
      target:
        entity_id: todo.pet_care
      data:
        status: needs_action
      response_variable: pet_care_to_do

    - condition: '{{ pet_care_to_do["todo.pet_care"]["items"] | count != 0 }}'

    - repeat:
        for_each: '{{ pet_care_to_do["todo.pet_care"]["items"] }}'
        sequence:

          # Tests whether repeat.item due date is in 10 days
          - if:
              - >-
                {% set due_date = expand(repeat.item)["due"] %}
                {% set now_date = (now()).strftime ("%Y-%m-%d") %}
                {{ (due_date | as_datetime) > (now_date | as_datetime) and 
                   (due_date | as_datetime) - (now_date | as_datetime) == timedelta(days=10) }}

            then:
              - action: notify.all_ios_devices
                data:
                  title: '{{ expand(repeat.item)["summary"] }}'
                  message: Reminder, this to-do is due in 10 days.

And here’s the trace:

{
  "trace": {
    "last_step": "action/2/repeat/sequence/0/if/condition/0",
    "run_id": "7b4fe4129064100d062bb883e140a3c7",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2024-08-15T07:43:38.232940+00:00",
      "finish": "2024-08-15T07:43:38.249979+00:00"
    },
    "domain": "automation",
    "item_id": "9493688666",
    "trigger": null,
    "trace": {
      "trigger": [
        {
          "path": "trigger",
          "timestamp": "2024-08-15T07:43:38.233242+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.cat_to_do_reminders",
              "state": "on",
              "attributes": {
                "id": "9493688666",
                "last_triggered": "2024-08-15T01:54:32.747827+00:00",
                "mode": "parallel",
                "current": 0,
                "max": 10,
                "friendly_name": "Cat to-do reminders"
              },
              "last_changed": "2024-08-15T07:43:29.894429+00:00",
              "last_reported": "2024-08-15T07:43:29.894429+00:00",
              "last_updated": "2024-08-15T07:43:29.894429+00:00",
              "context": {
                "id": "01J5AFVR360VPPHQZMYDQ1N2BA",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "platform": null
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2024-08-15T07:43:38.234322+00:00",
          "changed_variables": {
            "context": {
              "id": "01J5AFW07RKMDFPWKE4HMDRAM4",
              "parent_id": "01J5AFW07RSQPFW2003GVX3JPG",
              "user_id": null
            },
            "pet_care_to_do": {
              "todo.pet_care": {
                "items": [
                  {
                    "summary": "Cat flea treatment",
                    "uid": "bfb3c77a-5a86-11ef-a949-e45f01e623b1",
                    "status": "needs_action",
                    "due": "2024-09-16",
                    "description": "Treat Yuna for fleas."
                  },
                  {
                    "summary": "Cat worm treatment",
                    "uid": "c58dc326-5a86-11ef-a949-e45f01e623b1",
                    "status": "needs_action",
                    "due": "2024-09-23",
                    "description": "Treat Yuna for worms."
                  }
                ]
              }
            }
          },
          "result": {
            "params": {
              "domain": "todo",
              "service": "get_items",
              "service_data": {
                "status": "needs_action",
                "entity_id": [
                  "todo.pet_care"
                ]
              },
              "target": {
                "entity_id": [
                  "todo.pet_care"
                ]
              }
            },
            "running_script": false
          }
        }
      ],
      "action/1": [
        {
          "path": "action/1",
          "timestamp": "2024-08-15T07:43:38.240825+00:00",
          "result": {
            "result": true,
            "entities": []
          }
        }
      ],
      "action/2": [
        {
          "path": "action/2",
          "timestamp": "2024-08-15T07:43:38.241892+00:00"
        }
      ],
      "action/2/repeat/sequence/0": [
        {
          "path": "action/2/repeat/sequence/0",
          "timestamp": "2024-08-15T07:43:38.243518+00:00",
          "changed_variables": {
            "repeat": {
              "first": true,
              "index": 1,
              "last": false,
              "item": {
                "summary": "Cat flea treatment",
                "uid": "bfb3c77a-5a86-11ef-a949-e45f01e623b1",
                "status": "needs_action",
                "due": "2024-09-16",
                "description": "Treat Yuna for fleas."
              }
            }
          }
        },
        {
          "path": "action/2/repeat/sequence/0",
          "timestamp": "2024-08-15T07:43:38.247031+00:00",
          "changed_variables": {
            "repeat": {
              "first": false,
              "index": 2,
              "last": true,
              "item": {
                "summary": "Cat worm treatment",
                "uid": "c58dc326-5a86-11ef-a949-e45f01e623b1",
                "status": "needs_action",
                "due": "2024-09-23",
                "description": "Treat Yuna for worms."
              }
            }
          }
        }
      ],
      "action/2/repeat/sequence/0/if": [
        {
          "path": "action/2/repeat/sequence/0/if",
          "timestamp": "2024-08-15T07:43:38.244051+00:00",
          "result": {
            "result": null
          }
        },
        {
          "path": "action/2/repeat/sequence/0/if",
          "timestamp": "2024-08-15T07:43:38.247186+00:00",
          "result": {
            "result": null
          }
        }
      ],
      "action/2/repeat/sequence/0/if/condition/0": [
        {
          "path": "action/2/repeat/sequence/0/if/condition/0",
          "timestamp": "2024-08-15T07:43:38.244141+00:00",
          "error": "In 'template' condition: UndefinedError: 'list object' has no attribute 'due'"
        },
        {
          "path": "action/2/repeat/sequence/0/if/condition/0",
          "timestamp": "2024-08-15T07:43:38.247255+00:00",
          "error": "In 'template' condition: UndefinedError: 'list object' has no attribute 'due'"
        }
      ]
    },
    "config": {
      "id": "9493688666",
      "alias": "Cat to-do reminders",
      "trigger": [
        {
          "platform": "state",
          "entity_id": "sensor.person1_iphone_battery_state",
          "to": "Not Charging",
          "id": "tom",
          "for": {
            "minutes": 5
          }
        },
        {
          "platform": "state",
          "entity_id": "sensor.person2_iphone_battery_state",
          "to": "Not Charging",
          "id": "teague",
          "for": {
            "minutes": 5
          }
        }
      ],
      "mode": "parallel",
      "condition": [
        {
          "condition": "time",
          "after": "06:00:00",
          "before": "11:00:00"
        },
        "{{ is_state(\"input_boolean.\" + trigger.id + \"_good_morning\", \"off\") }}",
        "{{ is_state(\"person.\" + trigger.id, \"home\") }}"
      ],
      "action": [
        {
          "action": "todo.get_items",
          "target": {
            "entity_id": "todo.pet_care"
          },
          "data": {
            "status": "needs_action"
          },
          "response_variable": "pet_care_to_do"
        },
        {
          "condition": "{{ pet_care_to_do[\"todo.pet_care\"][\"items\"] | count != 0 }}"
        },
        {
          "repeat": {
            "for_each": "{{ pet_care_to_do[\"todo.pet_care\"][\"items\"] }}",
            "sequence": [
              {
                "if": [
                  "{% set due_date = expand(repeat.item)[\"due\"] %} {% set now_date = (now()).strftime (\"%Y-%m-%d\") %} {{ (due_date | as_datetime) > (now_date | as_datetime) and \n   (due_date | as_datetime) - (now_date | as_datetime) == timedelta(days=10) }}"
                ],
                "then": [
                  {
                    "action": "notify.all_ios_devices",
                    "data": {
                      "title": "{{ expand(repeat.item)[\"summary\"] }}",
                      "message": "Reminder, this to-do is due in 10 days."
                    }
                  }
                ]
              }
            ]
          }
        }
      ]
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01J5AFW07RKMDFPWKE4HMDRAM4",
      "parent_id": "01J5AFW07RSQPFW2003GVX3JPG",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Cat to-do reminders",
      "message": "triggered",
      "source": null,
      "entity_id": "automation.cat_to_do_reminders",
      "context_id": "01J5AFW07RKMDFPWKE4HMDRAM4",
      "when": 1723707818.2333784,
      "domain": "automation"
    }
  ]
}

above I said:

but you didn’t do that?

Sorry I’m not following, I have:

expand(repeat.item)["due"]

Do you mean I should remove expand ?

Yes no expand

Thanks so much, that worked - sorry for my misunderstanding!

1 Like

great! glad it’s working for you!