Shopping list state

It would be nice to have a way to check if there is any unchecked items on the shopping list, in such a way that it can be used as a condition in an automation. I only want my shopping list to pop up in the store if there are items on it for me to buy. so either an true false state or a number of items unchecked would be great.

- condition: template
  value_template: "{{ states('sensor.shopping_list_items')|count > 0 }}"

Note this is not counting the number of items but the number of letters in the words of unchecked items.

Clever. Gets the job done! Thanks:-)

What have I done wrong if I don’t have a sensor.shopping_list_items for my shopping list?

Nether do i. So the sensor returns unknown, and then the count of unknown is always >0 so the template returns true regardless of the state of the shopping list. Any other suggestions?

That sensor does not exist by itself…I have a custom python-script sensor (named differently). Let’s see what tom reports back

Ah, sorry. It’s ages since I set this up. I had forgotten. I thought it was a core sensor but it is not. There’s actually a list empty binary sensor too:

rest:
  - resource: http://10.1.1.100:8123/api/shopping_list  # replace with your HA IP address
    headers:
      authorization: !secret shopping_list
      content-type: 'application/json'
    method: GET
    scan_interval: 60
    binary_sensor:
      - name: Shopping List Empty
        value_template: >
          {% if value_json is defined %}
            {{ value_json|selectattr('complete', 'false')|map(attribute='name')|list|length == 0 }}
          {% else %}
            false
          {% endif %}
    sensor:
      - name: Shopping List Items
        value_template: >
          {% if value_json is defined %}
            {{ value_json|selectattr('complete', 'false')|map(attribute='name')|list|join(', ') }}
          {% else %}
            false
          {% endif %}

The resource authorization secret is of the form:

shopping_list: "Bearer long_lived_token_here"

You can generate the long lived token at the bottom of your profile page.

So with that the condition can be even simpler:

- condition: state
  entity_id: binary_sensor.shopping_list_empty
  state: 'off'
2 Likes

Awesome, but yet i repeatingly get this error now:

Did I miss something? Even adding "Bearer " in the secret throws the same error

You definitely do need bearer in the authorisation value.

Do you normally use https to access home assistant locally?

No, I don’t.

Maybe I could try using the Nabu Casa URL for the sensors

Nope.

Token in secrets.yaml looks like this:

shopping_list: "Bearer redacted.iR5EVpqDNaFXEYYrghqBRWb6z6BWhP-0NYCC-Hztoken"

I edited your message to obscure the token. Not wise to share that.

It all looks ok to me. Not sure what the issue is.

1 Like

Tx, but I already did that myself too (cf. looks)

1 Like

It’s the address:

rest:
  - resource: http://10.1.1.100:8123/api/shopping_list  # replace with your HA IP address

should be (the hyphen):

rest:
  - resource: http://10.1.1.100:8123/api/shopping-list  # replace with your HA IP address

1 Like

Good eyes.

For me, this one-liner works great:

{{ states('todo.your_todo_list') | int > 0 }}

I created a binary_sensor within the template section of my configurataion.yaml:

template:
  - binary_sensor:
    - name: Name for sensor
      state: >
        {{ states('todo.your_todo_list') | int > 0 }}

It will return true, if there are open tasks in the list and if the list only has finished tasks, it will return false.