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
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?
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
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
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.
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?
I am trying to “reverse engineer” what @Didgeridrew has done so exploring what happened when you add an item by using the “events” under “Developer tools”
Seeing that the add_item
service results in:
What you see in the script
uses trigger.event.
plus the structure form the event:
The remove_item
service fires the following event:
So I take it with some modifications to @Didgeridrew code, you can achieve what you want.
For now I am not able to find how do you know that e.g. you need the | title
in:
trigger.event.data.service_data.item | title
so I am not really in the position to give you the answer.
I am as eager to hear the answer in anyone can provide it.
Apologies for not being of much help.
trigger:
- platform: event
event_type: call_service
event_data:
domain: todo
service: update_item
variables:
list_name: |
{{ state_attr((trigger.event.data.service_data.entity_id)[0], 'friendly_name') }}
item: "{{ trigger.event.data.service_data.rename }}"
condition:
- alias: Check if event is in the desired todo list
condition: template
value_template: "{{ list_name == 'Shopping List' }}"
- alias: Check if event is for completing a todo
condition: template
value_template: "{{ trigger.event.data.service_data.status == 'completed' }}"
action:
...
AFAIK, there isn’t an event posted when you add a calendar event through the UI, but if it is done through a service call to calendar.create_event
, that can be used as a trigger in the same general manner.
The filter title
just changes the output string to title case i.e. “this is an example” is returned as “This Is An Example”. It is not functionally necessary, it just looks better.
Thank you so much! This is really such an amazing automation and variation.
I wonder if you have a generalised automation for recurving upcoming calendar events that you use and would be happy to share?
It would be best for you to start a new thread with a specific description of the calendar automation you have in mind.