New OH2 binding: Tankerkoenig

Hi,

I spent the last days trying to implement an OH2 binding for getting the latest (german) gas prices by using the tankerkoenig api (https://www.tankerkoenig.de/).

I know there is already the possibilty to get that information by using the http-binding and a json-transformation (I saw a discussion already here: Sort/compare multiple number items (Gas/Petrol Prices)).
I thought a more comfortable way could adress more people in the openhab community and it would be a perfect first possibilty to contribute instead of just benefitting of this amazing smart home platform (“Geben ist seliger als nehmen” :sunglasses: ).

So what I have implemented:
The binding allows you to get the latest gas prices for a specific gas station.
To use the binding you need two things:

  1. A free api-key from tankerkoenig (https://creativecommons.tankerkoenig.de/).
  2. An existing id which identifies a gas station from tankerkoenig
  3. Optionally you can configure an refresh interval

Have a look here:

The pull request was created today.

I hope the binding can be a facilitation for a few users out there :slight_smile:

Ideas, doubts and discussions are always welcome!

10 Likes

Nice one! I will add this binding to my setup as soon as it got merged :wink: One thing I can already tell you that will stall the merge: your binding is missing the mandatory README.md. Check out this example to see what’s supposed to be in there: https://github.com/openhab/openhab2-addons/blob/master/addons/binding/org.openhab.binding.feed/README.md

Nice work on that topic.

I’m a frequent user of that api, some findings from my side:
As far as I understand your code, you are only using the details.php. Tankerkoenig.de request the user NOT TO update prices via the details.php. Users calling that script periodically (several times a day) migth get their API-Key deactivated!
I’d suggest to incooperate the prices.php script which can update the prices of a list of stations.

Thanks for watching the code @opus, your hint is right.

If I only configure on thing the minimal refresh interval will prevent that the request with the api are getting too high.
But if I have 3 or more “Tankestellen”-things with the same api-key they have all their own request which could lead to the deactivation of the api key by tankerkoenig itself.

At the moment I don’t know how to handle various configs over one request in openhab, but I think I’ll find an easy way.

Does anbody know if there’s the possibility to limit the amount of a “thing” in a binding? For now, I could allow just one “Tankestellen”-thing and later on, when the binding is optimized, you could use up to 10.

Should I cancel the pull request in order to save the maintainers time checking the “incomplete” binding (README.md is also missing)?

Thank you guys!

Just one idea:
If the number of Things ofr a binding can’t be limted, I’d try to create a dynamic number of channels. By allowing a list of station-ID, the binding could create set of channels for each station in the list.

I’d like to help coding, however my skills in the creation of java code are very limited!

I don’t think it’s needed to cancel the PR, now that it’s there already. A PR to be open and actively improved is totally normal. Just keep the process clear by commenting from time to time.

Regarding the api load problem: Synchronizing requests sounds like the right approach. You could check other bindings how something like that is done there. Otherwise you could contact developers of similar bindings. A less technical solution would be to simply warn users in the README to not overuse their API key, considering their things count and the configured interval. That would be an okay solution for a first version.

Keep going!!!

Hi all,

thanks to @opus, who supported me a lot with his experience with the tankerkoenig api, I changed a bit the binding handling in order to save some superfluous api requests.

Additionally I added the README.md with a little description how to set up the binding.

If someone is curious and wants to test the binding, you can download the binding here: https://www.dropbox.com/s/5xpcmizc7gz40oh/org.openhab.binding.tankerkoenig-2.0.0-SNAPSHOT.jar?dl=0
I’d recommend to set it up in a development environment, since there will be a few bugs for sure. One known issue I’m already working on is, that the binding seems to update values only after a restart :frowning:

Nevertheless, I would appreciate to get it tested :slight_smile:

Besides the need to restart before any data is fetched from tankerkonig.de I have observed no errors over the day of testing ( using 3 fuel-stations).
Thanks for this binding.

Did check how often the prices were fetched, I can’t see the 10 minutes I did set at all!
I do see steps between 1 and 10 minutes!
I’ll stop the binding for now.

I have made a “sidestep” and created a rule that does what I intend to do with the binding regarding opening times. That is working! Posted here
Now I’m trying to get the same working in Eclipse with the binding. I do have problems using either the feature the author used (make another polling job, use the *service to get data). But I’ll get there, but it will take some more time.

It seems to me that I’m at a dead end street in trying to adjust the binding using Eclipse.
Could anybody try to help? Or should I ask on github?

I used the code build by @dolic and added additional code which mimics the already used classes and methods “TankerkoenigListXXX” and added “TankerkoenigDetailXXX” (including all needed sub-types). The difference between both is that the later uses the “detail.php” to get detail data.
For using them I also created a “TankerkoenigDetailService” with the “GetTankerkoenigDetailResult” method.
When trying to call it inside a response job using this code:

    logger.info("UpdateTankstellenDetails jsonData : {}", jsonData);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(TankerkoenigDetailResult.class,
            new CustomTankerkoenigDetailResultDeserializer());
    Gson gson = gsonBuilder.create();
    logger.info("UpdateTankstellenDetails before creating res");
    TankerkoenigDetailResult res = gson.fromJson(jsonData, TankerkoenigDetailResult.class);
    logger.info("UpdateTankstellenDetails res : {}", res); 

I get correct log-entries for the jsonData (I verfied the reported string) and as a last entry “UpdateTankstellenDetails before creating res”. After that I get no more entries.

[Edit:]
The problem is my " CustomTankerkoenigDetailResultDeserializer", but I’m unable to solve that one.

Hello,

i dont get the binding running. The itemvalues just show NULL an i got this in my logs:

18:09:47.696 [ERROR] [ome.core.thing.internal.ThingManager] - Exception occurred while initializing handler of thing 'tankerkoenig:config:a174bea6': java.lang.NullPointerException
java.util.concurrent.ExecutionException: java.lang.NullPointerException
        at java.util.concurrent.FutureTask.report(FutureTask.java:122)[:1.8.0_131]
        at java.util.concurrent.FutureTask.get(FutureTask.java:206)[:1.8.0_131]
        at org.eclipse.smarthome.core.common.SafeMethodCaller.callAsynchronous(SafeMethodCaller.java:194)[99:org.eclipse.smarthome.core:0.9.0.201705120951]
        at org.eclipse.smarthome.core.common.SafeMethodCaller.call(SafeMethodCaller.java:83)[99:org.eclipse.smarthome.core:0.9.0.201705120951]
        at org.eclipse.smarthome.core.common.SafeMethodCaller.call(SafeMethodCaller.java:67)[99:org.eclipse.smarthome.core:0.9.0.201705120951]
        at org.eclipse.smarthome.core.thing.internal.ThingManager$9.run(ThingManager.java:710)[106:org.eclipse.smarthome.core.thing:0.9.0.201705120951]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)[:1.8.0_131]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_131]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)[:1.8.0_131]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)[:1.8.0_131]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[:1.8.0_131]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)[:1.8.0_131]
        at java.lang.Thread.run(Thread.java:748)[:1.8.0_131]
Caused by: java.lang.NullPointerException
        at org.openhab.binding.tankerkoenig.handler.BridgeHandler.initialize(BridgeHandler.java:82)[237:org.openhab.binding.tankerkoenig:2.1.0.201706012056]
        at org.eclipse.smarthome.core.thing.internal.ThingManager$9$1.call(ThingManager.java:713)[106:org.eclipse.smarthome.core.thing:0.9.0.201705120951]
        at org.eclipse.smarthome.core.thing.internal.ThingManager$9$1.call(ThingManager.java:1)[106:org.eclipse.smarthome.core.thing:0.9.0.201705120951]
        at org.eclipse.smarthome.core.common.SafeMethodCaller$CallableWrapper.call(SafeMethodCaller.java:181)[99:org.eclipse.smarthome.core:0.9.0.201705120951]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_131]
        ... 3 more

Could you give some details on your setup?
Where did you get the binding from? Since the pull request isn’t approved yet, the most actual.jar can be downloaded on the eclipse marketplace (last one from yesterday)
How did you setup your system? Did you create a bridge with your personal Api key?
Did you create any 'Tankstelle" with the respective ID?
Are you using openHAB2? This binding is made for that version!
Link to the Marketplace

hi,

i installed it with the paperui… there it appears as a marketplace-binding, i am using openhab2, snapshot of last week.

on the homepage i created a api-key, then i added the configuration as a thing with the api-key, then i added three tankstellen as things with their ids. of course i choose the configuration-thing as bridge (was the only value i could choose).
then i created a linked item for one of the tankstellen.

all the created things have the status “UNINITIALIZED - HANDLER_INITIALIZING_ERROR” in the paperui things overview

thanks for your help!

It looks like you are doing everything correct!
You are on a snapshot release which I’m not, however that should not explain that error.
Can you give me your exact binding version ( from the karaf console use bundle:list). That way I could pinpoint where the error is coming from, or if it might be resolved already (don’t think so)
I not at home, so actual help by changing code does have to wait until the end of the weekend, I’m sorry.

ok, here is the binding version. i will try to update to the latest snapshot, if this solves the problem, i give you an update here.

thanks!

237 | Active | 80 | 2.1.0.201706012056 | Tankerkoenig Binding

Thanks, although that was stupid question of me, since that is reported in the error you posted already.
You are using the version of last night, so the is nothing newer!

I’m not sure ,but thisLink the my Dropbox might give you on older version, that was reported working.
Will look into the one on the marketplace when I ’ m back.

thank you - i am going to try this out.

updating to the latest snapshot #935 didnt help