watkins-matt / home-assistant-google-keep-sync

Custom component for Home Assistant that enables bidirectional synchronization with Google Keep lists.
MIT License
50 stars 4 forks source link

Generic automation #27

Closed agmorpheus closed 6 months ago

agmorpheus commented 6 months ago

Thanks for this great integration.

I have a question. Is it possible to make your sample automation more generic? I have several list that I want to sync with anylist. I think it is not the best solution to create for each list pair a seperate automation.

alias: Google Shopping List
description: Sync Google Shopping List with Anylist
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: todo
      service: add_item
    variables:
      item_name: "{{ trigger.event.data.service_data.item }}"
      list_name: "{{state_attr((trigger.event.data.service_data.entity_id)[0],'friendly_name')}}"
      list_entity_id: "{{ (trigger.event.data.service_data.entity_id)[0] }}"
      origin: "{{ trigger.event.origin }}"
condition:
  # Update this to the name of your To-do list in Home Assistant.
  - condition: template
    value_template: "{{ list_entity_id == 'todo.google_keep_**shopping_list**' }}"  <-------------------
action:
  # Optional: Send a notification of new item in Home Assistant.
  - service: notify.persistent_notification
    data:
      message: "'{{item_name}}' was just added to the '{{list_name}}' list."
  # Add new item to your Anylist list
  # Update the entity_id list name to your list in Home Assistant.
  - service: todo.add_item
    data:
      item: "{{item_name}}"
    target:
      entity_id: todo.anylist_alexa_**shopping_list**   <-------------------
  # Complete item from google shopping list. Can also call todo.remove_item to delete it from the list
  # Update entity_id to the id of your google list in Home Assistant.
  - service: todo.update_item
    target:
      entity_id: todo.google_keep_**shopping_list**   <-------------------
    data:
      status: completed
      item: "{{item_name}}"
# The maximum number of updates you want to process each update. If you make frequent changes, increase this number.
mode: parallel
max: 50

The "shopping_list" string of the entities needs to be a variable. Maybe it possible to use the list_entity_id somehow. But I dont know how? I am new to HA, maybe you have an idea?

Thanks!

watkins-matt commented 6 months ago

I don't personally use the example automation so I can't test this, but I can try to point you in the right direction. In the future, the Home Assistant forums will usually be more helpful for general automation questions.

You might be able to do something like this

alias: Universal List Sync
description: Sync any specified list with Anylist
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: todo
      service: add_item
variables:
  item_name: "{{ trigger.event.data.service_data.item }}"
  list_name: "{{ state_attr(trigger.event.data.service_data.entity_id[0], 'friendly_name') }}"
  list_entity_id: "{{ trigger.event.data.service_data.entity_id[0] }}"
  prefix: "todo.google_keep_"
  list_key: "{{ list_entity_id.replace(prefix, '') }}"
condition: []
action:
  - service: notify.persistent_notification
    data:
      message: "'{{ item_name }}' was just added to the '{{ list_name }}' list."
  - service: todo.add_item
    data:
      item: "{{ item_name }}"
    target:
      entity_id: "todo.anylist_alexa_{{ list_key }}"
  - service: todo.update_item
    target:
      entity_id: "{{ list_entity_id }}"
    data:
      status: completed
      item: "{{ item_name }}"
mode: parallel
max: 50

The key part is this

prefix: "todo.google_keep_"
list_key: "{{ list_entity_id.replace(prefix, '') }}"

Basically, you would have to rename the entities names so the suffixes match between Google Keep Sync and Anylist Alexa. So they would all keep their prefixes (all Keep todo entities start with todo.googlekeep and all Anylist with todo.anylistalexa) and then make sure that everything that comes after todo.googlekeep would match up with the list you want to sync to with anylist_alexa.

So example:

You want todo.google_keep_sync_shopping_list to sync to Anylist, you would name it todo.anylist_alexa_shopping_list. You want todo.google_keep_sync_grocery_list to sync, you name the Anylist todo.anylist_alexa_grocery_list. Again, I can't test this, but hopefully this helps out.

agmorpheus commented 6 months ago

Thanks for your help. I got it working. I also added a condition, that the automation is not applied again for the new item on the Anylist List