Split attributes/data in separate template sensors

Hi! I have used several evenings to try getting this solved… i am trying to display attributes/data from a sensor that has multiple similar attributes. My goal is to fetch petrol price for each - id: , and fueltypeid, but i am not getting anywhere with templates (not good at it). Either it is to split them in separate sensors by - id: (petrol stations identity) or a card that can display the selected items.
Here is both a screenshot of the output, and partly output as text from the command line sensor:
Everything that points me in the right direction is highly appreciated!

screenshot

//

  • 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-15T20:45:19.000+00:00’
    prices:
  • id: 296
    fuelTypeId: 1
    currency: Kr
    price: 22.75
    lastUpdated: 1708029919106
    createdAt: ‘2024-01-08T22:26:06.000+00:00’
    deleted: 0
  • id: 296
    fuelTypeId: 2
    currency: Kr
    price: 22.75
    lastUpdated: 1708029919106
    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-15T14:33:34.000+00:00’
    prices:
  • id: 315
    fuelTypeId: 1
    currency: Kr
    price: 23.36
    lastUpdated: 1708007613967
    createdAt: ‘2024-01-08T22:26:06.000+00:00’
    deleted: 0
  • id: 315
    fuelTypeId: 2
    currency: Kr
    price: 23.36
    lastUpdated: 1708007613967
    createdAt: ‘2024-01-08T22:26:06.000+00:00’
    deleted: 0
    amenityIds:
    //

Please copy this into Developer Tools / Templates, and paste the response surrounded by three backticks.

{{ state_attr('sensor.fuel_prices_command','Verdi') }}

then copy and paste the response, which should look like this:

[
  {
    "id": 296,
    "brandid": 3,
    [lots more data]
  }
]

It’s much easier for us to test and work with real data structures to develop suggestions for you.

Sure! Had to change V in ‘Verdi’ to lowercase, as it is case sensitive.

[{'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': []}]

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'] }}

2 Likes

Thank you so much @Troon ! when i play with the id´s for the different stations (yes they are static) and the fuel types in template editor i got the prices!
So next after dinner i try to create a sensor or two, i post the results then :+1:

1 Like

image

Thanks again! I also think i earlier got into a couple of “traps” that i should know…
HA is not happy with uppercase characters (besides in friendly names).
Not hyphens either, should be underscore instead.
You saved me a lot of frustration with your quick and very good answer @Troon :ok_hand:

1 Like

If you give it a unit_of_measurement: 'Kr' the history will appear as a graph.

Ahh sure, just forgot it until i had created all sensors for stations i was interested in :slight_smile:
Also refresh/scan interval has to be done.