JSONPATH Help

Hi Guys,

i want to display the current Bitcoin Price in my OH.
i found this JSON outputs:
https://api.coinmarketcap.com/v1/ticker/
or
https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR

How do i get them correctly in OH, i already managed to get this output:

[“19055.9”]

but i want to get the output as number/string without the brackets and quotation marks

this are my test items:

{ http="<[https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR.json:10000:JSONPATH($..price_usd)]" }

{ http="<[https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR:60000:JSONPATH($.0.price_usd)]" }

both give me:
[“19055.9”]

thanks!

EDIT:
nobody ? :frowning: - i am on 2.2 now - the first 4 items worked with 2.1 …

There are other threads that also discuss this. Seek those.

okay, thanks for the help…

( i wouldn’t open a thread, if i had found something related to my problem…)

[WARN ] [ab.binding.http.internal.HttpBinding] - Transformation 'JSONPATH($.['price_usd'])' threw an exception. [response=[

    {
        "id": "bitcoin", 
        "name": "Bitcoin", 
        "symbol": "BTC", 
        "rank": "1", 
        "price_usd": "18165.7", 
        "price_btc": "1.0", 
        "24h_volume_usd": "16375500000.0", 
        "market_cap_usd": "304304994262", 
        "available_supply": "16751625.0", 
        "total_supply": "16751625.0", 
        "max_supply": "21000000.0", 
        "percent_change_1h": "-0.72", 
        "percent_change_24h": "-4.1", 
        "percent_change_7d": "5.48", 
        "last_updated": "1513692554", 
        "price_eur": "15382.0789605", 
        "24h_volume_eur": "13866200257.5", 
        "market_cap_eur": "257674818467"
    }
]]

Please HELP - its frustrating… how do i get the god damn fuckin bitcoin value in my OH…

the manual is telling me NOTHING about it

What does your full item look like, and what about your sitemap?

String Bitcoin_BM_EUR3    "TEST2-Bitcoin Kurs EUR [%s]" <alarm>   (bitcoin)       { http="<[https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR:60000:JSONPATH($..price_usd)]" }

Sitemap

Frame label=""
            {                
                Group item=bitcoin label="Bitcoin TEST"
            }

Result:
17

Just to make sure, use Text instead of Group on your sitemap.

Text item=bitcoin

The brackets and quotes might be a result of using Group with an item like that. Notice the removal of the label as well.

Thanks rich, i was testing a lot of JSONPATH constellations (trial & error) in the group item but without success…

i did try your guess too, no luck - so i decided to go with different rules - this works as expected

just one little thing, i get a string out of my rule and want to have it as DecimalType for rounding - could you help here ?

i found your post in another thread about String:format, but dont get it to work correctly…

Thanks!

my guess was:

rule "BITCON EUR (coinmarketcap)"
    when 
        Time cron "0 * * * * ?"
    then       
        var String json = sendHttpGetRequest("https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR")
        var String value = transform("JSONPATH", "$.[0].price_eur", json)
        val value = String::format("%.1f EURO", (Bitcoin_EUR_JSON.state as DecimalType).floatValue
        logInfo("JSON RULE","*************: " + value + " EURO" )
        Bitcoin_EUR_JSON.postUpdate(value)      
end
[ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'BITCON EUR (coinmarketcap)': Could not cast 14704.9216524 to org.eclipse.smarthome.core.library.types.DecimalType; line 26, column 50, length 37

http://docs.openhab.org/configuration/rules-dsl.html#number-item

now its working:

FLOAT / String :

rule "BITCON EUR (coinmarketcap)"
    when 
        Time cron "0 * * * * ?"
    then       
        var String json = sendHttpGetRequest("https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR")
        var String value = transform("JSONPATH", "$.[0].price_eur", json)
        value = String::format("%.1f EUR", Float.parseFloat(value))
        logInfo("JSON RULE","*************: " + value + " EURO" )
        Bitcoin_EUR_JSON.postUpdate(value)      
end

EDIT:
i found a solution for a Number Item finally

rule "BITCON EUR (coinmarketcap) Test"
    when 
        Time cron "0 * * * * ?"
    then       
        var String json = sendHttpGetRequest("https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=EUR")
        var String value = transform("JSONPATH", "$.[0].price_eur", json)
        value = String::format("%.0f", Float.parseFloat(value))
        logInfo("JSON RULE","******BITCOIN-TEST*******: " + value )
        Bitcoin_EUR_JSON_Test.postUpdate(Float::parseFloat(String::format("%s",value)))  
end
2 Likes

Hi,

Thanks for this great info. It seems to work very nicely. I still have another problem/question. I would also like to add the value of other currencies. For many of them the value is lower then or around 1. Therefore it is important to also have the decimal values. Your rule works till “logInfo(“JSON RULE”,"BITCOIN-TEST*: " + value )” and the variable “value” will show the right value, but the transformation of it into a “Number” does not seem to work.

Any idea how I could solve this?

Big thank you !

Tijl

Hi Philipp,

Can you post what is in the transform file for JSONPATH to this thread?

Best, Jay

Sorry, you can ignore. I didn’t have the BINDING installed.

JSONPath Transformation
transformation-jsonpath - 2.3.0.SNAPSHOT

Best, Jay

1 Like