German Fuel Prices

Thats my configuration to see the fuel prices for my often used petrol stations (Germany):

  • Create an API-Key on creativecommons.tankerkoenig.de
  • Get station ID’s from the finder: TankstellenFinder
  • Create an json sensor in configuration to retrieve prices from Tankerkoenig and a template sensor per station/attribute (eg. Diesel)
  • Some logos I stored in /config/www/fuel
sensor:

  - platform: rest
    name: Fuel
    resource: "https://creativecommons.tankerkoenig.de/json/prices.php?apikey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&ids=22301ffd-8110-47c6-a9d6-2bf873a10c72,c18cef16-99cc-4f8e-b959-e6ab6521d2be"
    scan_interval: 900
    verify_ssl: false
    value_template: '{ value_json.prices.[] }'
    json_attributes:
    - prices

  - platform: template
    sensors:
      bft_wolfhagen_diesel:
        friendly_name: Wolfhagen
        unit_of_measurement: €
        entity_picture_template: /local/fuel/bft.png
        value_template: '{{ states.sensor.fuel.attributes["prices"]["22301ffd-8110-47c6-a9d6-2bf873a10c72"]["diesel"]}}'
  • For Lovelace sorting I used the custom auto-entiies card:
type: custom:auto-entities
card:
  type: entities
  show_header_toggle: false
  state_color: false
  title: Diesel-Preise
filter:
  include:
    - entity_id: sensor.*diesel
  exclude: []
sort:
  method: state
  reverse: false
  numeric: true
show_empty: false

2 Likes

There is an integration for that:

And with that, this is the only code I have in my configuration:

tankerkoenig:
  # https://www.home-assistant.io/integrations/tankerkoenig/
  api_key: 99d55e5d-9999-1111-f543-d1c11a999cd9
  radius: 1
  fuel_types:
    - "diesel"
    - "e10"
  scan_interval: "0:15:01"
  stations:
    - awergfghi-9999-654a-a4a8-9c9bf99b9c9d

Sensors are then automatically there, no need for manual configuration.

2 Likes

Are you sure you want to post your API key in public?

I am sure that I don‘t (even though I just use a throw away mail/account for that and in the worst case it would get blocked if too many use it), and that is why you see quite a lot of the same numbers in there :wink:

To exclude the closed stations I used the filter exclude.

unknown = closed

type: custom:auto-entities
card:
  type: entities
  show_header_toggle: false
  state_color: false
  title: Super E10-Preise
filter:
  include:
    - entity_id: sensor.*_super_e10
  exclude:
    - state: unknown
sort:
  method: state
  reverse: false
  numeric: true
show_empty: false

Just found out that mehr-tanken.de has some API behind which can be easily queried. I created a bunch of rest sensors for my favorite gas stations to have up to date prices.
The serviceStationId can be copied from the url of the gas station, for some it might help to first review the payload via CURL/Postman/whatever to see which fuel type you need (in my case it’s always “Super Plus”)…

Example:

- scan_interval: 600
  resource: "https://page-api.auto-motor-und-sport.de/mt/poi-detail/"
  method: POST
  timeout: 10
  headers:
    Content-Type: "application/json"
  payload: '{ "serviceStationId": "96ea2b34", "updateDB": false, "includeServiceImages": false }'
  binary_sensor:
    - name: "ARAL Hockenheim geöffnet"
      unique_id: tankstelle_68766_aral_offen
      device_class: opening
      icon: mdi:store
      value_template: >-
        {{ value_json.station.open | bool }}
  sensor:
    - name: "ARAL Hockenheim Super Plus"
      unique_id: tankstelle_68766_aral_super_plus
      device_class: monetary
      unit_of_measurement: EUR
      icon: mdi:gas-station
      availability: >-
        {{ is_number(value_json.station.prices | selectattr('fuelId', 'match', '8') | map(attribute='price')|first) }}
      value_template: >-
        {% set super = value_json.station.prices | selectattr('fuelId', 'match', '8') | map(attribute='price')|first  %}
        {{ super }}