Hi everybody,
I use the grocy
custom component with an external instance of grocy. It is connected and works fine, there are these sensors by default
- binary_sensor.grocy_expired_products
- binary_sensor.grocy_expiring_producs
- binary_sensor.grocy_missing_products
- sensor.grocy_chores
- sensor.grocy_shopping_list
- sensor.grocy_stock
The sensor.grocy_stock
entity provides attributes like these
attribution: Data from this is provided by grocy.
items:
- _product_id: 3
_available_amount: 3
_best_before_date: '2999-12-31T00:00:00+00:00'
_name: Test1
_barcodes:
- ''
_product_group_id: null
- _product_id: 4
_available_amount: 17
_best_before_date: '2999-12-31T00:00:00+00:00'
_name: '[BAT] CR1632'
_barcodes:
- '6972646746335'
_product_group_id: 1
- _product_id: 5
_available_amount: 8
_best_before_date: '2999-12-31T00:00:00+00:00'
_name: '[BAT] CR2025'
_barcodes:
- '4891199001130'
_product_group_id: 1
- _product_id: 6
_available_amount: 48
_best_before_date: '2999-12-31T00:00:00+00:00'
_name: '[BAT] AG10'
_barcodes:
- '6998817810100'
_product_group_id: 1
unit_of_measurement: Product(s)
friendly_name: grocy.stock
icon: mdi:format-quote-close
When I use {{ states.sensor.grocy_stock.attributes }}
in templates, I get this
{'attribution': 'Data from this is provided by grocy.', 'items': [{'_product_id': 3, '_available_amount': 3.0, '_best_before_date': datetime.datetime(2999, 12, 31, 0, 0, tzinfo=datetime.timezone.utc), '_name': 'Test1', '_barcodes': [''], '_product_group_id': None}, {'_product_id': 4, '_available_amount': 17.0, '_best_before_date': datetime.datetime(2999, 12, 31, 0, 0, tzinfo=datetime.timezone.utc), '_name': '[BAT] CR1632', '_barcodes': ['6972646746335'], '_product_group_id': 1}, {'_product_id': 5, '_available_amount': 8.0, '_best_before_date': datetime.datetime(2999, 12, 31, 0, 0, tzinfo=datetime.timezone.utc), '_name': '[BAT] CR2025', '_barcodes': ['4891199001130'], '_product_group_id': 1}, {'_product_id': 6, '_available_amount': 48.0, '_best_before_date': datetime.datetime(2999, 12, 31, 0, 0, tzinfo=datetime.timezone.utc), '_name': '[BAT] AG10', '_barcodes': ['6998817810100'], '_product_group_id': 1}], 'unit_of_measurement': 'Product(s)', 'friendly_name': 'grocy.stock', 'icon': 'mdi:format-quote-close'}
How can I pick a particular item from here? Letās say I want the item with {'_product_id': 5}
; I tried {{ states.sensor.grocy_stock.attributes.items }}
, and wanted to break it down further from there, but this will just return <built-in method items of mappingproxy object at 0x7f732be53fd0>
.
A different approach is this
sensor:
- platform: rest
resource: http://<grocy-ip>:9283/api/stock/products/4
headers:
GROCY-API-KEY: !secret grocy_api_key
name: "[V] (BAT) CR1632"
value_template: "{{ value_json.stock_amount }}"
unit_of_measurement: "StĆ¼ck"
scan_interval: 3600
json_attributes:
- last_purchased
- last_used
This works fine, but requires me to create an individual rest sensor per item! I currently only have 3 items, because I just started with grocy again and wanted to see how well I can integrate it in Home Assistant. However, I plan to have all kinds of items I can automate (dish tabs, dryer towels, laundry detergent, etc.) in grocy to automatically update their states whenever one of them has been used.
Can this be done by parsing the sensor.grocy_stock
instead of creating countless individual sensors? Since there are
- grocy.add_product
- grocy.consume_product
- grocy.execute_chore
services, I could easily achieve the consumption part of this. But Iād like to display the current stock of each relevant item in a dedicated tab within Home Assistant (rather than in grocy). To be precise, Iād like to somehow display "stock of <item-3>: {{ <whatever goes here to display available stock for item 3> }}"
.
Thank you for your ideas