Send message with items from shopping list

Hi!
How can I make an automation which would send me a message when I leave work with items from shopping list?

And if there is no items there, then I should not get any message.
How to work with shopping list? How can I use that data?

P.S. I only need to understand how to:

  • check if there is any item
  • use items list in notification
2 Likes

Can you provide an example of what the shopping list entity looks like, both with and without items in it?

Take a look at this.

Actually, I can not…




Interesting. Well, following the link that @Burningstone supplied would seem to imply the information is saved in some file in JSON format. No idea how it’s supposed to be used. I have no familiarity with this “integration”, and the doc page seems sparse at best.

Thank you, that worked!

Thank you anyways
Actually, I need a little more help

Here is an automation:

- id: '1530354314132'
  alias: What to buy
  initial_state: 'on'
  trigger:
    platform: zone
    zone: zone.zoo
    entity_id: person.gayane
    event: leave
  action:
    service: notify.mobile_app_iphone
    data_template:
      message: '{{states.sensor.shopping_list.state}}'

But it will trigger even if there is nothing in shopping list. Can I do here anything? Here is a sensor with something to buy:

and without any item at shopping list

1 Like

You should be able to add a condition to only send the list if the state of the sensor is not equal to what it shows when the shopping list is empty.

I tried this, but action is still working:

- id: '1530354314132'
  alias: What to buy
  initial_state: 'on'
  trigger:
    platform: zone
    zone: zone.zoo
    entity_id: person.gayane
    event: leave
  condition:
    condition: template
    value_template: "{{ states('sensor.shopping_list') != 'Купить:' }}"
  action:
    service: notify.mobile_app_iphone
    data_template:
      message: "{{states('sensor.shopping_list')}}"

I copied “Купить:” (“to buy” in russian) from states. But when I looked to .py file I found that there is “/n” after “купить”. So I tried “Купить:\n” and that did not help. I even tried to write “купить” and then tap enter. That did not help too, I am still getting the message.

Hmm maybe you can use the length of the string to only send if length > ‘length witth empty list’.

Will try it out too when I will figure out the syntax

Can I delete “to buy” from .py at all? So Just will have a list of items or nothing. And then I will use length.
In that case I can use shopping list not only for shopping. Maybe I will write there what I have to do after leaving work.

Isn’t this correct?

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

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

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

content += u"\n"

print(content)

If you will have time, please, look at this:

- id: '1530354314132'
  alias: What to buy
  initial_state: 'on'
  trigger:
    platform: zone
    zone: zone.zoo
    entity_id: person.gayane
    event: leave
  condition:
  - condition: template
    value_template: "{{ states('sensor.shopping_list') != 'To buy:' }}"
  action:
    service: notify.mobile_app_iphone
    data_template:
      message: "{{states('sensor.shopping_list')}}"

I tried to test template and got false:

So condition should not work out, isn’t is?
I still get the message. Where did I go wrong?

This script will not work because of this
content = u

The prefix ‘u’ is used to tell python that the string that follows it will be formatted in unicode.

Try this:

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

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

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

print(content)

I assume that this doesn’t work because of the new line that is added (content += u"\n") at the end of the script even if the shopping list is empty.

Ok, then your advice with length should work. But I can’t find the documentation of “length” even here
How should I write that?

I don’t use a lot of templating but try this:

- id: '1530354314132'
  alias: What to buy
  initial_state: 'on'
  trigger:
    platform: zone
    zone: zone.zoo
    entity_id: person.gayane
    event: leave
  condition:
  - condition: template
    value_template: "{{ states.sensor.shopping_list.state | length >= 10 }}"
  action:
    service: notify.mobile_app_iphone
    data_template:
      message: "{{states('sensor.shopping_list')}}"
2 Likes

Thank you that works. And

"{{ states('sensor.shopping_list') != 'To buy:' }}"

should work too, I just was triggering automation manually and that is why conditions was ignored

1 Like

I’m a little confused. If it’s working, fine, but I don’t see in the Python script where it’s outputting a first line of 'To buy:'. Also in the picture of the States page you showed sensor.shopping_list and it had something in a completely different alphabet.

Sorry, I am just constantly changing “to buy” to russian word and trying different things. In the end, as I understand Issue was only in me, because I was triggering automation manually and that is why condition was ignored

So maybe I’m too late too the party, but I figured out the sensor part with the following jq statement:

- platform: command_line
  name: shopping_list
  command: "jq length .shopping_list.json"

It removes the dependency on the .py file to parse the json + you get the amount of items in your shopping list as a number.

3 Likes