How to add a JSON configuration to a things file?

  • Platform information:
    • Hardware: Raspberry Pi
    • OS: RaspiOS
    • openHAB version: 3.2.0

I tested the webhook binding.
In the GUI the configuration worked quite straight forward:
Settings - Things - JSON configuration - Edit script
and pasting the following configuration:

{
  "channels": [
    {
     "key": "tempinf",
     "kind": "STATE",
     "state": "req.parameters.tempinf[0]"
    },
     {
     "key": "humidityin",
     "kind": "STATE",
     "state": "req.parameters.humidityin[0]"
    }
  ]
}

Now I would like to use a .things file instead of using the GUI. Unfortunately I could not find any information on how to add a configuration there.

gw1100.things:

Thing httplistener:HttpListener:ecowittGateway {
 Channels:
  State String : tempinf "Temperature" [
   configParameter="req.parameters.tempinf[0]"
  ]
}

The code above does not work. How is it possible to add the configuration, please?
In the GUI the status shows “UNINITIALIZED, HANDLER_CONFIGURATION_PENDING”.

This thread seems to deal with a related topic, but it did not help me.

IMHO for this new binding nobody really tried to find out how the needed syntax for the (sort of deprecated) .things file would be.
Authors of older bindings did put the syntax for their .things file into the docs because it was needed back then, however that changed with OH3.
Making up the syntax from the UI code won’t work for sure.

I’d just use the UI.

1 Like

It might be able to be manually generated from the UI-created YAML code, but you’d need to show the code for the whole Thing, not just your Channels, for best chances of success

OK, so this is specific to the binding, I thought it’s a generic OH topic…
In general the UI is convenient to use, but personally I prefer the configuration files for setting up new/migrating OH servers (e.g. after OS upgrade).

In the GUI I simply add a HttpListener thing, add the JSON configuration as shown above, and everything works.
I am now wondering whether I can achieve the same result with a .things file instead of using the GUI.
Which information do you miss?

The complete YAML code from the Code tab of your Thing.

I see, thank you for the hint!
Here it is:

UID: httplistener:HttpListener:700145ac7c
label: HttpListener thing
thingTypeUID: httplistener:HttpListener
configuration:
  jsonConfig: |-
    {
    	"channels": [
    		{
    			"key": "tempinf",
    			"kind": "STATE",
    			"state": "req.parameters.tempinf[0]"
    		},
    		{
    			"key": "humidityin",
    			"kind": "STATE",
    			"state": "req.parameters.humidityin[0]"
    		}
    	]
    }

No this is not the case. It’s still recommended to add textual configuration of things in the binding documentation. Although not everyone is on the same page here.

Yes that should be possible. This version of the binding uses a custom json format, not related to any openHAB syntax, to dynamically add channels. Therefor it’s not possible to use the UI supported way or the textual way of adding channels. The initial configuration you guessed would have been the way if it was implemented using the openHAB way. My guess is a configuration would look something like this:

Thing httplistener:HttpListener:ecowittGateway [jsonConfig="<your json goes here, \" should be escaped, and newlines removed">]

Note: Looking at the GitHub repository of the binding It does look like the binding developer rewrote the httplistener binding (renamed to webhook) which uses a different way of configuring the channels (at first sight it looks like the openHAB way of dynamically creating channels). But that version doesn’t seem to be released.

Great, thank you - that works, the “thing” goes ONLINE now!

Thing httplistener:HttpListener:ecowittGateway [jsonConfig="{ \"channels\": [ { \"key\": \"tempinf\", \"kind\": \"STATE\", \"state\": \"req.parameters.tempinf[0]\" }, { \"key\": \"humidityin\", \"kind\": \"STATE\", \"state\": \"req.parameters.humidityin[0]\" } ] }"]

Unfortunately it still does not work, but this seems to be a binding specific topic (will follow up there).
But the question of how to configure a thing in a file seems to be resolved.

As a first step, you might inspect your Thing settings in the GUI (even though you configured via file) and see if they have parsed the way that you expected.

That’s properly shown, similar to the GUI (excluding the line breaks):
Configuration:

{ "channels": [ { "key": "tempinf", "kind": "STATE", "state": "req.parameters.tempinf[0]" }, { "key": "humidityin", "kind": "STATE", "state": "req.parameters.humidityin[0]" } ] }

Required JSON configuration for listener thing. Refer to documentation about the format.

But it does not behave similar to the GUI thing.