Retrieve JSON values

  • Platform information:
    • Hardware: RPI
    • OS: Docker
    • openHAB version:3.2.0
  • Issue of the topic: sensor not filled

All,

I have an API endpoint/sensor which is queried via HTTP, however temperature is used/found but the humidity is not:

$ curl http://192.168.1.9/ha/dht11.json -s | jq
{
  "meterkast": {
    "temperature": 20,
    "humidity": 48
  },
  "bedroom2": {
    "temperature": 20,
    "humidity": 48
  },
  "bedroom3": {
    "temperature": 20,
    "humidity": 48
  }
}

The ‘code’ in Things looks like:

UID: http:url:958b230f8b
label: temp-humidity
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: false
  headers:
    - ""
  baseURL: http://192.168.1.9/ha/dht11.json
  delay: 0
  stateMethod: GET
  refresh: 30
  commandMethod: GET
  contentType: application/json
  timeout: 3000
  bufferSize: 2048
channels:
  - id: temperature
    channelTypeUID: http:number
    label: MK Air temperature
    description: ""
    configuration:
      stateTransformation: JSONPATH:$.meterkast.temperature
  - id: mk_humidity
    channelTypeUID: http:number
    label: Meterkast humidity
    description: ""
    configuration:
      mode: READONLY
      stateTransformation: JSONPATH:$.meterkast.humidity



The logging on the ‘console’ does not show any errors anymore (it did show some errors initially).

What am I forgetting here?

Thanks for any feedback.

I would create a new String Channel for this Thing, with no transformation applied: can you confirm that openHAB receives the full JSON payload that you expect?

If so, remove the non-working Channel and create a new one through the UI.

Hi Thanks for the suggestion. Please remember I am a real OH newbie…

I remove the thing, and created a new one, not sure if this is what you would intent:

UID: http:url:958b230f8b
label: temp-humidity
thingTypeUID: http:url
configuration:
authMode: BASIC
ignoreSSLErrors: false
headers:
- “”
baseURL: http://192.168.1.9/ha/dht11.json
delay: 0
stateMethod: GET
refresh: 30
commandMethod: GET
contentType: application/json
timeout: 3000
bufferSize: 2048
channels:

  • id: meterkast_temperatuur
    channelTypeUID: http:string
    label: meterkast_temperatuur
    description: “”
    configuration: {}

How can I best confirm what OH receives? I see this in the GUI after linking the channel:

In the top it looks like the HTTP response is correct. Now I am not sure about how to ‘extract’ this into 3 different ‘sensors’ ?

I also receive these errors now in the console log:

21:17:23.779 [WARN ] [rofiles.JSonPathTransformationProfile] - Could not transform state ‘{“meterkast”: {“temperature”: 20.0, “humidity”: 45.0},“bedroom2”: {“temperature”: 20.0, “humidity”: 45.0},“bedroom3”: {“temperature”: 20.0, “humidity”: 45.0}}’ with function ‘JSONPATH:$.meterkast.temperature’ and format ‘%s’

Create New thing
Test each channel with a string by entering this in the code tab

channels:
  - id: meterkast_temperature
    channelTypeUID: http:string
    label: Meterkast Temperature
    description: ""
    configuration:
      stateTransformation: JSONPATH:$.meterkast.temperature
  - id: meterkast_humidity
    channelTypeUID: http:string
    label: Meterkast Humidity
    description: ""
    configuration:
      stateTransformation: JSONPATH:$.meterkast.humidity
  - id: bedroom2_temperature
    channelTypeUID: http:string
    label: Bedroom2 Temperature
    description: ""
    configuration:
      stateTransformation: JSONPATH:$.bedroom2.temperature
  - id: bedroom2_humidity
    channelTypeUID: http:string
    label: Bedroom2 Humidity
    description: ""
    configuration:
      stateTransformation: JSONPATH:$.bedroom2.humidity
  - id: meterkast_temperature
    channelTypeUID: http:string
    label: Bedroom2 Temperature
    description: ""
    configuration:
      stateTransformation: JSONPATH:$.bedroom3.temperature
  - id: bedroom3_humidity
    channelTypeUID: http:string
    label: Bedroom3 Humidity
    description: ""
    configuration:
      stateTransformation: JSONPATH:$.bedroom3.humidity

Did you enter transformation in an item profile.
no need to transform on channel and item it won’t work

You could also just add multiple items to the same channel with transformation profile

Edit:
You can’t change the channel type UID after it is created so delete thing and start again you can see this when you try and create an item and it is a different type to the channel. Is it a bug or a feature?

I think I figured it out with your help, my error was that I twice used the JSONPATH transformation.
I will check if this keeps without error, and mark as solution…

I have no errors, and the values get updated in the GUI.
However, the only thing I cannot find/understand is that there is no graph created anymore with historic values.

For simplicity I adjusted the API a little bit, so 1 URL returns only 1 location. In this case I retrieve only 2 values.

UID: http:url:aa0da43a8d
label: mk-temp-humidity
thingTypeUID: http:url
configuration:
authMode: BASIC
ignoreSSLErrors: false
baseURL: http://192.168.1.9/ha/dht11.json
delay: 0
stateMethod: GET
refresh: 30
commandMethod: GET
timeout: 3000
bufferSize: 2048
location: Meterkast
channels:

  • id: meterkast_humidity
    channelTypeUID: http:string
    label: meterkast_humidity
    description: null
    configuration:
    stateTransformation: JSONPATH:$.humidity
  • id: meterkast_temperature
    channelTypeUID: http:string
    label: meterkast_temperature
    description: null
    configuration:
    stateTransformation: JSONPATH:$.temperature

I am not sure if it is me, but sofar I find things quite cumbersome, and difficult to find recent howto’s (a lot is from previous versions)

That’ll be because your Items are strings. That was just a suggestion for diagnostic purposes, change back to Number types now.

Hi Guys,

Thanks for the feedback and help.
I remove the config, and applied it with http:number again, but even though the channel is online, the values keep in UNDEF state.

I am really disappointed how difficult and unreliable a simple implementation like this is openhab. In these times when we should strive to have ‘the masses’ getting onboard with these tools to prevent energy consumption, this is simply not going to work for the majority, and also not for me.

Did you try and just have one string channel with number items and the transformation on the item?

Once you have persisted and gotten it all to work can you please write a tutorial

James, no I did not try, and have no intention anymore. I removed my openhab.

The exact point why, is that for something this simple, I should be writing a new tutorial?
Exactly this should have some people scratch their head and wonder why something simple as an API rest call, and persisting the data, is that difficult in openhab to achieve.

Even though a lot of effort has been put in openhab, at this point in time, it simply cannot be used by people without thorough programming skills, lot’s of patience, and time on their hands for putting into their openhab instance. In my opinion that is a pity.

As you’ve discovered, retrieving and processing HTTP data encoded as JSON is not as simple as you think.

Programmers don’t write good tutorials for beginners. We can’t forget everything and start again.