Send message with items from shopping list

This is only giving you the number of items, but after some trial and error I found this jq string who gives me only shopping list that are not ticked “complete”.

  - platform: command_line
    name: shopping_list
    command: "jq '.[] | select(.complete==false) | .name' .shopping_list.json "

Now I just need to find a way to send this to my phone as a message, since I don’t have access to HA outside of home and if you click or clear the notification it get’s lost forever

1 Like

The HA Android App doesn’t require remote access to your instance for sending notifications as it is based on Firebase, for iOS I don’t know.

Indeed notifications arrive to the app even when not connected (just tested now by disabling wifi on my iphone), the problem is they are just notifications and you accidentally click on them you lose them, let’s say I’m in at the supermarket and I want to check the list, I don’t want to lose them and I don’t have access to my home assistant dashboard.
I think I will try to link it with a telegram bot

You can make the notifications sticky so you can’t just wipe it away.

Hey, guys,
Sorry if I’m a total noob, but I can’t get this to work:
I’m trying to get the number of items in the list as a number (as you, @Thumbleweed, built), but filtering for those that are not marked as completed (as @djlorenz built)…

Has anyone figured out how to retrieve the length of a filtered down array?
The closest I got was with the following:

sensor:
  - platform: command_line
    name: "Shopping List"
    unit_of_measurement: "items"
    command: "jq '.[] | select(.complete==false) | .name | length' .shopping_list.json"

But it ends up returning the length of the string of each uncompleted item (number of characters of each uncompleted item), separated by a space.

Thanks, guys!

I got it! Hahaha

It was quite simple once I started working/testing through the terminal instead.
Here is the code:

sensor:
  - platform: command_line
    name: "Shopping List"
    unit_of_measurement: "items"
    command: "jq '[.[] | select(.complete==false) | .name] | length' .shopping_list.json"

As you can notice, it was really just about arranging the results from the filter into an array that is afterwards checked for how many objects it has inside.

Hope this is useful for someone!

  - platform: command_line
    name: shopping_list
    command: "jq '.[] | select(.complete==false) | .name' .shopping_list.json "

That is working perfect for me. thanx very much!

But I have a cosmetical issue:

the email I receive all items listed in quotation marks like this:

“Brot”
“Toast”
“Möhren”
“Zucchini”
“Gehacktes”
“Hühnchenbrust”
“Wein”

Is there a way to avoid sending the Quotation marks?

in my automation I use following script:

action:
  - service: notify.dirk
    data_template:
      message: '{{states.sensor.einkaufsliste.state}}'

would be very nice to get it like this:

Brot

Toast

Möhren

Zucchini

Gehacktes

Hühnchenbrust

Wein

It would be easier to read, wouldn’t it?

How to avoid to get the message text in the email with Quotion marks? And with space between?

Thanx

Add option -r to jq command, example:

- platform: command_line
    name: shopping_list
    command: "jq -r '.[] | select(.complete==false) | .name' .shopping_list.json"
2 Likes

@frosty:

not working. When option -r is added the email has no content and is completely empty

Here is the implementation I went with. It includes both complete and incomplete items (in separate attributes), and uses the length of the incomplete list for the state.

sensor:
  - platform: command_line
    name: Shopping List
    json_attributes:
      - complete
      - incomplete
    command: "jq -r '{complete: [.[] | select(.complete==true)], incomplete: [.[] | select(.complete==false)]}' /config/.shopping_list.json"
    value_template: "{{ value_json.incomplete | length }}"
4 Likes

I tested all the answers here, but i get all items on one line. Did you get it to work ? :slight_smile:

@dale3h How can I then translate the attributes into a string list to send with notify?

I know this is an old thread, but if not helpful to you anymore, @Marcus_Lundblad and @Legromorph, it might be for others.

This does both of what you wanted to do, it joins the items into a string and puts them on separate lines:

{{ state_attr('sensor.einkaufsliste', 'incomplete') | join('\n') }}

And this is my sensor, based on @dale3h’s code, but only picking the item names:

sensor:
  - platform: command_line
      name: Einkaufsliste
      json_attributes:
        - complete
        - incomplete
      command: "jq -r '{complete: [.[] | select(.complete==true) | .name], incomplete: [.[] | select(.complete==false) | .name]}' .shopping_list.json"
      value_template: "{{ value_json.incomplete | length }}"
5 Likes

Thank you !

You’re great, thanks for posting this!!
I slightly changed config in order to meet my needs and tested - works as intended now!!

sharing my full config related to shopping list notifications:

file:

/config/packages/shopping_list.yaml

sensor:
    - platform: command_line
      name: shopping_list
      json_attributes:
        - complete
        - incomplete
      command: "jq -r '{complete: [.[] | select(.complete==true) | .name], incomplete: [.[] | select(.complete==false) | .name]}' .shopping_list.json"
      value_template: "{{ value_json.incomplete | length }}"

file:

automations.yaml

alias: LISTA ZAKUPOW - push gdy dodano coś do listy - powiadomienie do ARTUR
description: ""
trigger:
  - platform: event
    event_type: shopping_list_updated
    context: {}
    id: artur_shoppinglist_updated
    event_data:
      action: add
condition: []
action:
  - service: command_line.reload
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: notify.mobile_app_s22ultra
    data_template:
      message: "{{ state_attr('sensor.shopping_list', 'incomplete') | join('\\n') }}"
      title: >-
        Lista zakupów zaktualizowana: {{ states('sensor.shopping_list') }}
        pozycji
      data:
        clickAction: /shopping-list
        tag: zakupy
        notification_icon: mdi:shopping-outline
        color: yellow
        actions:
          - action: URI
            title: Otwórz listę zakupów
            uri: /shopping-list
mode: single

2 Likes

I can’t get it to work for me, unfortunately, maybe something changed with the new HA updates?

alias: SHOPPING LIST - notification when something is added to the list
description: ""
trigger:
  - platform: event
    event_type: shopping_list_updated
    context: {}
    event_data:
      action: add
    id: nestico_shoppinglist_updated
condition: []
action:
  - service: command_line.reload
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: notify.davids_notify_devices
    data_template:
      message: >-
        {{ state_attr('sensor.shopping_list_items', 'incomplete') | join('\n')
        }}
      title: >-
        Lista zakupów zaktualizowana: {{ states('sensor.shopping_list_items') }}
        pozycji
      data:
        clickAction: /shopping-list
        tag: zakupy
        notification_icon: mdi:shopping-outline
        color: yellow
        actions:
          - action: URI
            title: Open shopping list
            uri: /shopping-list
mode: single

plese refer to this thread https://community.home-assistant.io/t/shopping-list-state-attributes-are-gone-with-todays-ha-update/581157

1 Like

Thanks, it works! :+1:

hello, if i would create 2 automations: one if i add an item, my wife received a notification while i dont, and the second automation viceversa?

im using:

trigger:
  - platform: event
    event_type: shopping_list_updated
    context: 
        user_id: xxxxxx
    event_data:
      action: add

but before it worked and now dismiss and no one receive a notification, because the trigger is not activated.

could you help me to solve?

Maybe because of the updates? Shopping list is a todo list now.
My shopping list is called “shopping list” so the sensor state show the number of items.
todo.shopping_list

To get item I use
todo.get_items

Have a look at the current service to-do list supports.