Austrian fuel prices from Spritpreisrechner

Recently I did some digging how to incorporate Austrian fuel prices into HA. Since there might be others interested in this topic I decided to share my findings.

Where to get the data
Every site that you can check fuel prices on (like ÖAMTC, ARBÖ,…) bases its data on those by Spritpreisrechner.at. You can query this data via the E-Control Sprit API.

The first step is to go to the API documentation and build yourself a request URL. You can either do that by region (Bezirk or Bundesland) or by geo coordinates. Since I only want fuel stations nearby I chose geo coordinates. Navigate in the search section to GET /search/gas-stations/by-address and hit “Try it out”.
Update 2023: The URLs built by swagger are for some reason wrong. you need to replace “api” in the part after the domain with “sprit/1.0”

Get your geo coordinates for example on latlong.net and enter them. Choose your fuel type and whether you want to include closed stations. Hit “execute” and copy the request URL.

Setting up HA
In your configuration.yaml set up the rest platform and some sensors.

rest:
  - resource: my_request_URL
    scan_interval: 1200
    sensor:
      - name: Diesel Preis 1
        value_template: "{{ value_json[0]['prices'][0]['amount'] }}"
        unit_of_measurement: "€"
      - name: Diesel Name 1
        value_template: "{{ value_json[0]['name'] }}"
      - name: Diesel Preis 2
        value_template: "{{ value_json[1]['prices'][0]['amount'] }}"
        unit_of_measurement: "€"
      - name: Diesel Name 2
        value_template: "{{ value_json[1]['name'] }}"
      - name: Diesel Preis 3
        value_template: "{{ value_json[2]['prices'][0]['amount'] }}"
        unit_of_measurement: "€"
      - name: Diesel Name 3
        value_template: "{{ value_json[2]['name'] }}"

In this example I made for each of the 3 cheapest fuel stations in my area a sensor for its name and the price.

It is important to set a reasonable scan interval. If you don’t, your request URL (not your IP) will get banned for a day.

The JSON response is a list of all fuel stations. The first 5 are ordered by price and have price values in them, for the rest there are no prices. So by choosing the first element [0] you get the cheapest one and so on. Basically this makes it almost impossible to track the prices of a specific fuel station, but you can get an overview of the prices and where you can refuel the cheapest.

Here’s how it looks in my dashboard (using a fake location):
2021-11-24_16h38_12

10 Likes

Do you have a template for lovelace?

I’m using a simple glance card:

type: glance
entities:
  - entity: sensor.diesel_name_1
  - entity: sensor.diesel_preis_1
  - entity: sensor.diesel_name_2
  - entity: sensor.diesel_preis_2
  - entity: sensor.diesel_name_3
  - entity: sensor.diesel_preis_3
show_name: false
show_icon: false
show_state: true
columns: 2
title: Dieselpreise

You can also use this:

This could also work but you would need to set up the rest platform/sensor differently:

Thanks @FPro for that =)

What i really would like to add is the city of the fuel station but i got stuck… i only get an unknown value :frowning:

value_template: "{{ value_json[0]['location'][0]['city'] }}"

liebe Grüße aus der Steiermark :slight_smile:

1 Like

What’s your request URL?

@nightfever not actually mine but to get the idea:

https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=47.427390&longitude=14.755530&fuelType=DIE&includeClosed=false

This is mine (coordinates anonymised here) and working perfect:
https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=48.xyz&longitude=16.xyz&fuelType=SUP&includeClosed=false

Everything else (name, price) is working fine but i cannot extract the city name.

i found out that the format is different between location and prices but i am not able to get my head around how to translate that to the value_template.


 "location": {
      "address": "Industriepark 1",
      "postalCode": "8772",
      "city": "Traboch - Timmersdorf",
      "latitude": 47.3724078,
      "longitude": 14.9887659
   "prices": [
      {
        "fuelType": "DIE",
        "amount": 1.9,
        "label": "Diesel"

Awesome, thanks for sharing.

@Joebinator: Try this:

value_template: "{{ value_json[0]['location']['city'] }}"
2 Likes

Oh, now I get the point. Tried to read the address, but also get unknown.
Will try that fix.

@fuze1

yeah that did the trick, thanks :smiley:

Hi,

I’ve also played a little bit with the API. In my implementation there are five sensors with the prices of the cheapest fuel stations. The station’s name and the address are stored as attributes within the sensor. For displaying these attributes I am using Thomas Loven’s Template Entity Row and the result is quite presentable.

Iimage

Below you can see both configurations for the REST sensors and for the display.

Rest Sensor Configuration


# API Documentation
# https://api.e-control.at/sprit/1.0/doc/index.html?url=https://api.e-control.at/sprit/1.0/api-docs%3Fgroup%3Dpublic-api#/search/searchGasStationsByAddressUsingGET


rest:
  - resource: https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=47.405965&longitude=15.261849&fuelType=DIE&includeClosed=true
    headers:
      accept: application/json
    scan_interval: 1800
    sensor:
      - name: fuel_price_bruck_die_1
        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
      - name: fuel_price_bruck_die_2
        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
      - name: fuel_price_bruck_die_3
        device_class: monetary
        value_template: "{{ value_json[2]['prices'][0]['amount'] }}"
        unit_of_measurement: "EUR"
        force_update: True
        json_attributes_path: "$[2]"
        json_attributes:
          - name
          - id
          - location
      - name: fuel_price_bruck_die_4
        device_class: monetary
        value_template: "{{ value_json[3]['prices'][0]['amount'] }}"
        unit_of_measurement: "EUR"
        force_update: True
        json_attributes_path: "$[3]"
        json_attributes:
          - name
          - id
          - location
      - name: fuel_price_bruck_die_5
        device_class: monetary
        value_template: "{{ value_json[4]['prices'][0]['amount'] }}"
        unit_of_measurement: "EUR"
        force_update: True
        json_attributes_path: "$[4]"
        json_attributes:
          - name
          - id
          - location

EDIT: Fix indention of code

UI Lovelace Card

using https://github.com/thomasloven/lovelace-template-entity-row

type: entities
title: Bruck an der Mur
entities:
  - type: custom:template-entity-row
    icon: mdi:gas-station-outline
    entity: sensor.fuel_price_bruck_die_1
    state: '{{states.sensor.fuel_price_bruck_die_1.state_with_unit}}'
    name: '{{state_attr(''sensor.fuel_price_bruck_die_1'', ''name'')}}'
    secondary: >-
      {{state_attr('sensor.fuel_price_bruck_die_1', 'location')['postalCode']}}
      {{state_attr('sensor.fuel_price_bruck_die_1', 'location')['city']}},
      {{state_attr('sensor.fuel_price_bruck_die_1',
      'location')['address']|title}}
  - type: custom:template-entity-row
    icon: mdi:gas-station-outline
    entity: sensor.fuel_price_bruck_die_2
    state: '{{states.sensor.fuel_price_bruck_die_2.state_with_unit}}'
    name: '{{state_attr(''sensor.fuel_price_bruck_die_2'', ''name'')}}'
    secondary: >-
      {{state_attr('sensor.fuel_price_bruck_die_2', 'location')['postalCode']}}
      {{state_attr('sensor.fuel_price_bruck_die_2', 'location')['city']}},
      {{state_attr('sensor.fuel_price_bruck_die_2',
      'location')['address']|title}}
  - type: custom:template-entity-row
    icon: mdi:gas-station-outline
    entity: sensor.fuel_price_bruck_die_3
    state: '{{states.sensor.fuel_price_bruck_die_3.state_with_unit}}'
    name: '{{state_attr(''sensor.fuel_price_bruck_die_3'', ''name'')}}'
    secondary: >-
      {{state_attr('sensor.fuel_price_bruck_die_3', 'location')['postalCode']}}
      {{state_attr('sensor.fuel_price_bruck_die_3', 'location')['city']}},
      {{state_attr('sensor.fuel_price_bruck_die_3',
      'location')['address']|title}}
  - type: custom:template-entity-row
    icon: mdi:gas-station-outline
    entity: sensor.fuel_price_bruck_die_4
    state: '{{states.sensor.fuel_price_bruck_die_4.state_with_unit}}'
    name: '{{state_attr(''sensor.fuel_price_bruck_die_4'', ''name'')}}'
    secondary: >-
      {{state_attr('sensor.fuel_price_bruck_die_4', 'location')['postalCode']}}
      {{state_attr('sensor.fuel_price_bruck_die_4', 'location')['city']}},
      {{state_attr('sensor.fuel_price_bruck_die_4',
      'location')['address']|title}}
  - type: custom:template-entity-row
    icon: mdi:gas-station-outline
    entity: sensor.fuel_price_bruck_die_5
    state: '{{states.sensor.fuel_price_bruck_die_5.state_with_unit}}'
    name: '{{state_attr(''sensor.fuel_price_bruck_die_5'', ''name'')}}'
    secondary: >-
      {{state_attr('sensor.fuel_price_bruck_die_5', 'location')['postalCode']}}
      {{state_attr('sensor.fuel_price_bruck_die_5', 'location')['city']}},
      {{state_attr('sensor.fuel_price_bruck_die_5',
      'location')['address']|title}}

5 Likes

@matrixx567 WOW! That is great! I copied it right away :smiley:

What’s wrong with my code?

Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/configuration.yaml”, line “headers”

rest:
  - resource: https://api.e-control.at/sprit/1.0/search/gas-stations/by-address?latitude=47.405965&longitude=15.261849&fuelType=DIE&includeClosed=true
      headers:
        accept: application/json
    scan_interval: 1800
    sensor:
      - name: fuel_price_bruck_die_1
        device_class: monetary
        json_attributes_path: "$[0]"
        value_template: "{{ value_json[0]['prices'][0]['amount'] }}"
        unit_of_measurement: "EUR"
        force_update: True
        json_attributes:
          - name
          - id
          - location

Hy,
The indention of third and fourth line (headers and accept) is wrong.
There was mistake during copying to the forum, I’ve already changed my post.

1 Like

Hi there,

@FPro first thanks for the thread, very helpful.

Everything works as desired, the three cheapest gas stations in the area are displayed dynamically. But now I want to get the price of a specific gas station with the associated price? For example, the gas station with the ID 4143:

{
      "id":4143,
      "name":"Disk",
      "location":{
         "address":"Milser Straße 22",
         "postalCode":"6060",
         "city":"Hall",
         "latitude":47.2831587,
         "longitude":11.519987
      },
      "contact":{
         "telephone":"435022776061",
         "fax":"435022774061",
         "mail":"[email protected]",
         "website":"http://www.gutmann.cc"
      },
      "openingHours":[
         {
            "day":"MO",
            "label":"Montag",
            "order":1,
            "from":"06:00",
            "to":"20:00"
         },
         {
            "day":"DI",
            "label":"Dienstag",
            "order":2,
            "from":"06:00",
            "to":"20:00"
         },
         {
            "day":"MI",
            "label":"Mittwoch",
            "order":3,
            "from":"06:00",
            "to":"20:00"
         },
         {
            "day":"DO",
            "label":"Donnerstag",
            "order":4,
            "from":"06:00",
            "to":"20:00"
         },
         {
            "day":"FR",
            "label":"Freitag",
            "order":5,
            "from":"06:00",
            "to":"20:00"
         },
         {
            "day":"SA",
            "label":"Samstag",
            "order":6,
            "from":"06:00",
            "to":"20:00"
         },
         {
            "day":"SO",
            "label":"Sonntag",
            "order":7,
            "from":"07:00",
            "to":"19:00"
         },
         {
            "day":"FE",
            "label":"Feiertag",
            "order":8,
            "from":"07:00",
            "to":"19:00"
         }
      ],
      "offerInformation":{
         "service":false,
         "selfService":true,
         "unattended":false
      },
      "paymentMethods":{
         "cash":true,
         "debitCard":true,
         "creditCard":true,
         "others":"Gutmann-Card"
      },
      "paymentArrangements":{
         "cooperative":false,
         "clubCard":false
      },
      "otherServiceOffers":"CARWASH",
      "position":1,
      "open":true,
      "prices":[
         {
            "fuelType":"SUP",
            "amount":1.717,
            "label":"Super 95"
         }
      ]
   },

I would now like to receive the current price for this gas station, no matter how it is ranked. I’ve tried adding the ID directly to the URL, but to no avail.

It would be great if someone could help me with this!

Hi,

the API doesn’t allow this directly (see e-control FAQ).

But you can implement a template sensor that loads the value from the cheapest sensors.
Below you find a simple script to do that.

{% set station_id = 1471127 %}
{% if state_attr('sensor.fuel_price_bruck_die_1', 'id') == station_id %}
{{ states('sensor.fuel_price_bruck_die_1') }}        
{% elif state_attr('sensor.fuel_price_bruck_die_2', 'id') == station_id %}
{{ states('sensor.fuel_price_bruck_die_2') }}        
{% elif state_attr('sensor.fuel_price_bruck_die_3', 'id') == station_id %}
{{ states('sensor.fuel_price_bruck_die_3') }}     
{% elif state_attr('sensor.fuel_price_bruck_die_4', 'id') == station_id %}
{{ states('sensor.fuel_price_bruck_die_4') }}  
{% elif state_attr('sensor.fuel_price_bruck_die_5', 'id') == station_id %}
{{ states('sensor.fuel_price_bruck_die_5') }}  
{% else %}
unavailable
{% endif %}
1 Like

Thanks @matrixx567, that was exactly what i needed!

Thank you @FPro for the idea and @matrixx567 for the card design, that help me how to do this for my country and it’s API.

https://community.home-assistant.io/t/cheapest-5-gas-stations-in-your-zone-city-spain/

Hi,

you could also just filter the json data - no need for a script.
Here is my configuration for a sensor with specified id (value_template info came from @petro @Craig_McGowan in this thread):

      - name: Diesel F.Leitner Leoben
        unit_of_measurement: €
        device_class: monetary
        value_template: "{{(value_json | selectattr('id','eq',1469006) | list | first).prices[0].amount}}"
        json_attributes_path: "$.[?(@.id== 1469006)].location"
        json_attributes:
          - latitude
          - longitude

You would only need to adapt the id to in value_template and json_attributes_path to make it work for you. I extracted lat/long as attributes, so that ha shows the fuel stations on the map.

br

2 Likes