Hi Everyone!
I’m trying to optimize my supply chain by logging the inventory levels, available units and store count for products in a few stores a few times a day and log these levels in a timeseries database like influxDB so that I can run some analyses in Grafana. This is for a pet project in my city.
Information for all products is accessible via an API.
I’m using multiscrape in the service tab of Developer tools to prototype my code before I move it to configurations.yaml. I’m able to get a response from the endpoint, but I’m stuck at how to process the response using templates so that I can store the response for each product in sensor entities in home assistant. I don’t know how many products the API would respond with since sometimes the store runs out.
Does anyone know the best way to achieve this? Ideally I don’t have to writer a python script and can use existing HACS or other add-ons.
Example code
service: multiscrape.scrape
data:
name: Product Inventory Scrape
resource: <API request + state parameters>
sensor:
- unique_id: products
name: Products
value_template: "{!{ value_json.hits.hits }!}"
Response looks something like this
{
"took": 100,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 300, <- this number changes
"max_score": 1,
"hits": [
{
"_index": "something",
"_type": "product",
"_id": 327885,
"_score": 1,
"sku": 327885,
"availableUnits": 94,
"last_updated": "2024-06-10 20:10:01",
"_regularPrice": 47.99,
"isOrganic": false,
"storeCount": 6,
},
{
"_index": "something",
"_type": "product",
"_id": 327885,
"_score": 1,
"sku": 327885,
"availableUnits": 123,
"last_updated": "2024-06-10 20:10:01",
"_regularPrice": 47.99,
"isOrganic": false,
"storeCount": 16
},
298 other products in hits list
]
}
}
Thank you