Sending a telegram message containing a data from a todo response variable

Hi,

I have a todo list that I’ve populated with books and their release dates.

My goal is to have an automation run each morning that checks the list and if any books in my list have been released (based on the due field) I’m then sent a telegram message with the title (summary field).

My problem is between getting the list and sending the telegram message.

The structure that the response variable following follows, but I’m at a loss to understand how I:

  1. reference the data within the variable
  2. compare the ‘due’ with the current date
    I’ve given it a stab, but success has been low…
books:
  todo.books:
    items:
      - summary: yesterday test book
        uid: e4eea50a-5b85-11ef-a3bb-2ccf6718c332
        status: needs_action
        due: '2024-08-15'
alias: Notify - Books Released
description: ""
trigger:
  - platform: time
    at: "09:00:00"
condition: []
action:
  - action: todo.get_items
    metadata: {}
    data:
      status: needs_action
    target:
      entity_id: todo.books
    response_variable: books
  - action: telegram_bot.send_message
    metadata: {}
    data:
      target: 6XXXXXXXXX
      message: |-
        {% for each in books %}
          {% if each[3] <= now() %}
            <li>{{ each[0] }}</li> 
          {% endif %}
        {% endfor %}
mode: single

Any ideas?

Thanks!
Sam

don’t have your dataset so this isn’t tested code, but it should show you the right approach i think.

alias: Notify - Books Released
description: ""
trigger:
  - platform: time
    at: "09:00:00"
condition: []
action:
  - action: todo.get_items
    metadata: {}
    data:
      status: needs_action
    target:
      entity_id: todo.books
    response_variable: books
  - action: telegram_bot.send_message
    metadata: {}
    data:
      target: 6XXXXXXXXX
      message: |-
        {% for each in books['todo.books']['items'] %}
          {% if each.due | string <= now().date() %}
            <li>{{ each.summary }}</li> 
          {% endif %}
        {% endfor %}
mode: single

Thank you, that solves the first issue I had! With the below I can now get a telegram message with the book name and release date. Big step forward :smiley:

action: telegram_bot.send_message
metadata: {}
data:
  target: 6XXXXXXXX
  message: |-
    {% for each in books['todo.books']['items'] %}
      {{ each.summary }}
      {{ each.due }}
    {% endfor %}

When I add in the date test I get the following error though:

Error rendering data template: TypeError: ‘<=’ not supported between instances of ‘str’ and ‘builtin_function_or_method’

Any ideas on that one?

Thanks very much for the response and help, it’s very much appreciated!

sorry, should be now().date(). i forgot the () on date. i’m fixing it above.

I got the same error with that change, I asked Google Gemini for some ideas and it suggested the below. This seems to work well although the message formatting is a bit weird (multi-line space between books and the 2nd row has a leading space). If you have any ideas on that I’d be interested to hear them. Otherwise, thanks for your help solving this!

    {% for each in books['todo.books']['items'] %}
      {% if as_timestamp(each.due) <= now().timestamp() %}
        {{ each.summary }} - Released {{ each.due }}
      {% endif %}
    {% endfor %}

huh… well, converting to timestamps is certainly a fine option.

something you can always test is putting the template into the dev-tools->template page… if you put:

{{ now().date() }}

this should give you today’s date. i supposed you could force a cast to string:

{{ each.due | string <=  now().date() | string }}

now they are both forced strings. so if you’re getting that same error, there’s something else fishy going on. but regardless, converting to timestamp is also totally valid.