I was somewhat surprised to learn the Shopping List integration does not provide a service to call the actual shopping list as it’s only visible through Lovelace or the associated card. This effectively means that it can’t be used in automations or notifications (seems like a What The Heck candidate to me!). So until this becomes available in the integration, I am requesting some help to make this possible.
So far I realised there is an undocumented API to call the shopping list through a RESTful service. I have this successfully working using the following YAML configuration which returns a JSON string with all shopping list items but with limitations…
YAML sensor configuration
# Shopping list
- platform: rest
name: Grocery List
headers:
authorization: !secret shopping_list
content-type: 'application/json'
resource: http://<HA-IP>:8123/api/shopping_list
value_template: '{{ value_json[0].name }}, {{ value_json[1].name }}, {{ value_json[2].name }}, {{ value_json[3].name }}'
method: GET
scan_interval: 60
Returned JSON string example
[
{
"name": "Milk",
"id": "d08f74d8ea3d46cab50daed30386cd52",
"complete": false
},
{
"name": "Cereal",
"id": "5f23907a1fc34fa4911aa5a7266f86a4",
"complete": false
},
{
"name": "Bread",
"id": "2d258ae619fe4b17aaf404f108bba09d",
"complete": false
},
{
"name": "Fruit",
"id": "518db2becf024d1ba157c618d452469f",
"complete": false
}
]
Example sensor results shown in entity alongside Shopping List Card
The first limitation is that I’ve hardcoded the number of shopping list items in the value template. So if there are fewer shopping list items than the stated number (4) it causes the RESTful service to fail and likewise if there are more shopping list items they are not captured. I need to create a for/loop statement of such to dynamically capture the number of items. Similar example here using the command sensor.
The second limitation is the template does not filter items that are “complete” and therefore all items are returned regardless of whether they were purchased or not. It would be nice just to return items that need to be bought.
Once the above limitations are sorted it should be straightforward to send the sensor state through Telegram message or any notification. I am requesting help from someone with more skill and experience in Jinja/templates, thanks.