REST API with Python: Create Channels

Tags: #<Tag:0x00007f6173851e80> #<Tag:0x00007f6173851d90> #<Tag:0x00007f6173851cc8>

Hi together,

I try to create my Things via REST API. I use python along with the requests library.
The creation of Things and Items is easy and runs smoothless.

However, I do not understand the REST API Doc in which way I can create the Channels of a Think.
Linking them is also possible with the corresponding endpoint.

Here is the payload for the Thing:

{
    "label": "Subwoofer",
    "bridgeUID": "mqtt:systemBroker:embedded-mqtt-broker",
    "configuration": {},
    "properties": {},
    "UID": "mqtt:topic:44Subwoofer68Subwo",
    "thingTypeUID": "mqtt:topic",
    "channels": [
        {
            "uid": "mqtt:topic:44Subwoofer68Subwo:SubwooferPower",
            "id": "subwooferpower_power",
            "channelTypeUID": "mqtt:switch",
            "itemType": "Switch",
            "kind": "STATE",
            "label": "SubwooferPower",
            "description": "Control ON/OFF",
            "properties": {},
            "configuration": {
                "commandTopic": "cmnd/subwoofer/POWER",
                "stateTopic": "state/subwoofer/POWER"
            }
        }
    ],
    "location": "Living Room"
}

But results in:

<Response [500]>
{‘error’: {‘http-code’: 500, ‘exception’: {‘class’: ‘java.lang.NullPointerException’}}}
Traceback (most recent call last):
File “openHabConfig.py”, line 88, in createThing
r.raise_for_status()
File “/Users/olivergoetz/Development/home-automation/venv/lib/python3.8/site-packages/requests/models.py”, line 941, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: http://openhab:8080/rest/things

Currently I do not know how to proceed.

It gives a error 500 on a NullPointerException. Meaning something went wrong. Possible you missed a property or have a typo in the json and the rest api didn’t handle it properly. I would expect you should be able to find the error in the openHAB log file That might give an indication what is missing.

I’ve created HABApp which provides this functionality as an API which is exposed to the user.
It also has a powerful processor to automatically create items and link them to things.

If you don’t want to use it you can still look at the implementation (it’s python) and copy paste the code.