Create JSON Item in OH2

items
json
jsonpath
Tags: #<Tag:0x00007fd312236ff8> #<Tag:0x00007fd312236e18> #<Tag:0x00007fd312236c88>

(paddy) #1

I’m using openhabian (Version 2.1) and I want to create an item with values of the local weather station. The URL is:
http://daten.buergernetz.bz.it/services/meteo/v1/sensors?station_code=65600MS&sensor_code=LT

There I get following code:

[
{
“SCODE”:“65600MS”,
“TYPE”:“LT”,
“DESC_D”:“Lufttemperatur”,
“DESC_I”:“Temperatura dell´aria”,
“DESC_L”:“Temperatura dl’aria”,
“UNIT”:“°C”,
“DATE”:“2017-08-15T07:50:00CEST”,
“VALUE”:12.9
}
]
I would like to create an item conaining the field “VALUE” (temperature). How do i create an item in OH2?

Thank you in advance!


(Mikael Gustafsson) #2

Hi,

You will need the http-binding and JSON transformation. You can read about both the binding and the transformation on their respective pages in the online documentation. There’s also an almost direct example of the item definition needed here (under JSONPath Examples).

Mikael


(paddy) #3

Thank you, I made a .items file and a sitemap with the item. Unfortunatley I don’t get any values, I will try further on.


(Scott Rushworth) #4

I do something similar using a rule for checking the Weather Underground API to get weather alerts and outside lux levels. It uses the JSONPATH transform to populate item values. Try something like this:

Item:

Number    Outside_Temperature    "Outside Temperature [%.1f °C]"    <temperature>    (gWeather)

Rule:

rule "Alert: Check for temperature"
when
    Time cron "0 1/5 * * * ?"
    //or
    //Item Virtual_Switch_1 changed
then
    logDebug("Rules", "Alert: Check for temperature: Starting")
    val String rawMessage = executeCommandLine("/bin/sh@@-c@@/usr/bin/curl -s -X GET http://daten.buergernetz.bz.it/services/meteo/v1/sensors?station_code=65600MS&sensor_code=LT",60000)
    if (rawMessage.length > 0) {
        val String transformedMessage = transform("JSONPATH", "$.VALUE", rawMessage.substring(1,rawMessage.length-1))
        if (transformedMessage != null) {
            logDebug("Rules", "Alert: Check for temperature: transformedMessage=[{}]",transformedMessage)
            Outside_Temperature.sendCommand(transformedMessage)
        }
    }
end

Sitemap:

Text item=Outside_Temperature

(paddy) #5

It seems to work! Thank you so much!


(paddy) #6

If I want to create some other items for example pressure, I only have to change the json-adress and the names of the items?


(Scott Rushworth) #7

Yes, you could. I’m not familiar with the weather service you are using, but they may have restrictions on the frequency of the API calls. You could just do one call to http://daten.buergernetz.bz.it/services/meteo/v1/sensors?station_code=65600MS, parse it, and update your items.

If this is your station, you could also add it to Weather Underground (your location might be a factor) and use the new ESH Weather Underground binding, the older OH Weather binding, or use the approach outlined in this thread:


(paddy) #8

Thanksgiving you, to geht other values Like humidity or pressure I only have to Change the sensorcode to LF ord LD.RED and i getti the values. So in the script I only have to Change the code and the Name of the item and rule?

Thanks


(Scott Rushworth) #9

As I said, you could do that. But rather than make several calls, you can make one and iterate over the results. Add Outside_Humidity and Outside_Pressure items and try something like this. Add more items and case statements to add other values.

rule "Alert: Check for weather"
when
    Time cron "0 1/5 * * * ?"
then
    var String rawMessage = executeCommandLine("/bin/sh@@-c@@/usr/bin/curl -s -X GET http://daten.buergernetz.bz.it/services/meteo/v1/sensors?station_code=65600MS",60000)
    if (rawMessage != null && rawMessage.contains("SCODE")) {
        rawMessage = rawMessage.substring(1,rawMessage.length - 1).replace("},\n","},,")
        val String[] rawMessageArray = rawMessage.split(",,")
        rawMessageArray.forEach[item|
            val String transformedMessage = transform("JSONPATH", "$.VALUE", item + "}")
            switch (transform("JSONPATH", "$.TYPE", item + "}")) {
                case "LT" : Outside_Temperature.sendCommand(transformedMessage)
                case "LF" : Outside_Humidity.sendCommand(transformedMessage)
                case "LD.RED" : Outside_Pressure.sendCommand(transformedMessage)
            }
        ]
    }
end

(paddy) #10

Fantastic, it works! Thank you so much.