New service calls and sensors for shopping list component

I submitted a Pull Request which got released with version 0.71 of Home Assistant.

There are now two services which allow adding and removing items from the shopping list!

2 Likes

Cool! Half of my request is fulfilled. Now waiting for a sensor that will list down all items in the shopping list.

1 Like

I want to support this request because I like the way the shopping list works and how it looks. However, I am struggling with working with the shopping list at the moment.
My intention is to send the filled shopping list via email and start a new one afterwards. At the moment, I am able to send it via email (using an external script reading the .shopping_list.json) but I have no idea how to clear the complete list to start a new one.

In the future, I also want to add voice control by snips. I guess the new services should help with that, so I already appreciate what has been done so far :slight_smile:

great idea. appreciate if you can share on how you do that. thanks in advance.

Sure. It is just a little quick & dirty python code that I added as a shell command to homeassistant:

#!/usr/bin/python
# coding: utf8
import json, smtplib, datetime

sender='XXX'
reciever='YYY'
account='ZZZ'
password='BBB'

with open('PATHTOHOMEASSISTANT/.shopping_list.json') as data_file:
    shoppingListData = json.load(data_file)
   
# build message
content = u"Shopping list:\n"
for entry in shoppingListData:
    if not entry['complete']:
        content += u"- %s\n" % entry['name']

content += u"\nThank you for shopping!"

mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(account,password)
message = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (sender, reciever, "Shopping list", content)
mail.sendmail(sender,reciever,message.encode('utf-8'))
mail.close()

The code was tested using gmail. However it should work with other email providers by replacing the SMTP server accordingly. You need to enter the sender email address, the receiver email address and your mail account information that are hard coded (there certainly is room for improvements :wink:)

Why not make an command line sensor? This is working in docker also.

sensor:    
  - platform: command_line
    name: shopping_list
    command: /config/shopping_list.py

shopping_list.py:
#!/usr/local/bin/python
# coding: utf8
import json

with open('/config/.shopping_list.json') as data_file:
    shoppingListData = json.load(data_file)

content = u"Boodschappen:\n"
for entry in shoppingListData:
    if not entry['complete']:
        content += u"- %s\n" % entry['name']

content += u"\n"

print(content)

Then this is possible:

trigger:
  platform: zone
  zone: zone.shop
  event: enter
action:
  service: notify.telegram
  data_template:
    message: '{{states.sensor.shopping_list.state}}'
4 Likes

I’m wondering if someone is working on recognize 2D Barcode (specific hardware device or using image processing ?).
There is an API for matching barcode and a specific product. It could be cool to scan a barcode to automatically add the product to the shopping list.

1 Like

Thanks. Command line sensor works perfectly for me.

This is great. Thanks for the simple python script. At first the sensor reported a status of unknown for me. I had to set the sensor up slightly differently in Hassio by prefacing the command with python3.

  - platform: command_line
    name: shopping_list
    command: python3 /config/shopping_list.py
4 Likes

Hello All and Flovie in particular.
New to Homeassistant and Linux
Im trying to use your python script above but i dont know how to execute it.
I have created a folder under the homeassistant folder called “python_scripts” and created a send.py file there with your script , ofcourse edited with my e-mail address and path
home/homeassistant/.homeassistant/python_scripts/shopping_list.json
I dont know how to activate or run the script, can i create a button “send email” to run this script or can i controll it with a simple voice command?
Any help appreciated.

#!/usr/bin/python
# coding: utf8
import json, smtplib, datetime

sender='[email protected]'
reciever='[email protected]'
account='[email protected]'
password='xxx'

with open(' home/homeassistant/.homeassistant/python_scripts/shopping_list.json') as data_file:
    shoppingListData = json.load(data_file)
   
# build message
content = u"Shopping list:\n"
for entry in shoppingListData:
    if not entry['complete']:
        content += u"- %s\n" % entry['name']

content += u"\nThank you for shopping!"

mail=smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(account,password)
message = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (sender, reciever, "Shopping list", content)
mail.sendmail(sender,reciever,message.encode('utf-8'))
mail.close()

Hi,

if you are using homeassistant in an environment where you are free to place and run python scripts. Than you can go the way I went:

  1. create a file with the pyhton code inside (for example sendshoppinglist.py)

  2. create a shell_command component in your config.yaml. For example:

    shell_command:
    sendshoppinglist: /sendshoppinglist.py

I didn’t get the correct indentation working in this example, so be careful with copy&paste. This code will expose the python code as a service to your home assistant.
3. You are free to call this service how you like. May it be voice command or an automation.

If you are in a more restricted environment, then you should take a look at the code by Mark_Meijer and use a notification component of your choice.

I hope this helped you out and pointed in the right direction.

Thank you Flovie
Yes that helped me, i have it all working now.
Ran into more problems with gmail thoo, they have beefed up their security and default setting is Not to allow unsecure apps to e-mail. I logged into my gmail and under “security…” there was an option to turn off the setting for unsecure apps.

thanks again

Hi guys.

I would like to add an item to the shopping list with snips but i am getting errors.

  addShoppingListItem:
    action:
      - service: shopping_list.add_item
        data_template:
          name: {{ shopping_item }}

This gives me this error:

Tue Jan 01 2019 15:19:43 GMT+0100 (Mitteleuropäische Normalzeit)

invalid key: "OrderedDict([('shopping_item', None)])"
  in "/home/homeassistant/.homeassistant/intent_script.yaml", line 167, column 0

check line 167 in your file intent_script.yaml
There must be an error there.

Thank you for your answer. line 167 is

name: {{ shopping_item }}

Any ideas to integrate with the google shopping list (home)?

not sure about google home. but i manage to sync the shopping list with alexa shopping list using IFTTT. Whenever Alexa shopping list is updated (i.e. item added or remove), IFTTT will update my HA shopping list via webhook.

1 Like

“Add” and “Check Off” are now services, but the only way I know of to delete the checked items is to use the Lovelace card.

Would be nice to have a third service, “Delete” to actually remove a shopping list item.

There is several good ideas on this thred.

I would like to get a easyer way to have a inventory list for all my food storages. Eg one for the freezer and a other food storages like the dry cabinets and the fridge.
Now I use google shopping lists: https://shoppinglist.google.com
With google home I can use voice to add items to my primary list and all family members can do the same.
With NFC stickers I can open a spesific inventory list for the cabinet. This could also be done with QR code and camera on the phone (just opens a url).

But for a better user experience it would be good to be able to scan the barcode on my item as I put it in the trashcan, then it would be removed from my inventory list and added to the main shopping list.
Same if I put a new item into the freezer I culd just scan the barcode with the phone and it would update the inventory for the freezer.

So my wishlist for the shopping list is:

  1. The ability to add/remove items with a QR/barcode scanner for a spesific list.
  2. A sensor for lookup the barcode data e.g. from openfoodfacts.org
  3. Integration/syncronisation with my primary google shoppinglist.
1 Like

You are probably looking for something like https://grocy.info?