Check if an item is on the shopping list

I’ve created a few buttons to quick add specific items to the HA shopping list. Is there a way to check if an item is already added? For example to change the appearance of the button (thinking of adding a checkmark icon).

In essence if I can show on my dashboard if a specific item is already on the shopping list.

Not (easy) while you are typing it in, the (web)page is disconnected from the data untill you hit ‘commit/submit/whatever’. Probably easier is to remove duplicates from the list as it is a json file…probably …
EDIT: I have a script running that informs me what is on the list via notification immediately after it changes, this one could probably (?!) extend to check/remove duplicates

I’m not typing it in. I added buttons that can be pressed that would add a specific item to the shopping list. At the moment however I get no feedback that it has been added. Seeing a counter go up everytime you press it was one of my ideas.

Basically:

  1. Press the button "laundry detergent’ in the Dashboard. It will automatically add “laundry detergent” to the shoppinglist
  2. In the Dashboard it would show “laundry detergent 1”
  3. Press the button again
  4. In the Dashboard it would show “laundry detergent 2”

or

  1. Press the button "laundry detergent’ in the Dashboard. It will automatically add “laundry detergent” to the shoppinglist
  2. In the Dashboard it would show checkbox that it has been added
  3. Press the button again
  4. In the Dashboard it would show checkbox that it has been added

That would mean a helper per item and each time you are adding an new item (button) you need to add that. I was more thinking of checking the .shopping_list with this:

{{  x in shoppinglist_json_from_file  | map(attribute='name') | list }}

where x would be the item of course and you could possibly change the color of the button based on that.

1 Like

This actually looks exactly like what indeed I would love to do. Perhaps this is a really dumb question. but what would be a realworld example of shoppinglist_json_from_file? I tried it in the template editor and it wouldn’t parse. But perhaps it doesn’t work there, I need to fetch it somehow? I feel a bit dumb asking, but how do i access this?

Suggestion, others may chime in too :slight_smile:
create a sensor that reads all the items into attribs

sensor:
  - platform: command_line
    name: shoppinglist
    command: >
          echo "{\"list\":" $( cat .shopping_list.json) "}" 
    value_template: > 
        {{ value_json.list | length }}
    json_attributes:
        - list     

Then you can use the logic as above

{{ x in state_attr('sensor.shoppinglist','list') | map(attribute='name') | list }}

instead of x you need to add the item-name, could be templated I guess but has to be a string

2 Likes

Thank you! That indeed works like a charm! And for me something new, a sensor for command_line. I hadn’t discovered those yet.

Small addition of my own. A sensor won’t immediately update. So for it to show an response in the dashboard directly it’s good to trigger an update of the sensor whenever the shoppinglist is updated.

There is an event type for this:

event_type: shopping_list_updated

And then you can call the service update entity on that sensor.

All my automations are in Node-red, so it’s hard for me to share it. But basically listening for the specific event ‘shopping_list_updated’ and then firing a service call ‘update entity’ on the sensor.

Note: update on the shoppinglist is for any update (add and delete).

Yeah, true…forgot that, I am using another command line with python which for some odd reason does (!) update immediately, that script puts values of shopping list formatted in the state of another sensor and then it send a notification to me so I know it has been updated. I also have a notification when I am close to a supermarket :slight_smile:

1 Like

Seeing as this thread is the first hit in Google and I had a very similar requirement, I thought I’d share my super simple workaround:

For my scenario I have NFC tags scattered around that add specific items to the list when scanned, using the name of the tag: {{ trigger.event.data.name }}. Obviously this would cause duplicates of the same item if scanned multiple times.

Rather than building an array and having it check for the presence of that item as a condition - as per the above - I just made the first action delete that item from the list {{ trigger.event.data.name }} before (re)adding it. It has the side effect of changing the order of items, but for a shopping list it’s no big deal.