Automating your shopping list with Home Assistant and Grocy

Today I’m going to show you some of the ways I’m using Home Assistant and Grocy to automate our shopping list. Using Home Assistant with Grocy allows our smart home to tell us when we need to purchase something, as opposed to adding things to a shopping list when it’s already too late.

 

What is Grocy?

If you’re running HASS IO, you may have seen an add-on from Franck called Grocy. This little gem is a stock management system for the home. Grocy allows you to know how much of a product you have at home, and to track when items are due to expire. For example how many litres of milk you have at home, and when it will expire.

The recipes feature of Grocy is where it all comes together. Once you put in the ingredients required for a meal, Grocy can tell you what ingredients you have on hand to make that meal, or allow you to add missing items to the shopping list. When you cook a recipe, by the click of a button, Grocy will reduce the stock levels of those products and keep track of your remaining stock.

For those times when you don’t use a fixed recipe, there is also the ability to “consume” items in Grocy. This is where you can tell Grocy you just drank a can of Fosters, so it knows how much you have left in stock.

For the most part this is great, but if you want to take full advantage of Grocy it does require you to login and track your usage of items regularly. This can get quite annoying if you need to open your phone each time you use an egg from the fridge or wake up in the night for a glass of milk. This is where we can leverage our smart home controller, in my case Home Assistant, to automate the consumption of items in Grocy.

 

Setting up Home Assistant

One of the things I love about Home Assistant is its flexibility to integrate with other systems and software. There’s a whole bunch of ways to integrate things, from HTTP with REST sensors and switches, to MQTT.

To make things easier, I’m going to use a Shell Command, which will make a cURL request to the Grocy REST API to consume an item. I want to be able to re-use as much code as possible, so I’m going to use template data to dynamically send data to this cURL request. This will come in handy later.

To begin, lets start with our shell command, and I’ll explain what this is doing.

In this command, I’ve got some parameters wrapped in {{brackets}}. These will allow us to use the same shell command in our scripts and automations moving forward. Let’s break down these parameters.

{{server_ip}} The IP address of the server where Grocy is running. If running on the same machine as Home Assistant, this could be 127.0.0.1
{{PRODUCT_ID}} The Grocy ID of the product to be consumed. You can get this from the URL of the edit product page inside Grocy.
{{grocy_key}} The API key for your Grocy install. Get this from your Grocy settings page.
{{AMOUNT}} The amount you wish to consume. This depends on the product.

We’ll use this shell command throughout the next few scenarios.

 

The Latest In Your Inbox

Enter your email address below to receive my latest blog posts and videos about Home Automation in your Inbox

 

Dishwashing Tablets

If you’ve previously read my blog on making dishwashers and washing machines smart, you’ll know that my smart home knows the state of my dishwasher. This means that my home also knows when we start the dishwasher.

With this information, we can get Home Assistant to tell Grocy when we have used a dishwashing tablet.

 

Setting up Grocy

First we need to create a product in Grocy for dishwashing tablets. For this I’ve created a new Quantity Unit called tablet. Once I’ve created a product in Grocy, I then use the inventory to record how many dishwashing tablets I have in the house.

There’s a couple of things going on in this screenshot, so let me explain.

The minimum stock amount is defined when you create your item in Grocy. Once I have less than 60 dishwashing tablets in stock, Grocy will mark the item as low in stock and prompt me to add this to my shopping list.

Next up is the QU Purchase or Quantity Purchase. Here you’ll notice I’ve created a Quantity Unit in Grocy called 80pk Box. This is because you don’t go to the supermarket and ask for 1 dishwashing tablet. You buy them in packs. For me, the brand I like to use always comes in an 80pk box. For this reason, the QU Factor is set to 80. This means when I purchase 1 box of dishwashing tablets, Grocy will automatically add 80 tablets in stock.

Finally the Product Group is handy for the shopping list feature. Items will be grouped together nicely, so when you’re in the supermarket you can have your shopping list arranged into the aisles.

Once we’ve created the product we can use the Inventory feature of Grocy to tell Grocy how many tablets we have in stock already. Once Grocy knows how many dishwashing tablets are in the house, its time to tell Home Assistant to consume (remove 1 tablet from stock) each time the dishwasher starts.

 

Home Assistant

To start off simple, each time my dishwasher goes into the Running state, I want Home Assistant to remove 1 tablet from Grocy. To do that, I’ll use the following automation.

Pretty basic, right? Using those template variables in the shell command, we can also use Home Assistant secrets as variables. This way if your product ID for dishwashing tablets changes in the future, you’ve just got one place to update.

Although now I have a problem. Let’s take a look at the diagram from my Dishwasher state automation.

In this diagram, we can see that the state can go back to Running in the middle of a cycle. This means each time the power drops and comes back up, Home Assistant will remove a dishwashing tablet from my stock, even though I haven’t put another tablet in the dishwasher.

I need to stop this from happening. So let’s go ahead and make a small change to our automation above.

This will turn the automation off once it has been run. Now we need another automation to turn this back on once the dishwasher door has been opened.

Now Home Assistant will automatically remove a dishwashing tablet from my stock levels when the dishwasher is run. Grocy will prompt me to buy tablets when stock levels get below my set threshold.

 

Washing Machine

So the dishwasher was pretty simple. But what about something a tiny bit more complicated? The washing machine needs to manage two items. Washing Powder and Fabric Softener. However unlike the dIshwasher, these are different amounts. Washing powder is measured in weight, whilst the Fabric Softener is measured in litres.

This makes things slightly tricky. For my washing powder, I can easily use a kitchen scale to weigh how much a scoop of powder consumes. Luckily for the liquid, it mentions there are 25 washes in the bottle. Using this, we can do some maths to work out how much Home Assistant should subtract each time the washing machine is turned on.

2000ml / 25 washes = 80ml per wash

From there, we can then plug these into our Home Assistant automations.

In Grocy I’ve set these items up with the right purchase and consumption units like the dishwashing tablets.

 

 

Regular Consumables

So far we’ve automated things where Home Assistant can “see” them being consumed. But what about other things that Home Assistant can’t see via sensors?

For daily things, we can setup timed automations to execute at a certain time. For example every day at 9am, remove a breakfast bar from stock, but only if it is a workday.

 

For items consumed on an ad-hoc basis, we can use the Home Assistant interface. This would be particularly helpful for a tablet like an Amazon Fire Tablet mounted to a wall in the kitchen.

We can easily create scripts, which can be used to tell Grocy to consume items when called. Here’s a script which will remove 100ml of Milk from stock when executed.

 

Which can appear in Lovelace with an entity card like:

 

 

Alerting when item below threshold

Grocy has the ability to mark items in its interface when they are below a threshold. However this requires you to open the Grocy interface. It would be better if Home Assistant could send you a notification when your stock levels have reached a certain point.

We can easily do this with a REST Sensor, and some automations. We’ll need to use a full URL to the Grocy API for this.

Now we’ll setup an automation to send a notification at 9:30pm if there are less than 10 dIshwashing tablets in stock.

Maybe instead of bugging me at 9:30pm when the grocery store is closed, Home Assistant could just add the item to my shopping list once. There are several different systems Home Assistant can integrate with for this. For example you may choose to send an email to Todoist. For the sake of simplicity, I’m going to use the Home Assistant shopping list for this example.

 

You can also automate marking the items as done once the stock amount returns to a normal threshold

The only problem with marking things off the Home Assistant shopping list automatically is that there is no way to check if an item exists in the Shopping List. This means each time you use a dishwashing tablet, and it goes down from 50 -> 49, 49 -> 48 this automation will get called and may throw some errors.

We can simply turn the automation off so that once the Dishwashing tablets have been marked as complete, we won’t try again.

For good measure we should also turn that automation back on, once we have added the item to the shopping list.

 

Making it simple with the Grocy custom component

The examples I’ve given so far will work for any Home Assistant installation, because they’re using core components to talk directly with the Grocy API.

There is also a custom component for Home Assistant from Sebastian Rutofski. Using the custom component, you could instead use service calls and sensors instead of relying on shell commands and REST sensors. If you’re interested, you can check out that component on GitHub.

The Grocy custom component is also available for one-click install via the Home Assistant Community Store (HACS).

 

Going Further

So far we’ve got the the shopping list automated based on time, and when devices change state. However there’s some other cool use cases you could use with Home Assistant. Here’s some ideas…

  • Use a Xiaomi Wireless button as a “consume button”. Stick these near your fridge or pantry, and label the button. Each time you press the button, Home Assistant will remove the applicable item (for example 200ml of Milk, or a can of soda) from your stock levels.
  • Automatically update the stock levels of the popcorn when a movie starts playing on Plex late at night.
  • Remove a portion of soap or shampoo from your inventory each time someone has a shower, by taking action when a water leak sensor in your shower is activated.
  • Use an ESP8266 device with a weight sensor to automatically report the stock levels of jarred items like flour or sugar directly into Grocy.
  • If your device tracker GPS enters a radius near the grocery store, have Home Assistant email you any items that are below the threshold set in Home Assistant.

 

Buy me a hot chocolateBuy me a hot chocolate
Recommended Posts
  • Jokerigno

    Hi Phil,

    I was looking at grocy’s add on some days ago. It definitely has an high potential but in my opinion the smartest solution will be using voice.
    Unfortunately at the moment there is no support for Alexa or Google Assistant and that’s a pity in my opinion.

    • Yeah totally. We still use voice for those items. I am strongly considering making Home Assistant the middle man when it comes to the Alexa shopping list, for example

      Alexa, add milk to my Shopping list.
      -> IFTTT receives a notification of new item
      -> IFTTT sends a webhook to Home Assistant via Nabu Casa
      -> Home Assistant sees Milk added to the shopping list, tells Grocy to add Milk to the shopping list.

      Currently there’s no “going back” to Alexa. You could potentially use something like Todoist to do it, for example:

      Add milk to Shopping list in Grocy
      -> Home Assistant sees milk in Grocy, sends an email to Todoist to add milk to the Shopping list
      -> Todoist and Alexa tasks are synced, so Milk is added to the Alexa shopping list automatically by Todoist.

      You could also remove Todoist out of the mix, and use a custom Alexa skill hosted on EC2 or something. Maybe that will be my next mission,

      • Jokerigno

        Well I’m a google assistant guy actually 🙂 .

        And (don’t ask me why) google assistant shopping list is some kind of black hole at the moment
        (No API)

    • Robert Broughton

      You could use any of the voice recognition sw (google assistant, alexa, snip ai, etc) to recognize it, then use something like Node-Red and MQTT to both add it to your list and then have a response text sent to a TTS (text to speech) call to acknowledge the addition to your grocery list (or chores).

  • Tijtij

    Another good idea is to have a barcode scanner near the trash and scan packages of used up items as you throw them away.

    • TheFuzz4

      I do pretty much that. But I knew that I couldn’t have the scanner attached to something. I needed to maximize the WAF so I wrote a python script to handle the barcode scanning
      https://github.com/grocy/grocy-pyscanner

      My script does make API calls to HomeAssistant only for voice to text to give the user some feedback. If the item isn’t in the system it will attempt to go find it. Of course once it does find it, you will need to do some manual updates to the system but it does speed things up.

  • Call me a luddite but a pencil and folded up sheet of paper has worked for us for decades, and I have not seen a single automated shopping list system that even approaches if for simplicity. I’m all for home automation, but some things seem to be made unnecessarily more complex just for a tech factor

    • Robert Broughton

      A true luddite would use just basic memory and skip that advanced processed wood stick with a carbon core and the accompanying processed wood that was smashed flat (plus all the fancy education that goes into learning to read and write). Everyone prefers their own tech level it seems.

    • Not automatic, but there are a lot of ways tech makes grocery shopping easier. For example: auto grouping by aisle, add frequently purchased items, see when the last time you bought something was.

    • Not automatic, but there are a lot of ways tech makes grocery shopping easier. For example: auto grouping by aisle, add frequently purchased items, see when the last time you bought something was.

  • Tom Still

    Hey there. Thanks for the write up! Just wanted to ask where is that grocy.yaml file supposed to live? I have it setup via the official add-on store in Home Assistant. Do i just create a grocy.yaml file where my main config yaml is with the code you put? I wasnt sure if i need to update the IP and API in that code or if its pulled from the integration.

    • Hi @disqus_un8OkJ6hrj:disqus

      That file is part of a Home Assistant Package (https://www.home-assistant.io/docs/configuration/packages/)

      If you haven’t split up your config into packages, then you can just use the specific parts where needed. For example, anything under “automation:” should go in your automations.yaml file/section in configuration yaml.

      Cheers

  • Hello, thank you for the awesome article. Thanks to this article I manage to automate consumption of my dishwasher chemicals. I have a question, how can I automate recipe?
    I wrote this line:
    grocy_consume_recepie: "curl -X POST http://192.168.0.1:8123/api/hassio_ingress/34k56kh45k6hk345h6k3b/api/recipes/{{recipe_id}}/consume -H 'accept: */*' "

    but automation doesn’t seem to be working

    • Nice one Ivan!

      I must admit I haven’t automated the consumption recipes. Although looking at your code there, I can’t see why it wouldn’t work.

      I would check in Grocy that you can consume the recipe. Perhaps there is a unit not in stock, and hence it can’t be consumed for some reason?

  • ikrowni

    Is there anyway to monitor chore state and execute an automation to mark a chore as complete? I’m able to turn my kids internet to all of their devices on/off through tasker but i need a method to monitor the state and mark the chores as complete.

    • Yep there should be.

      Check this Grocy API endpoint which can mark a chore as done: https://demo.grocy.info/api#/Chores/post_chores__choreId__execute

      • ikrowni

        thanks but how would i get the current state of the chore into home assistant? I’m wanting to create a lovelace UI block that shows the days chores for my son with a button for him to press that says “complete” that switches the chore from incomplete to complete which then fires the automation when all of them have been checked as complete? Is this doable?

        • Yeah I think that should be doable with a combination of automations and sensors.

          I would use a REST sensor to check the status of the chore from the Grocy API (look at the GET method).

          I would use the “next_estimated_execution_time” field to determine if the chore is due today/overdue. If the chore is due today, then the sensor will return true/on.

          In Lovelace, if the chore sensor is “on”, it means it needs to be done. So then display the button to mark the chore as done. Once the chore is marked as done, the sensor will be marked as “off” and hidden in Lovelace.

          then fires the automation when all of them have been checked as complete

          For that, I would have an automation that watches all of the binary sensors. When the conditions are that they are all “off”, execute the automation as required.

          To point you in the right direction, here is what I think you would need:

          https://www.home-assistant.io/integrations/rest/
          https://www.home-assistant.io/integrations/binary_sensor.template/ (is the chore due today/overdue yes/no)

          • ikrowni

            sorry to keep buggin you. Im trying to make the rest sensor and it just comes up as “unknown”

            – platform: rest
            resource: http://192.168.0.15:8123/api/hassio_ingress/REDACTED/api/chores/08
            headers:
            GROCY-API-KEY: REDACTED
            name: James Dishes
            value_template: ‘{{ value_json.next_estimated_execution_time }}’
            scan_interval: 3600

          • Hmm that looks ok. Is that a binary sensor or just a sensor?

          • ikrowni

            Got the sensor working but im not sure how using the next_estimated_execution_time would work. It just displays as a date and time as the sensor state. Think you could give me an example of how i would use next_estimated_time to give me a state of on/off within home assistant?

            Also how do you use the post part of it to mark it done? I just get errors when trying to use it. Im new to rest sensors sorry if i sound like such a noob

          • ikrowni

            Okay im making some progress here.

            I created a binary_sensor with this code here

            – platform: rest
            resource: http://192.168.0.15:9192/api/chores/08
            headers:
            GROCY-API-KEY: REDACTED
            name: James Dishes
            value_template: ‘{{ value_json.next_estimated_execution_time }}’
            scan_interval: 5

            This is now showing an off state but i assume it will change to on the next time the chore is due.

            i created a shell_command with this code here

            james_dishes_complete: “curl -X POST http://192.168.0.15:9192/api/chores/08/execute -H ‘accept: application/json’ -H ‘GROCY-API-KEY: REDACTED’ -H ‘Content-Type: application/json’ -d ‘{“tracked_time”:”2020-05-07T03:56:14.571Z”,”done_by”:0}'”

            This is now tracking when it is executed within grocy and seems to be working great. Everytime i click it i can see the tracked count go up and it displays who completed the chore as well.

            Going to start working on the automation and the lovelace cards next. Thanks for all of your help hopefully i can get back to you if i come upon any other issues. I’m not sure how i can test the on/off state without waiting for the next chore assignment date.

          • Great stuff! This is exactly how I envisaged it working.

            I think I can see an error in your on/off sensor, so let’s update that template a little bit so it returns a true/false. I haven’t tested this myself, but if it doesn’t work there should be enough for you to google around to get working.

            Essentially what we’re doing in that template is converting the time string to numbers. We then look to see if the next scheduled execution date is within the next 12 hours (43200 seconds) or overdue. If it is within the next 12 hours or overdue, we mark the binary sensor as “on”.

          • ikrowni

            Hitting a wall again. I used the template you said but within the grocy ERP addon for daily chores it says

            “This means the next execution of this chore is scheduled 1 day after the last execution”

            So for daily chores if the kids were to do their chores at lets say 5pm on monday then the chores wont reset until again until 5pm on tuesday. But if they decide to do their chores earlier than 5pm wont the chore not show up within home assistant to be completed? Im using the entity_filter lovelace card to filter out the chores that are not due at the time.

Start typing and press Enter to search