JSON Transformation Help needed

Hi Andreas,

ich will create a new topic with all the files and rules and …

Thomas

In order to get prices from multiple gas-stations in one JSON-string you should use the (new) prices.php script as posted on the website. Works great.
…and, why don’t you use JSON Transformation Service in order to directly get the needed values? No extra file needed!

Look at my other (solved) questions, I’m rght at that topic.

@opus I use the new prices script and yes it works great. Could you give a example how to use the JSON Transformation Services.

Could you provide a link to your solutions?

Thomas

Look at that thread, last posting:

I’m at work posting from my mobile, sorry for the "format"of the link.

No problem, i’m at work too.

OK i understand the approach. This could reduce the nnumber of item i use. Is it right that the json at the of the statement holds the complete json string. How do you fill this?

Thomas

The part for getting the json is in this thread

If you wait until later today I’ll post my rule which gets all prices of three fixed stations every 5 minutes.

That moves the solution forward to more elegance :slight_smile:

Thanks.

I will come back with my complete solution after rebuiding it

Thomas

Although my solution is running now, my head is working on reducing the number of requests.
I do read the details of the station once a day, that way I have the opening times. However I do check every 5 minutes for a price update, even if the station is closed. So there is room for improvement!

Currently the opening times are not of interest, because usually stations are open while i am awake :slight_smile:

What is your concern about the number of requests? You could pause the check during the night when ou are asleep. Eg between 11:00 and 4:00 in the morning what ever suites you best. Or you can go down to check every 10 minutes. I think that depaends on you behaviour. Do you check price and based on the price you go to the station or is it more like “I’m on the way lets see if gas is cheap and if so let refuel”

thomas

Ah, sorry, didn’t tell!
My main approach is to chart the prices for day/week/4week period using a RoundRobinDB. Works well!

OK and why just for fun?

Just for the users on my openhab server.

This my rule:

import org.openhab.core.library.types.*
var String Tankstelle1_ID=""
var String Tankstelle2_ID=""
var String Tankstelle3_ID=""
var String API_Key=""
var String Status
var Number E10
var Number E5
var Number Diesel



rule "Benzinpreise"
when 
    Time cron "0 0/5 * * * ?"
then
    var String URL= "https://creativecommons.tankerkoenig.de/json/prices.php?ids=" + Tankstelle1_ID + "\," +Tankstelle2_ID + "\," + Tankstelle3_ID + "&apikey=" + API_Key
    //logInfo ("Benzinpreise", "URL = {}", URL)
    var String json = sendHttpGetRequest(URL)
    //logInfo("Benzinpreise","JSON-String = {}", json)
    var String OK = transform("JSONPATH", "$.ok", json)
    //logInfo("Benzinpreise", "OK = {}", OK)
    if (OK=="true") {
        Status = transform("JSONPATH", "$.prices." + Tankstelle1_ID + ".status", json)
        //logInfo("Benzinpreise", "Status = {}", Status)
        if (Status=="open") {
            E10 = transform("JSONPATH",  "$.prices." + Tankstelle1_ID + ".e10", json)
            E5  = transform("JSONPATH",  "$.prices." + Tankstelle1_ID + ".e5", json)
            Diesel  = transform("JSONPATH",  "$.prices." + Tankstelle1_ID + ".diesel", json)
            E10_1.postUpdate(E10.toString)
            E5_1.postUpdate(E5.toString)
            Diesel_1.postUpdate(Diesel.toString)
            //logInfo("Benzinpreise", "E10 = {}", E10)
            //logInfo("Benzinpreise", "E5 = {}", E5)
            //logInfo("Benzinpreise", "Diesel = {}", Diesel)
        }
        else {
            logInfo("Benzinpreis", "Status = {}", Status)
        }
        Status = transform("JSONPATH", "$.prices." + Tankstelle2_ID + ".status", json)
        //logInfo("Benzinpreise", "Status = {}", Status)
        if (Status=="open") {
            E10 = transform("JSONPATH", "$.prices." + Tankstelle2_ID + ".e10", json)
            E5  = transform("JSONPATH", "$.prices." + Tankstelle2_ID + ".e5", json)
            Diesel  = transform("JSONPATH", "$.prices." + Tankstelle2_ID + ".diesel", json)
            E10_2.postUpdate(E10.toString)
            E5_2.postUpdate(E5.toString)
            Diesel_2.postUpdate(Diesel.toString)
            //logInfo("Benzinpreise", "E10 = {}", E10)
            //logInfo("Benzinpreise", "E5 = {}", E5)
            //logInfo("Benzinpreise", "Diesel = {}", Diesel)
          }
          else {
            logInfo("Benzinpreis", "Status = {}", Status)
        }
        Status = transform("JSONPATH", "$.prices." + Tankstelle3_ID + ".status", json)
        //logInfo("Benzinpreise", "Status = {}", Status)
        if(Status=="open") {
            E10 = transform("JSONPATH",  "$.prices." + Tankstelle3_ID + ".e10", json)
            E5  = transform("JSONPATH",  "$.prices." + Tankstelle3_ID + ".e5", json)
            Diesel  = transform("JSONPATH",  "$.prices." + Tankstelle3_ID + ".diesel", json)
            E10_3.postUpdate(E10.toString)
            E5_3.postUpdate(E5.toString)
            Diesel_3.postUpdate(Diesel.toString)
            //logInfo("Benzinpreise", "E10 = {}", E10)
            //logInfo("Benzinpreise", "E5 = {}", E5)
            //logInfo("Benzinpreise", "Diesel = {}", Diesel)
        }
        else {
            logInfo("Benzinpreis", "Status = {}", Status)
        }
    }
    else {
        var String HttpError = transform("JSONPATH",  "$.message", json)
        logInfo("Benzinpreis", "HttpError = {}", HttpError)
    }
end

I’m commented out all loggins if the request runs normal.
E10_1, E10_2, E10_3, … are items on the sitemap, they are persitet in a rrd4j database.
You need to set the strings for API_Key and the 3 gas-station IDs

Do you operate it on OH 1.8.3? With OH2 i get the error that the transform returns a String not a Number

Thomas

I’m on OH2
Which variable are you talking about?

The fuel variable could you please post if you are using OH1 or 2 and your imports.

Thanks
Thomas

As stated above I’m using OH2. The only relevant import is JSON Transformation Service(IMHO).

I do pass the number I got from the json ( either E10, E5 or Diesel ) to the relevant (number)item as a string. Why? I don’t remember, but it works that way. It gets displayed on the sitemap and it gets persisted correctly in the Database.

Currently i have the problem that the transform statement throws an exception and i dont know why.

Thomas

Could you please share an example of the modified tankerkoenig rule?
@Dibbler42 or @opus

The detail.php does not work at the moment.