Hi folks,
I’ve used https://jsonpath.herokuapp.com/ to get working filter:
min($.[?(@.ModelID=='ABC123')].Price)
It gets the right JSON for me. But how to implement this JSONPath expression? Using a rest sensor with a value_template should do the trick. But I keep running into a configuration error for the filter character ‘$’
Invalid config for [sensor.rest]: invalid template (TemplateSyntaxError: unexpected char '$' at 15) for dictionary value @ data['value_template']. Got '{{ value_json.[$.[?(@.ModeloID=="ABC123)].Price]] }}'. (See ?, line ?).
below the JSON output of the API:
[
{
"Model1":"Product1 XXX with descriptions ASD",
"Price":4275.28,
"Price2":3676.84,
"Store":"Wallmart",
"Link":"http://wallmart.com/product1",
"ModelID":"ABC123"
},
{
"Model1":"Product1 YYY with descriptions",
"Price":2275.28,
"Price2":2676.84,
"Store":"Amazon",
"Link":"http://amazon.com/product1",
"ModelID":"ABC123"
},
{
"Model1":"Product2 XXX with descriptions ",
"Price":2489,
"Price2":2856,
"Store":"Wallmart",
"Link":"http://wallmart.com/product2",
"ModelID":"QWE123"
}
]
I need to get the minimum value price from each model ID, example:
min($.[?(@.ModelID=='ABC123')].Price)
min($.[?(@.ModelID=='QWE123')].Price)
So I found a way, from @petro in another topic and it works nicely! In his suggestion he gets the first number found in his list, and for my luck, my data set is ordered by asc, so the first price is the lowest price.
Below my rest code:
- platform: rest
name: pricestore_abc_price
resource: https://www.example.com/mylist.json
scan_interval: 30
timeout: 60
force_update: true
verify_ssl: false
value_template: >
{% set items = value_json | selectattr('ModelID','eq','ABC123') | list | first %}
{{ items['Price'] }}
unit_of_measurement: $
But it only return the price, i cant get the respective attributes from that price:
- Model1
- Price
- Price2
- Store
- Link
- ModelID
How can I put this attributes to show in the same entity?
I tried the suggestion that @petro said in another topic, to use:
# {{ items.items() | selectattr(0, 'in', attributes) | sort(attribute=0) | map(attribute=1) | join(',') }}
and create a platform template to show… but i received a error saying there is more than 255 characters which is the limit.
anyway, I really would like to have my entity from pricestore_abc_price with the price and its respective attributes.
would it be possible?