Gas price via Tankerkönig (Germany)

Tankerkönig provides a nice API for getting the current price of gas stations.
https://creativecommons.tankerkoenig.de/#usage

In my case I want to monitor the diesel price of my local gas station.

How to set up:

  1. Request an API key @ Tankerkönig API
    The APi key should look like “00000000-0000-0000-0000-000000000002”
  2. Find your gas station you want to monitor at: Tankerkönig Konfigurator
    e.g. “94e70fc4-b22f-4e5a-877f-bc1082cdae81”
  3. Add a new sensor and replace the API key and station id
sensor:
  - platform: rest
    resource: https://creativecommons.tankerkoenig.de/json/prices.php?apikey=00000000-0000-0000-0000-000000000002&ids=94e70fc4-b22f-4e5a-877f-bc1082cdae81
    name: Diesel
    unit_of_measurement: '€'
    value_template: '{{ value_json.prices["94e70fc4-b22f-4e5a-877f-bc1082cdae81"].diesel }}'
	scan_interval: 900

make it pretty:

customize:
sensor.diesel:
icon: mdi:gas-station

if you need E10 or E5, just use
value_json.prices[“94e70fc4-b22f-4e5a-877f-bc1082cdae81”].e5 or value_json.prices[“94e70fc4-b22f-4e5a-877f-bc1082cdae81”].e10

The sensor now will update ever 15min. Please keep in mind, that based on the terms and conditions only updates every 10min are allowed:
> Home-Automation-Systeme, Smart-Mirrors und ähnliches
> ein Aufruf alle 10 Minuten maximal, oder - besser - bei Bedarf

14 Likes

Nice, would be cool if places around me offered that

edit: looked into it a bit, seems you could probably use gasbuddy.com for us US people with a scrape sensor or something

i’ve done the same with french price :slight_smile:

I’ve 2 stations near my home and i can choose the lowest now.

Thank you for the tip!
Slightly off topic since I am not using the exact API in question…
I am having some issues pulling data from an API (I created a thread but didnt get any response).
Since you seem to have this down Id thought Id ask here.

I have the API +key that gives me the time of departure of next subway from my closest station.

The API link gives me the following from which I want to pull the “DisplayTime” and “Destination”.
Do you have any idea what I should put under value_template: ?
Cant get it to work =/

{"StatusCode":0,"Message":null,"ExecutionTime":109,"ResponseData":{"LatestUpdate":"2017-03-18T11:18:27","DataAge":18,"Metros":[{"GroupOfLine":"tunnelbanans blå linje","DisplayTime":"3 min","TransportMode":"METRO","LineNumber":"11","Destination":"Akalla","JourneyDirection":1,"StopAreaName":"Näckrosen","StopAreaNumber":3221,"StopPointNumber":3221,"StopPointDesignation":"1","TimeTabledDateTime":"2017-03-18T11:21:45","ExpectedDateTime":"2017-03-18T11:21:47","JourneyNumber":30302,"Deviations":null},{"GroupOfLine":"tunnelbanans blå linje","DisplayTime":"7 min","TransportMode":"METRO","LineNumber":"11","Destination":"Kungsträdgården","JourneyDirection":2,"StopAreaName":"Näckrosen","StopAreaNumber":3221,"StopPointNumber":3222,"StopPointDesignation":"2","TimeTabledDateTime":"2017-03-18T11:25:00","ExpectedDateTime":"2017-03-18T11:26:04","JourneyNumber":30029,"Deviations":null},{"GroupOfLine":"tunnelbanans blå linje","DisplayTime":"13 min","TransportMode":"METRO","LineNumber":"11","Destination":"Akalla","JourneyDirection":1,"StopAreaName":"Näckrosen","StopAreaNumber":3221,"StopPointNumber":3221,"StopPointDesignation":"1","TimeTabledDateTime":"2017-03-18T11:31:45","ExpectedDateTime":"2017-03-18T11:31:45","JourneyNumber":30304,"Deviations":null},{"GroupOfLine":"tunnelbanans blå linje","DisplayTime":"11:35","TransportMode":"METRO","LineNumber":"11","Destination":"Kungsträdgården","JourneyDirection":2,"StopAreaName":"Näckrosen","StopAreaNumber":3221,"StopPointNumber":3222,"StopPointDesignation":"2","TimeTabledDateTime":"2017-03-18T11:35:00","ExpectedDateTime":"2017-03-18T11:35:00","JourneyNumber":30030,"Deviations":null},{"GroupOfLine":"tunnelbanans blå linje","DisplayTime":"11:41","TransportMode":"METRO","LineNumber":"11","Destination":"Akalla","JourneyDirection":1,"StopAreaName":"Näckrosen","StopAreaNumber":3221,"StopPointNumber":3221,"StopPointDesignation":"1","TimeTabledDateTime":"2017-03-18T11:41:45","ExpectedDateTime":"2017-03-18T11:41:45","JourneyNumber":30306,"Deviations":null},{"GroupOfLine":"tunnelbanans blå linje","DisplayTime":"11:45","TransportMode":"METRO","LineNumber":"11","Destination":"Kungsträdgården","JourneyDirection":2,"StopAreaName":"Näckrosen","StopAreaNumber":3221,"StopPointNumber":3222,"StopPointDesignation":"2","TimeTabledDateTime":"2017-03-18T11:45:00","ExpectedDateTime":"2017-03-18T11:45:00","JourneyNumber":30031,"Deviations":null}],"Buses":[{"GroupOfLine":null,"TransportMode":"BUS","LineNumber":"515","Destination":"Sundbybergs station","JourneyDirection":1,"StopAreaName":"Näckrosen","StopAreaNumber":50407,"StopPointNumber":50407,"StopPointDesignation":null,"TimeTabledDateTime":"2017-03-18T11:18:00","ExpectedDateTime":"2017-03-18T11:18:00","DisplayTime":"11:18","JourneyNumber":20337,"Deviations":null},{"GroupOfLine":null,"TransportMode":"BUS","LineNumber":"515","Destination":"Odenplan","JourneyDirection":2,"StopAreaName":"Näckrosen","StopAreaNumber":50407,"StopPointNumber":50408,"StopPointDesignation":null,"TimeTabledDateTime":"2017-03-18T11:24:00","ExpectedDateTime":"2017-03-18T11:24:00","DisplayTime":"11:24","JourneyNumber":20367,"Deviations":null},{"GroupOfLine":null,"TransportMode":"BUS","LineNumber":"515","Destination":"Sundbybergs station","JourneyDirection":1,"StopAreaName":"Näckrosen","StopAreaNumber":50407,"StopPointNumber":50407,"StopPointDesignation":null,"TimeTabledDateTime":"2017-03-18T11:33:00","ExpectedDateTime":"2017-03-18T11:33:00","DisplayTime":"11:33","JourneyNumber":20364,"Deviations":null},{"GroupOfLine":null,"TransportMode":"BUS","LineNumber":"515","Destination":"Odenplan","JourneyDirection":2,"StopAreaName":"Näckrosen","StopAreaNumber":50407,"StopPointNumber":50408,"StopPointDesignation":null,"TimeTabledDateTime":"2017-03-18T11:39:00","ExpectedDateTime":"2017-03-18T11:39:00","DisplayTime":"11:39","JourneyNumber":20388,"Deviations":null},{"GroupOfLine":null,"TransportMode":"BUS","LineNumber":"515","Destination":"Sundbybergs station","JourneyDirection":1,"StopAreaName":"Näckrosen","StopAreaNumber":50407,"StopPointNumber":50407,"StopPointDesignation":null,"TimeTabledDateTime":"2017-03-18T11:48:00","ExpectedDateTime":"2017-03-18T11:48:00","DisplayTime":"11:48","JourneyNumber":20385,"Deviations":null}],"Trains":[],"Trams":[],"Ships":[],"StopPointDeviations":[{"StopInfo":{"StopAreaNumber":3221,"StopAreaName":"Näckrosen","TransportMode":"METRO","GroupOfLine":"tunnelbanans blå linje"},"Deviation":{"Text":"Korta tåg. Gå mot mitten av plattformen. *** Short trains. Continue to the middle of the platform. ","Consequence":null,"ImportanceLevel":2}}]}}

Dear Kimpan,

something like:

Next: {{value_json.ResponseData.Metros[0].Destination}} in {{value_json.ResponseData.Metros[0].DisplayTime}}
Would result in:
Next: Akalla in 3 min

To play around with the templating you can simply go to the http://yourip:8123/dev-template
Then type:

{% set value_json = [paste your JSON here] %}
Next: {{value_json.ResponseData.Metros[0].Destination}} in {{value_json.ResponseData.Metros[0].DisplayTime}}
1 Like

thanks
Not sure I follow completely. I put this into dev-template (switched the key):

{% set value_json = [http://api.sl.se/api2/realtimedeparturesV4.json?key=0896e051015e4c3g9e0ef05531cb4d3f&siteid=9304&timewindow=20] %}
Next: {{value_json.ResponseData.Metros[0].Destination}} in {{value_json.ResponseData.Metros[0].DisplayTime}}

and get this response:
Error rendering template: TemplateSyntaxError: expected token ‘,’, got ‘:’

please replace the

[http://api.sl.se/api2/realtimedeparturesV4.json?key=0896e051015e4c3g9e0ef05531cb4d3f&siteid=9304&timewindow=20]

with your JSON

{“StatusCode”:0,“Message”:null,“ExecutionTime”:109,“ResponseData”:{“LatestUpdate”:“2017-03-18T11:18:27”,“DataAge”:18,“Metros”:[{“GroupOfLine”:“tunnelbanans blå linje”,“DisplayTime”:“3 min”,“TransportMode”:“METRO”,“LineNumber”:“11”,“Destination”:“Akalla”,“JourneyDirection”:1,“StopAreaName”:“Näckrosen”,“StopAreaNumber”:3221,“StopPointNumber”:3221,“StopPointDesignation”:“1”,“TimeTabledDateTime”:“2017-03-18T11:21:45”,“ExpectedDateTime”:“2017-03-18T11:21:47”,“JourneyNumber”:30302,“Deviations”:null},{“GroupOfLine”:“tunnelbanans blå linje”,“DisplayTime”:“7 min”,“TransportMode”:“METRO”,“LineNumber”:“11”,“Destination”:“Kungsträdgården”,“JourneyDirection”:2,“StopAreaName”:“Näckrosen”,“StopAreaNumber”:3221,“StopPointNumber”:3222,“StopPointDesignation”:“2”,“TimeTabledDateTime”:“2017-03-18T11:25:00”,“ExpectedDateTime”:“2017-03-18T11:26:04”,“JourneyNumber”:30029,“Deviations”:null},{“GroupOfLine”:“tunnelbanans blå linje”,“DisplayTime”:“13 min”,“TransportMode”:“METRO”,“LineNumber”:“11”,“Destination”:“Akalla”,“JourneyDirection”:1,“StopAreaName”:“Näckrosen”,“StopAreaNumber”:3221,“StopPointNumber”:3221,“StopPointDesignation”:“1”,“TimeTabledDateTime”:“2017-03-18T11:31:45”,“ExpectedDateTime”:“2017-03-18T11:31:45”,“JourneyNumber”:30304,“Deviations”:null},{“GroupOfLine”:“tunnelbanans blå linje”,“DisplayTime”:“11:35”,“TransportMode”:“METRO”,“LineNumber”:“11”,“Destination”:“Kungsträdgården”,“JourneyDirection”:2,“StopAreaName”:“Näckrosen”,“StopAreaNumber”:3221,“StopPointNumber”:3222,“StopPointDesignation”:“2”,“TimeTabledDateTime”:“2017-03-18T11:35:00”,“ExpectedDateTime”:“2017-03-18T11:35:00”,“JourneyNumber”:30030,“Deviations”:null},{“GroupOfLine”:“tunnelbanans blå linje”,“DisplayTime”:“11:41”,“TransportMode”:“METRO”,“LineNumber”:“11”,“Destination”:“Akalla”,“JourneyDirection”:1,“StopAreaName”:“Näckrosen”,“StopAreaNumber”:3221,“StopPointNumber”:3221,“StopPointDesignation”:“1”,“TimeTabledDateTime”:“2017-03-18T11:41:45”,“ExpectedDateTime”:“2017-03-18T11:41:45”,“JourneyNumber”:30306,“Deviations”:null},{“GroupOfLine”:“tunnelbanans blå linje”,“DisplayTime”:“11:45”,“TransportMode”:“METRO”,“LineNumber”:“11”,“Destination”:“Kungsträdgården”,“JourneyDirection”:2,“StopAreaName”:“Näckrosen”,“StopAreaNumber”:3221,“StopPointNumber”:3222,“StopPointDesignation”:“2”,“TimeTabledDateTime”:“2017-03-18T11:45:00”,“ExpectedDateTime”:“2017-03-18T11:45:00”,“JourneyNumber”:30031,“Deviations”:null}],“Buses”:[{“GroupOfLine”:null,“TransportMode”:“BUS”,“LineNumber”:“515”,“Destination”:“Sundbybergs station”,“JourneyDirection”:1,“StopAreaName”:“Näckrosen”,“StopAreaNumber”:50407,“StopPointNumber”:50407,“StopPointDesignation”:null,“TimeTabledDateTime”:“2017-03-18T11:18:00”,“ExpectedDateTime”:“2017-03-18T11:18:00”,“DisplayTime”:“11:18”,“JourneyNumber”:20337,“Deviations”:null},{“GroupOfLine”:null,“TransportMode”:“BUS”,“LineNumber”:“515”,“Destination”:“Odenplan”,“JourneyDirection”:2,“StopAreaName”:“Näckrosen”,“StopAreaNumber”:50407,“StopPointNumber”:50408,“StopPointDesignation”:null,“TimeTabledDateTime”:“2017-03-18T11:24:00”,“ExpectedDateTime”:“2017-03-18T11:24:00”,“DisplayTime”:“11:24”,“JourneyNumber”:20367,“Deviations”:null},{“GroupOfLine”:null,“TransportMode”:“BUS”,“LineNumber”:“515”,“Destination”:“Sundbybergs station”,“JourneyDirection”:1,“StopAreaName”:“Näckrosen”,“StopAreaNumber”:50407,“StopPointNumber”:50407,“StopPointDesignation”:null,“TimeTabledDateTime”:“2017-03-18T11:33:00”,“ExpectedDateTime”:“2017-03-18T11:33:00”,“DisplayTime”:“11:33”,“JourneyNumber”:20364,“Deviations”:null},{“GroupOfLine”:null,“TransportMode”:“BUS”,“LineNumber”:“515”,“Destination”:“Odenplan”,“JourneyDirection”:2,“StopAreaName”:“Näckrosen”,“StopAreaNumber”:50407,“StopPointNumber”:50408,“StopPointDesignation”:null,“TimeTabledDateTime”:“2017-03-18T11:39:00”,“ExpectedDateTime”:“2017-03-18T11:39:00”,“DisplayTime”:“11:39”,“JourneyNumber”:20388,“Deviations”:null},{“GroupOfLine”:null,“TransportMode”:“BUS”,“LineNumber”:“515”,“Destination”:“Sundbybergs station”,“JourneyDirection”:1,“StopAreaName”:“Näckrosen”,“StopAreaNumber”:50407,“StopPointNumber”:50407,“StopPointDesignation”:null,“TimeTabledDateTime”:“2017-03-18T11:48:00”,“ExpectedDateTime”:“2017-03-18T11:48:00”,“DisplayTime”:“11:48”,“JourneyNumber”:20385,“Deviations”:null}],“Trains”:,“Trams”:,“Ships”:,“StopPointDeviations”:[{“StopInfo”:{“StopAreaNumber”:3221,“StopAreaName”:“Näckrosen”,“TransportMode”:“METRO”,“GroupOfLine”:“tunnelbanans blå linje”},“Deviation”:{“Text”:"Korta tåg. Gå mot mitten av plattformen. *** Short trains. Continue to the middle of the platform. ",“Consequence”:null,“ImportanceLevel”:2}}]}}

aaa! =) that worked!

So I would put “Next: {{value_json.ResponseData.Metros[0].Destination}} in {{value_json.ResponseData.Metros[0].DisplayTime}}” in value_template:?

Just give it a try.

as an alternative you could also try something like

{{ "Next: " + value_json.ResponseData.Metros[0].Destination + " in " + value_json.ResponseData.Metros[0].DisplayTime}}

Not sure if this works.
Sorry I don’t have any HASS instance on hand currently.

Worked great, thanks for your help!
It is displayed as seen below:

Would it be possible to only get the times towards “Kungsträdgården”? Would be easier to read on the frontend…
basically only get “{{value_json.ResponseData.Metros[0].DisplayTime}}” if {{value_json.ResponseData.Metros[0].Destination}} = Kungsträdgården,

I think it would make more sense to clarify this offline.
I’ll write you an message.

1 Like

Hello togehter,

i’m new in Home Assistant. The first post from leinich works great for me. Now i want to and some more sensors values.

It looks like:

- platform: rest
  resource: https://creativecommons.tankerkoenig.de/json/prices.php?apikey=xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx&ids=xxxxxxxxxxxid1xxxxxxxxxxxxxx,xxxxxxxxxxxid2xxxxxxxxxxxxxx,xxxxxxxxxxxid3xxxxxxxxxxxxxx
  name: JET_diesel
  unit_of_measurement: '€'
  value_template: '{{ value_json.prices["xxxxxxxxxxxid1xxxxxxxxxxxxxx“].diesel }}'
  scan_interval: 915

Next step ist to get the value for e5. Must i write again everything with e5 ? When i do that, i generate more than one request in this 10 minutes or not? But i also can’t work without the “resource”.

I also tried something like:

value_json.prices["xxxxxxxxxxxid1xxxxxxxxxxxxxx“].diesel and value_json.prices["xxxxxxxxxxxid1xxxxxxxxxxxxxx“].e5

But it doesn’t work for me. I only get the value for one sensor with one value.

I hope you can help me to learn more about this. Thank you in advance.

Dear PrivatHomeServer,

yes your assumption is correct.
Each sensor can take one value. If you want to track different values, you need to multiply the rest calls wich you need to design a way you are not exceed the max number of calls per hour/day,

I’m not aware of any possibility to achieve this besides writing a custom component.

You may try to use the sensor to display text like:

value_template: ‘Station1: {{ value_json.prices["xxxxxxxxxxxid1xxxxxxxxxxxxxx“].diesel }} / {{ value_json.prices["xxxxxxxxxxxid1xxxxxxxxxxxxxx“].e5 }} {{ “\n” }} Station2: value_json.prices["xxxxxxxxxxxid2xxxxxxxxxxxxxx“].diesel }} / {{ value_json.prices["xxxxxxxxxxxid2xxxxxxxxxxxxxx“].e5 }}’

Hello leinich,

thank you for the confirmation.
I tried it yesterday with the words “or” and “and” and also “/”. But without an effect. He only write in the “log” that he waits for an “integer”.

With this string I get all my values. Because I need for my wife the e5 and for me the diesel. And of course it will be nice to see different stations.

My problem is the view of these values with only one sensor as an “integer”. Is there any solution to safe the values into different sensors or variables or something like that? I don’t think so!

The other idea would be that I use “clever-tanken” with this function. But I think there is no experience from other people here to use this platform. And of course my experience with YAML and Home Assistant are low and starting at the moment.

Thank you for your support.

Greats Walter

nice one @leinich

but how to hide all sensible information in the secrets.yaml?

do i have to create for every resource: another entry in the secrets.yaml hence the &ids is always appended?

Thanks @void

As I’m not aware of any other possibility, I would add multiple entries in secrets.yaml.
Something like:

resource: !secret tankerkoenig_url_diesel_local
[...]
resource: !secret tankerkoenig_url_diesel_work
[...]
resource: !secret tankerkoenig_url_diesel_highway
[...]

yeah that is possible but i won’t spam my secrets… hiding the api key would be enough…

maybe i add the package to .gitignore for now and wait if someone points me in the right direction :wink:

Thanks, @leinich, great stuff.

Hi there, if somebody is interested in getting more than just 1 gas station per api query, just string together the station ids (comma separated) in the api url and use the code below. This also allows to query every 5 minutes (which is the minimum allowed time between queries) and saves some traffic :slight_smile:

  - platform: rest
    name: Fuel
    scan_interval: 300    
    resource: https://creativecommons.tankerkoenig.de/json/prices.php?apikey=yourapikey&ids=6550ce30-b15b-4087-9b7c-4ffb92702145,b09d3909-c74b-4c51-a51f-1e7dd76e1a4b,67720c01-2076-45a1-a695-3a0113bb97d9
    value_template: '{ value_json.prices.[] }'
    json_attributes:
    - prices
  - platform: template
    sensors:
      station1:
        friendly_name: 'Station 1'
        value_template: '{{ states.sensor.fuel.attributes["prices"]["6550ce30-b15b-4087-9b7c-4ffb92702145"]["e10"]}}'
        unit_of_measurement: '€'
      station2:
        friendly_name: 'Station 2'
        value_template: '{{ states.sensor.fuel.attributes["prices"]["b09d3909-c74b-4c51-a51f-1e7dd76e1a4b"]["e10"]}}'
        unit_of_measurement: '€'
      station3: 
        friendly_name: 'Station 3'
        value_template: '{{ states.sensor.fuel.attributes["prices"]["67720c01-2076-45a1-a695-3a0113bb97d9"]["e10"]}}'
        unit_of_measurement: '€'
5 Likes

I’ve created a state card for Tankerkönig. If you are interested look at https://www.panbachi.de/tankerkoenig-preise-in-homeassistant-darstellen-141/

There is a description how to create the state card.

tankerkoenig_homeassistant

5 Likes