Sync your Alexa/Todoist shopping list to the Home Assistant shopping list!

I have absolutely the same problem.
Any updates on a solution?

Guys, Iā€™ve also fixed it using Nabu Casa URL instead of DuckDNS and port forwarding.
Thank you all for the comments.

Please someone help me with my code, all two folders are in the right place and correctly named.
I think im either missing quotations or deleting too much. sorry but im new to python.

If someone could write it for me so i can copy and paste, then i can see where i went wrong and improve my python language.

import requests
import sys
import time
from importlib import reload
if "/config/pyscript_modules" not in sys.path:
    sys.path.append("/config/pyscript_modules")

import write_file

write_file = reload(write_file)

TODOIST_TOKEN = "5a818295043749fghjmh79b0e8ad8bd75660epfdea7"
TODOIST_PROJECT_ID = Alexa_Shopping_List

def get_tasks():
    get_tasks_url =f"https://api.todoist.com/rest/v1/tasks?project_id=Alexa_Shopping_List"
    headers = {"Authorization" : f"Bearer 5a81829505657689377b7657868d8bd75660e6efaa7"}

    status_code = 500

    while status_code is not 200:
        response = task.executor(requests.get, get_tasks_url, headers = headers)
        status_code = response.status_code
        time.sleep(1)
    json = response.json()
    return json

def add_task(item):
    url = "https://api.todoist.com/rest/v1/tasks"
    headers = {"Authorization" : f"Bearer T5a0437498946578898bd75660e6efaa7", "Content-Type" : "application/json"}
    body = {"content" : item, "project_id" : Alexa_Shopping_List}
    status_code = 500

    while status_code is not 200:
        response = task.executor(requests.post, url, headers = headers, json=body)
        status_code = response.status_code
        time.sleep(1)
    if status_code == 200:
        return True
    else:
        return False

def update_task(id, content):
    url = f"https://api.todoist.com/rest/v1/tasks/Alexa_Shopping_List"
    headers = {"Authorization" : f"Bearer 5a8182950477b0e8ad8bd75660e6efaa7", "Content-Type" : "application/json"}
    body = {"content" : content}
    status_code = 500

    counter = 0
    while status_code is not 204 and counter < 10:
        response = task.executor(requests.post, url, headers = headers, json=body)
        status_code = response.status_code
        time.sleep(1)
        counter += 1
    if status_code == 204:
        return True
    else:
        return False

def complete_task(id):
    url = f"https://api.todoist.com/rest/v1/tasks/Alexa_Shopping_List/close"
    headers = {"Authorization" : f"Bearer 5a818295044555675ad8bd75660e6efaa7"}
    status_code = 500

    counter = 0
    while status_code is not 204 and counter < 10:
        response = task.executor(requests.post, url, headers = headers)
        status_code = response.status_code
        time.sleep(1)
        counter += 1
    if status_code == 204:
        return True
    else:
        return False

@service
def sync_shopping_list():
    tasks = []
    json = get_tasks()
    for item in json:
        tasks.append({"name" : item["content"], "id" : str(item["id"]), "complete" : item["completed"]})

    task.executor(write_file.write_json, filename = "/config/.shopping_list.json", content=tasks)
    hass.data["shopping_list"].async_load()

@event_trigger('shopping_list_updated')
def update_shopping_list(action=None, item=None):
    if action == "add":
        add_task(item["name"])
        sync_shopping_list()
    if action == "update" and item["complete"] == False:
        update_task(item["id"],item["name"])
        sync_shopping_list()
    if action == "update" and item["complete"] == True:
        complete_task(item["id"])
        sync_shopping_list()

Two things I can spot:

  1. You donā€™t need to input your token and project ID in multiple places - just the first variable declaration on line 12/13. The rest of the code just references these variable.
  2. You need to use the number for your project ID which you can get from the end of the URL in Todoist when viewing the project. (this is described in the docs).

Hi folks,

Iā€™m getting a warning in the logs everytime I ask Alexa to add an item to the shopping list:

 Logger: homeassistant.util.async_
Source: util/async_.py:157
First occurred: 22 June 2022 at 22:20:07 (1 occurrences)
Last logged: 22 June 2022 at 22:20:07
Detected blocking call to sleep inside the event loop. This is causing stability issues. Please report issue to the custom component author for pyscript doing blocking calls at custom_components/pyscript/eval.py, line 1906: return func(*args, **kwargs)

According to pyscript docs, this can be avoided (see here), but Iā€™m no developer so wouldnā€™t know where to start. Anyone else come across the same issue, or know how to resolve? Asking here before raising a GitHub issue.

Iā€™m having the same issue tooā€¦

Logger: homeassistant.util.async_
Source: util/async_.py:157
First occurred: 16:47:16 (1 occurrences)
Last logged: 16:47:16

Detected blocking call to sleep inside the event loop. This is causing stability issues. Please report issue to the custom component author for pyscript doing blocking calls at custom_components/pyscript/eval.py, line 1906: return func(*args, **kwargs)

Any help would be really appreciated @MrLemur or anyone else who might have an ideaā€¦

1 Like

Iā€™m unable to get this working.

Iā€™m running Home Assistant Docker.
I install pyscript
It couldnā€™t find the integration, but I enabled it in my configuraiton.yaml file
I added shopping_list_sync.py to /config/custom_components/pyscript (I also tried making a new folder ā€œpyscriptā€ directly in config)
I reloaded home assistant and went to developer tools, but it only finds pyscript.reload and pyscript start jupyter kernel. I canā€™t find where Iā€™m supposed to put my files such that they show up in services.

Can someone point me in the right direction for where Iā€™m messing up?

I found out what was going wrong by checking the logs and it wasā€¦ a lot

First, since Iā€™m using docker I needed to add the pyscript and pyscript_modules folders to my persistent directory I set up when I made a docker instance.

Then I needed to enable execution rights to the shopping_list_sync.py after I made it

chmod +x shopping_list_sync.py

then for some reason I just canā€™t fathom, it didnā€™t like the file name ā€œwrite_file.pyā€ in my pyscript_modules folder so I had to change this to ā€œwf.pyā€

And of course this meant I needed to change line 7 from

import write_file

to

import wf

and change line 8 from

write_file = reload(write_file)

to

write_file = reload(wf)

once I had made those changes it worked in my container on Truenas.

Now I just need to find out why it doesnā€™t automatically update my list when something is added on todoist and fix that blocking warning others have been referencing

I was having this problem too. I had to go back to the todoist page and reauthorize the integration, somehow it had lost itā€™s authorization.

Working perfectly! My Home Assistant is not accessible off-grid (only via tailscale), so I created a button to update the shopping list (triggering the service) next to that list, and an automation to update it a few times a day

Thanks!

All done but got a weird one.

If I add an item or remove an item in HA it pushes it to Todoist and then to alexa, but wont work in return. If i remove an item from Alexa it pushes it to todoist but then not to HA. Manually calling the service fixes it, so I assume its the webhook not working correctly.

I have a weird oneā€¦
I also use the ā€œBring!ā€ integration to sync my Bring! shopping list to HA. This works ok, but because its happening in the back end and not the UI, it doesnā€™t appear to fire the ā€œshopping_list_updatedā€ event. I know thatā€™s a problem with that particular integration, but I was wondering if its possible to modify this one so it can look through the items on HA and compare them to the items on Todoist and if its not in Todoist, add it (or mark it as completed when its completed).

Also, for those who were having the ā€œblocking callā€ issue, I was able to update @MrLemurā€™s code to make it work without the blocking call. Iā€™m far from a python programmer but I was at least able to make these mods to fix the blocking call thanks to the link @fenty17 posted.
My updated version of the code is available here: https://github.com/alexisspencer/shopping_list_sync

2 Likes

@alexismadd - thanks so much for sharing! Delighted to have one less log error!

FYI - my setup of this successfully keeps all lists in sync, regardless of whether I update via HA, Todoist or Alexa.

Glad to help! As I said, Iā€™m far from a python coder but I do have some coding background in other languages that have some similarities so I was able to make those adjustments at leastā€¦
I too wasnā€™t enjoying seeing the errors appearing. Lol
Iā€™m happy for @MrLemur to grab my code adjustments if he thinks theyā€™re OK and update this original post.

1 Like

thanks for this integrations, works flawlessly but I managed to add both shopping list and to do list from alexa ā€¦todoist and haā€¦problem is when i call shopping list script it updated to shopping list and same for todo list but it doesnā€™t show both at the same timeā€¦i want both the categories to be shown say one for shopping list and one for todo listā€¦is it possible to do like that in ha frontend?

Ca anyone help me with Teamviever? I Installled all but it did not work. Please PN me thanks.

The main issue with trying to sync both is thereā€™s only one ā€œshopping listā€ in home assistant. So if you have a web hook from your Alexa-syncā€™d to do list in todoist, the home assistant side will simply add/remove from your shopping list

yeahā€¦but no option to have both projects displaced at the same timeā€¦But no worries, I found a todoist card from there iam able to add other projects in dashboard.

I followed all the instructions but the shopping_list_sync service IS NOT CREATED
Thank you

I am really new to this scripting. i am looking at my config and i do not see a ā€œpyscript_modulesā€??