#RESTful Sensor with a lot of data
1 messages · Page 1 of 1 (latest)
This is my current sensor:
rest:
#- resource: https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=47.815800&longitude=13.050220&fuelType=DIE&includeClosed=true
- resource: https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=47.841220&longitude=13.248650&fuelType=DIE&includeClosed=true
scan_interval: 60
sensor:
- unique_id: dieselpreis_avanti_thalgau
name: Dieselpreis Avanti
device_class: monetary
value_template: "{{ value_json[0]['prices'][0]['amount'] }}"
unit_of_measurement: "EUR"
force_update: True
json_attributes_path: "$[0]"
json_attributes:
- name
- id
- location
- unique_id: dieselpreis_jet_thalgau
name: Dieselpreis Jet
device_class: monetary
value_template: "{{ value_json[1]['prices'][0]['amount'] }}"
unit_of_measurement: "EUR"
force_update: True
json_attributes_path: "$[1]"
json_attributes:
- name
- id
- location
the problem is, value_json[0] is always the cheapest fuel station
but every entry also has an id, like "id": 30202,
how could I modify the value_template so it would select the entry based on a specific ID and not the index in the array?
I feel like it's simple, but my brain is locked up
- unique_id: dieselpreis_avanti_thalgau
name: Dieselpreis Avanti
device_class: monetary
value_template: "{{ (value_json | selectattr('id', 'eq', 30202) | first).prices[0].ammount }}"
unit_of_measurement: "EUR"
force_update: True
json_attributes_path: "$.[?(@.id == 30202)]"
json_attributes:
- name
- id
- location
@dark bone ^
you sir are an absolute rockstar, thank you @sick cosmos!
it does, you just added one extra m to spice things up 😉
ammmmmmmmmmmmmmmmmount
@sick cosmos I ran into another issue I'm sure you'd be able to help out in a breeze. It turns out sometimes the API lags out and returns empty prices. Apparently that would result in a NaN/unknown reading. How would I go about skipping the sensor entry if a value was invalid? And in the same line, skip if the reading is the same as before?
there's no reason to skip if the reading was the same as before. Also, rest does not have the ability to see what it was before.
if the code I wrote produces an error without data, post the error. The best that can be done for rest sensors is to remove the errors. The sensor will still go unavailable when the api returns emtpy responses.
there's no error to fix. could I not simply check for states('sensor.dieselpreis_avanti') in the template?