Thanks for sharing this automation. I have it mostly working, but I want to implement the check to see if both lists are identical so I can run the sync more frequently than 20 minutes. Where exactly does this section of the code go? Should I be inserting it as the first action? Below is the actions section of my automation and it doesn’t currently work for me.
actions:
- alias: Check if lists are different
sequence:
- alias: Get ALL list entries of both lists
action: todo.get_items
metadata: {}
data:
status:
- needs_action
- completed
response_variable: all_items
target:
entity_id:
- todo.alexa_shopping_list
- todo.shopping_list
- alias: Check If both lists are identical
if:
- condition: template
value_template: >
{% set shopping_list_items = all_items['todo.shopping_list']['items'] |
selectattr('status', 'equalto', 'needs_action') | map(attribute='summary') | list | sort %}
{% set alexa_shopping_list_items = all_items['todo.alexa_shopping_list']['items'] |
selectattr('status', 'equalto', 'needs_action') | map(attribute='summary') | list | sort %}
{{ shopping_list_items == alexa_shopping_list_items }}
then:
- stop: Both lists are identical
enabled: true
- choose:
- conditions:
- condition: template
value_template: >-
{{ states.todo.shopping_list.last_changed >
states.todo.alexa_shopping_list.last_changed }}
alias: Alexa list is more up-to-date than AnyList
- condition: template
value_template: " {{ (now() - states.todo.alexa_shopping_list.last_changed).total_seconds() > 180 }}"
alias: AnyList list has not been updated for several minutes
sequence:
- sequence:
- alias: Clear the AnyList list
sequence:
- alias: Retrieve existing entries from AnyList
target:
entity_id: todo.alexa_shopping_list
data:
status:
- needs_action
- completed
response_variable: alexa_shopping_list_items
action: todo.get_items
- alias: Empty the AnyList list
repeat:
while:
- condition: template
value_template: "{{ (states('todo.alexa_shopping_list') | int ) > 0 }}"
sequence:
- alias: Refresh AnyList list after deletion
target:
entity_id: todo.alexa_shopping_list
data:
status:
- needs_action
- completed
response_variable: alexa_shopping_list_items
action: todo.get_items
- delay: "00:00:02"
- repeat:
for_each: >-
{{ alexa_shopping_list_items['todo.alexa_shopping_list']['items'] |
map(attribute='summary') | list }}
sequence:
- target:
entity_id: todo.alexa_shopping_list
data: |
{{ dict(item=repeat.item) }}
action: todo.remove_item
- alias: Update AnyList list with Alexa items
sequence:
- alias: Retrieve all entries from Alexa
target:
entity_id: todo.shopping_list
data:
status:
- needs_action
- completed
response_variable: shopping_list_items
action: todo.get_items
- alias: Define variables for unique items
variables:
unique_needs_action_items: |-
{{ shopping_list_items['todo.shopping_list']['items']
| selectattr('status', 'eq', 'needs_action')
| map(attribute='uid')
| list
| map('regex_replace', '^', '{\"uid\": \"')
| map('regex_replace', '$', '\"}')
| map('from_json')
| list }}
unique_completed_items: |-
{{ shopping_list_items['todo.shopping_list']['items']
| selectattr('status', 'eq', 'completed')
| map(attribute='uid')
| list
| map('regex_replace', '^', '{\"uid\": \"')
| map('regex_replace', '$', '\"}')
| map('from_json')
| list }}
- alias: Copy pending items from Alexa to AnyList
repeat:
for_each: "{{ unique_needs_action_items }}"
sequence:
- target:
entity_id: todo.alexa_shopping_list
data:
item: >-
{{ shopping_list_items['todo.shopping_list']['items'] |
selectattr('uid', 'eq', repeat.item.uid) |
map(attribute='summary') | first }}
action: todo.add_item
- alias: >-
Copy completed items from Alexa to AnyList and mark as
completed
repeat:
for_each: "{{ unique_completed_items }}"
sequence:
- target:
entity_id: todo.alexa_shopping_list
data:
item: >-
{{ shopping_list_items['todo.shopping_list']['items'] |
selectattr('uid', 'eq', repeat.item.uid) |
map(attribute='summary') | first }}
action: todo.add_item
- target:
entity_id: todo.alexa_shopping_list
data:
item: >-
{{ shopping_list_items['todo.shopping_list']['items'] |
selectattr('uid', 'eq', repeat.item.uid) |
map(attribute='summary') | first }}
status: completed
action: todo.update_item
alias: Update AnyList list
alias: Alexa list is more up-to-date than AnyList list
- conditions:
- alias: AnyList list is more up-to-date than Alexa list
condition: template
value_template: >-
{{ states.todo.alexa_shopping_list.last_changed >
states.todo.shopping_list.last_changed }}
- alias: Alexa list has not been updated for several minutes
condition: template
value_template: " {{ (now() - states.todo.shopping_list.last_changed).total_seconds() > 180 }}"
sequence:
- alias: Update Alexa list with AnyList items
sequence:
- alias: Clear the Alexa list
sequence:
- alias: Retrieve existing entries from Alexa
target:
entity_id: todo.shopping_list
data:
status:
- needs_action
- completed
response_variable: shopping_list_items
action: todo.get_items
- alias: Empty the Alexa list
repeat:
while:
- condition: template
value_template: "{{ (states('todo.shopping_list') | int ) > 0 }}"
sequence:
- alias: Refresh Alexa list after deletion
target:
entity_id: todo.shopping_list
data:
status:
- needs_action
- completed
response_variable: shopping_list_items
action: todo.get_items
- delay: "00:00:02"
- repeat:
for_each: >-
{{ shopping_list_items['todo.shopping_list']['items'] |
map(attribute='summary') | list }}
sequence:
- target:
entity_id: todo.shopping_list
data: |
{{ dict(item=repeat.item) }}
action: todo.remove_item
- alias: Update Alexa list with AnyList items
sequence:
- alias: Retrieve all entries from AnyList
target:
entity_id: todo.alexa_shopping_list
data:
status:
- needs_action
- completed
response_variable: alexa_shopping_list_items
action: todo.get_items
- alias: Define variables for unique items
variables:
unique_needs_action_items: |-
{{ alexa_shopping_list_items['todo.alexa_shopping_list']['items']
| selectattr('status', 'eq', 'needs_action')
| map(attribute='uid')
| list
| map('regex_replace', '^', '{\"uid\": \"')
| map('regex_replace', '$', '\"}')
| map('from_json')
| list }}
unique_completed_items: |-
{{ alexa_shopping_list_items['todo.alexa_shopping_list']['items']
| selectattr('status', 'eq', 'completed')
| map(attribute='uid')
| list
| map('regex_replace', '^', '{\"uid\": \"')
| map('regex_replace', '$', '\"}')
| map('from_json')
| list }}
- alias: Copy pending items from AnyList to Alexa
repeat:
for_each: "{{ unique_needs_action_items }}"
sequence:
- target:
entity_id: todo.shopping_list
data:
item: >-
{{ alexa_shopping_list_items['todo.alexa_shopping_list']['items'] |
selectattr('uid', 'eq', repeat.item.uid) |
map(attribute='summary') | first }}
action: todo.add_item
- alias: >-
Copy completed items from AnyList to Alexa and mark as
completed
repeat:
for_each: "{{ unique_completed_items }}"
sequence:
- target:
entity_id: todo.shopping_list
data:
item: >-
{{ alexa_shopping_list_items['todo.alexa_shopping_list']['items'] |
selectattr('uid', 'eq', repeat.item.uid) |
map(attribute='summary') | first }}
action: todo.add_item
- target:
entity_id: todo.shopping_list
data:
item: >-
{{ alexa_shopping_list_items['todo.alexa_shopping_list']['items'] |
selectattr('uid', 'eq', repeat.item.uid) |
map(attribute='summary') | first }}
status: completed
action: todo.update_item
alias: AnyList list is more up-to-date than Alexa list
enabled: true
mode: single
Any suggestions would be really appreciated.