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.
Here is my working solution - maybe you can pick out the part you need
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
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.
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
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
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.
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?
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?