Brilliant, thanks. Getting the data out is fairly straightforwards, but the tricky bit is working out how you want to do this. As an example, here’s a state template you can use for the price of fuel type 1 from station 296 — you can either set this up in the UI or via YAML:
{{ (
(
state_attr('sensor.fuel_prices_command','verdi')
|selectattr('id','==',296)
|first
)['prices']
|selectattr('fuelTypeId','==',1)
|first
)['price'] }}
I’ve spaced it out to try to help you see how it works.
- from the data, look for items with
id
of 296
. Should be only one, so pick the first (as the function returns a list)
- then look at the prices list from that item
- choose the sub-item with fuel type 1
- read its price
If you know that the stations are going to be the same each time, you can simply create individual “static” sensors like this.
To help find the data you want, paste this JSON (same as you got for me but with double-quotes as per spec):
[{"id": 296, "brandId": 3, "countryId": 1, "stationTypeId": 1, "name": "Linderud Sør", "location": "Trondheimsveien 320, 0593 Oslo", "latitude": "59.94406", "longitude": "10.833225", "pending": 0, "deleted": 0, "createdAt": "2020-10-18T20:29:43.000+00:00", "updatedAt": "2024-02-16T14:51:04.000+00:00", "prices": [{"id": 296, "fuelTypeId": 1, "currency": "Kr", "price": 21.95, "lastUpdated": 1708095063913, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 296, "fuelTypeId": 2, "currency": "Kr", "price": 21.95, "lastUpdated": 1708095063913, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}], "amenityIds": []}, {"id": 315, "brandId": 3, "countryId": 1, "stationTypeId": 1, "name": "Strømmen", "location": "Strømsveien 9, 2010 Strømmen Akershus", "latitude": "59.94132", "longitude": "11.00152", "pending": 0, "deleted": 0, "createdAt": "2020-10-18T20:29:43.000+00:00", "updatedAt": "2024-02-16T14:48:47.000+00:00", "prices": [{"id": 315, "fuelTypeId": 1, "currency": "Kr", "price": 21.95, "lastUpdated": 1708094927197, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 315, "fuelTypeId": 2, "currency": "Kr", "price": 21.95, "lastUpdated": 1708094927197, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}], "amenityIds": []}, {"id": 556, "brandId": 2, "countryId": 1, "stationTypeId": 1, "name": "Skårer", "location": "Skårersletta 2, 1473 Lørenskog, Norway", "latitude": "59.927586", "longitude": "10.954638", "pending": 0, "deleted": 0, "createdAt": "2020-10-18T20:29:43.000+00:00", "updatedAt": "2024-02-16T13:56:23.000+00:00", "prices": [{"id": 556, "fuelTypeId": 1, "currency": "Kr", "price": 22.25, "lastUpdated": 1708091782716, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 556, "fuelTypeId": 2, "currency": "Kr", "price": 24.18, "lastUpdated": 1707919329791, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 556, "fuelTypeId": 3, "currency": "Kr", "price": 24.08, "lastUpdated": 1707894276106, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 556, "fuelTypeId": 19, "currency": "Kr", "price": 0.0, "lastUpdated": 1704754621218, "createdAt": "2024-01-08T22:57:01.000+00:00", "deleted": 0}, {"id": 556, "fuelTypeId": 23, "currency": "Kr", "price": 0.0, "lastUpdated": 1704754621218, "createdAt": "2024-01-08T22:57:01.000+00:00", "deleted": 0}, {"id": 556, "fuelTypeId": 24, "currency": "Kr", "price": 0.0, "lastUpdated": 1704754621218, "createdAt": "2024-01-08T22:57:01.000+00:00", "deleted": 0}, {"id": 556, "fuelTypeId": 36, "currency": "Kr", "price": 0.0, "lastUpdated": 1704754621218, "createdAt": "2024-01-08T22:57:01.000+00:00", "deleted": 0}], "amenityIds": []}, {"id": 634, "brandId": 4, "countryId": 1, "stationTypeId": 1, "name": "Fjellhamar", "location": "Strømsveien 180, 1474 Lørenskog", "latitude": "59.93406297183446", "longitude": "10.985618584578424", "pending": 0, "deleted": 0, "createdAt": "2020-10-18T20:29:43.000+00:00", "updatedAt": "2024-02-16T14:46:46.000+00:00", "prices": [{"id": 634, "fuelTypeId": 1, "currency": "Kr", "price": 22.66, "lastUpdated": 1708094805961, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 634, "fuelTypeId": 2, "currency": "Kr", "price": 22.66, "lastUpdated": 1708094805961, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 634, "fuelTypeId": 4, "currency": "kr", "price": 21.75, "lastUpdated": 1708002179840, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 634, "fuelTypeId": 23, "currency": "Kr", "price": 6.89, "lastUpdated": 1705076091905, "createdAt": "2024-01-08T22:57:01.000+00:00", "deleted": 0}, {"id": 634, "fuelTypeId": 36, "currency": "Kr", "price": 6.59, "lastUpdated": 1705075993818, "createdAt": "2024-01-08T22:57:01.000+00:00", "deleted": 0}], "amenityIds": []}, {"id": 716, "brandId": 4, "countryId": 1, "stationTypeId": 1, "name": "Lørenskog", "location": "Robsrudveien 1, 1470 Lørenskog", "latitude": "59.94185", "longitude": "10.945642", "pending": 0, "deleted": 0, "createdAt": "2020-10-18T20:29:43.000+00:00", "updatedAt": "2024-02-16T08:37:35.000+00:00", "prices": [{"id": 716, "fuelTypeId": 1, "currency": "Kr", "price": 23.14, "lastUpdated": 1708072655323, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 716, "fuelTypeId": 2, "currency": "Kr", "price": 23.11, "lastUpdated": 1708072655323, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 716, "fuelTypeId": 4, "currency": "kr", "price": 19.85, "lastUpdated": 1707893894890, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 716, "fuelTypeId": 19, "currency": "Kr", "price": 6.59, "lastUpdated": 1705076025680, "createdAt": "2024-01-08T22:57:01.000+00:00", "deleted": 0}, {"id": 716, "fuelTypeId": 23, "currency": "Kr", "price": 6.89, "lastUpdated": 1705076115096, "createdAt": "2024-01-08T22:57:01.000+00:00", "deleted": 0}, {"id": 716, "fuelTypeId": 27, "currency": "Kr", "price": 6.89, "lastUpdated": 1705076115096, "createdAt": "2024-01-08T22:57:01.000+00:00", "deleted": 0}, {"id": 716, "fuelTypeId": 36, "currency": "Kr", "price": 6.59, "lastUpdated": 1705076025680, "createdAt": "2024-01-08T22:57:01.000+00:00", "deleted": 0}], "amenityIds": []}, {"id": 1203, "brandId": 1, "countryId": 1, "stationTypeId": 1, "name": "Skårer", "location": "Solheimveien 120, 1473 Lørenskog", "latitude": "59.92890240602383", "longitude": "10.967357514574243", "pending": 0, "deleted": 0, "createdAt": "2020-10-18T20:29:43.000+00:00", "updatedAt": "2024-02-16T14:45:56.000+00:00", "prices": [{"id": 1203, "fuelTypeId": 1, "currency": "Kr", "price": 22.66, "lastUpdated": 1708094756372, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 1203, "fuelTypeId": 2, "currency": "Kr", "price": 22.66, "lastUpdated": 1708094756372, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}, {"id": 1203, "fuelTypeId": 3, "currency": "Kr", "price": 25.98, "lastUpdated": 1708042789604, "createdAt": "2024-01-08T22:26:06.000+00:00", "deleted": 0}], "amenityIds": []}]
into here:
and click JSON Viewer, then you can explore the structure:
You can then experiment in the template editor:
{% set a = state_attr('sensor.fuel_prices_command','verdi') %}
{% set id_list = a|map(attribute='id')|list %}
{% set ns = namespace(stations=[]) %}
{% for id in id_list %}
{% endfor %}
{{ ((a|selectattr('id','==',296)|first)['prices']|selectattr('fuelTypeId','==',1)|first)['price'] }}