How to send notification when item added to a Todo list?

I am trying to use the new Todo integration to create a shopping list that keeps me and my family notified when someone adds something or clears something on the shopping list.

I am having difficulty getting the name of the item that was added to/cleared on the list.

I am stuck at the part where I try to access the name of last item in the list.

Any advise?

alias: Notify on new shopping list item
trigger:
  - platform: state
    entity_id:
      - todo.shopping_list
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ trigger.to_state.state | int > trigger.from_state.state | int
              }}
        sequence:
          - service: todo.get_items
            target:
              entity_id: todo.shopping_list
            data:
              status: needs_action
            response_variable: new_items
          - service: notify.notify
            data:
              title: Shopping List Updated - Item Added
              message: >-
                {{ new_items['todo.shopping_list'].items | last }} has been
                added to the shopping list.
      - conditions:
          - condition: template
            value_template: >-
              {{ trigger.to_state.state | int < trigger.from_state.state | int
              }}
        sequence:
          - service: notify.notify
            data:
              title: Shopping List Updated - Item Removed
              message: An item has been removed from the shopping list.

It is this piece that I cannot figure out how to solve:

 {{ new_items['todo.shopping_list'].items | last }} has been added to the shopping list.

Because new_items is a string and not a JSON or object, I am not able to access the last item in the list.

1 Like

Here is my working solution - maybe you can pick out the part you need :slight_smile:

alias: New item on shopping list
description: ""
trigger:
  - platform: event
    event_type: shopping_list_updated
    event_data:
      action: add
condition: []
action:
  - service: notify.mobile_app_pixel_6
    data:
      data:
        ttl: 0
        priority: high
        clickAction: /shopping-list/shopping-list
        url: /shopping-list/shopping-list
      title: Shopping List
      message: "{{ trigger.event.data.item.name }} has been added to the shopping list"
  - service: notify.mobile_app_pixel_4a
    data:
      data:
        ttl: 0
        priority: high
        clickAction: /shopping-list/shopping-list
        url: /shopping-list/shopping-list
      title: Shopping List
      message: "{{ trigger.event.data.item.name }} has been added to the shopping list"
    enabled: true
mode: single

Thank you,
But you seem to be using the Shopping list integration, and not the new Todo integration?

Indeed. The shopping list works better as a shopping list. The to-dos are different. Just because they merged.the 2 doesn’t mean you can’t use it

I ended up having weird behavior of my shopping list after the latest upgrade so I decided to remove it. Perhaps I can try with a clean reinstall of it now that Todo is in place.

Thanks

{{ trigger.event.data.item.name }}
This part seems to give errors in my setup:

Error rendering data template: UndefinedError: ‘trigger’ is undefined

Anyone have a solution/ or reason for this?

Knipsel

this is my current yaml. but its not working. only if i remove the trigger event.

Only thing I can see is that I use double quotes instead of single. i.e. " instead of ’

message: "{{ trigger.event.data.item.name }} has been added to the shopping list"

I checked and i got the double quotes now… still not working.

Should the trigger referr to a template? I have the feeling im missing something

I ended up with this automation:

alias: Notify on shopping list item change
trigger:
  - platform: event
    event_type: shopping_list_updated
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.data.action == 'add' }}"
        sequence:
          - service: notify.notify
            data:
              data:
                ttl: 0
                priority: high
                clickAction: /lovelace-mobile/mobile-shopping-list
                url: /lovelace-mobile/mobile-shopping-list
              title: Shopping List
              message: "{{ trigger.event.data.item.name }} added to shopping list"
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.data.action == 'update' }}"
        sequence:
          - service: notify.notify
            data:
              data:
                ttl: 0
                priority: high
                clickAction: /lovelace-mobile/mobile-shopping-list
                url: /lovelace-mobile/mobile-shopping-list
              title: Shopping List
              message: "{{ trigger.event.data.item.name }} cleared from shopping list"
mode: single

Would have liked to also be able to see the name of the person who added/removed the item on the shopping list but I cannot seem to make that work following different ways as described online and from my own testing.

If anyone knows how to add the name of the user who conducted the action please let me know :slight_smile:

Nick,

Did you have to make a template ?
Can you explain a bit more about the value template?

Thanks.

How are you testing your automation? This error most commonly occurs when people erroneously test automations containing the trigger variable using the “Run” button or the automation.trigger service call. Both of those bypass the trigger block so no trigger variable is created. To test your automation you will need to actually add something to your list.

trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: todo
      service: add_item
    variables:
      list_name: |
        {{ state_attr((trigger.event.data.service_data.entity_id)[0], 'friendly_name') }}
      item: "{{ trigger.event.data.service_data.item | title }}"
      user_name: |
        {% set uid = trigger.event.context.user_id %}
        {% set user = None %}
        {% set matching_users = states.person | selectattr('attributes.user_id', '==', uid) | list %}
        {% set user = matching_users | first if matching_users | length > 0 else "System" %}
        {{ user if user == "System" else state_attr(user.entity_id,"friendly_name") }}
condition: []
action:
  - service: notify.mobile_app_example
    data:
      title: To Do item Added
      message: "{{item}} was just added to the {{list_name}} ToDo list by {{user_name}}."
mode: single

EDIT: Corrected typo

4 Likes

What do you mean with having to create a template?

Did you test the yaml I pasted? It should work straight off except the clickAction and URL that you need to modify to work in your own set up (or remove it if you dont want actionable notifications).

Thnx, copied your Yaml and its working now.
Might be also because of what @Didgeridrew said.
I clicked the run button for testing the automation i think.

But oke thanks its working

Thank you for this code.

One small question, it is working except the user_name is always system instead of the name who added the item.

Thanks for letting me know! There was a typo in the definition of the matching_users variable. I have corrected in the post above.

1 Like

This is a wonderful share! thank you. Is it possible to limit this automation to a specific to do list though rather than all of them?

Is it potentially also possible to get notified when something has been completed from a list? or removed? I wonder if this is also something you could use to notify of upcoming calendar events?

Add a condition to the automation such as:

condition:
  - condition: template
    value_template: "{{list_name == \"Shopping List\"}}"

Thanks! And is it possible to get a similiar notification when an item is marked as completed? And maybe recycle this automation for when an event is entered into a calendar?