Hello!
I am working on an automation for preventing duplicate items in my to-do list. For example, if I run my dryer twice I don’t need two instances of “Fold Laundry” on my to-do list.
I have the following:
alias: "To Do: Prevent Duplicates"
description: ""
triggers:
- event_type: call_service
event_data:
domain: todo
service: add_item
variables:
list_name: "{{ trigger.event.data.service_data.entity_id }}"
list_friendly_name: >
{{ state_attr((trigger.event.data.service_data.entity_id)[0],
'friendly_name') }}
task: "{{ trigger.event.data.service_data.item }}"
trigger: event
conditions: []
actions:
- target:
entity_id: "{{list_name}}"
data:
status: needs_action
response_variable: todo
action: todo.get_items
enabled: true
- choose:
- conditions:
- condition: template
value_template: "{{list_name == ['todo.before_bed']}}"
sequence:
- if:
- condition: template
enabled: true
value_template: >-
{{
todo['todo.before_bed']['items']|selectattr('summary','search',task)|list|count
> 0 }}
then:
- action: todo.remove_item
metadata: {}
data:
item: "{{task}}"
target:
entity_id: todo.before_bed
enabled: true
- conditions:
- condition: template
value_template: "{{list_name == ['todo.home_to_do_list']}}"
sequence:
- if:
- condition: template
enabled: true
value_template: >-
{{
todo['todo.home_to_do_list']['items']|selectattr('summary','search',task)|list|count
> 0 }}
then:
- action: todo.remove_item
metadata: {}
data:
item: "{{task}}"
target:
entity_id: todo.home_to_do_list
enabled: true
mode: queued
max: 90
What is interesting (aka I don’t understand why) is that the count used in the template condition of:
- condition: template
enabled: true
value_template: >-
{{
todo['todo.home_to_do_list']['items']|selectattr('summary','search',task)|list|count
> 0 }}
Is 1 less than what I would expect. If I use “>1” the automation only removes an item when the third instance populates the to do list, allowing for two instances of the item. If i use “>0” then it removes the second instance.
For some reason, this automation works great when I am testing out this automation by adding “fold laundry” manually, one at a time. However, every morning at 5am my to-do lists populate (starting empty) with what I need to get done that day. This is less than 40 items total across all my to do lists. For some reason this automation interferes with this and randomly allows a couple though and deletes the rest even though there are no duplicates being populated. Any idea what I am doing wrong?
I found similar (but not quite) posts here and here but these use completely different methods I would like to see if I can get this automation working since I think am close.
Thanks in advance!