Add a new thing and item with REST API

I want to add my Xiaomi gateway device and an item using Openhab REST API. I read the documentation and found out that I must use POST /things endpoint for adding things and PUT /items for adding an item. The model schema for thing is:

{
  "label": "string",
  "bridgeUID": "string",
  "configuration": {},
  "properties": {},
  "UID": "string",
  "thingTypeUID": "string",
  "channels": [
    {
      "uid": "string",
      "id": "string",
      "channelTypeUID": "string",
      "itemType": "string",
      "kind": "string",
      "label": "string",
      "description": "string",
      "defaultTags": [
        "string"
      ],
      "properties": {},
      "configuration": {}
    }
  ],
  "location": "string"
}

and for item is:

[
  {
    "type": "string",
    "name": "string",
    "label": "string",
    "category": "string",
    "tags": [
      "string"
    ],
    "groupNames": [
      "string"
    ],
    "groupType": "string",
    "function": {
      "name": "string",
      "params": [
        "string"
      ]
    }
  }
]

Can someone provide me with a working example to add my Xiaomi gateway and the following with REST API (I want the MQTT section as well):

Color Xiaomi_Color "Color" (gXiaomi) {channel="mihome:gateway:7811dcb24f2c:color", mqtt=">[mosquitto:xiaomiGW/color/pub:command:*:default],>[mosquitto:xiaomiGW/color/state:state:*:default], <[mosquitto:xiaomiGW/color/cmd:command:default]"}

You can’t do the mqtt section through the REST API and you can’t do it all in one REST call.

You need to create your Thing.

Then create the Item.

Then Link the Item to the desired Channel on the Thing. See the links endpoint of the REST API.

You cannot add or manage binding configs on Item for OH 1.x version bindings through the REST API. You can only use MQTT with Items defined in .items files, at least until the MQTT 2.4 is ready for general use.

I’m using the latest version of OH and MQTT. You mean, it is not possible at all to set the MQTTbinding with REST API?

It is not possible at all to set the binding config on an Item for ANY 1.x version binding.

Are there other ways, so that I can set a version 1.x binding (such as MQTT binding) remotely ?

The only way to set a 1.x binding config on an Item is to edit a .items files.

Can I edit my .items file remotely? (using SSH or something)

Of course. https://www.google.com/search?q=how+to+edit+files+through+ssh&rlz=1C1GCEA_enUS744US744&oq=how+to+edit+files+through+ssh&aqs=chrome..69i57.4768j0j1&sourceid=chrome&ie=UTF-8

Or samba.

Thank you. But getting back to my first question. Could you provide me with a working example (model schema) for adding my xiaomi gateway as a thing and the corresponding items?

No, I can’t. I don’t use Xiaomi. I can only direct you to the binding’s doc.

Hi @rlkoshak

I have developed our own device for wall switch control along with REST API implementation.

How can I link my device to openHAB?
Could I use HTTP Binding if our device work on REST API?

Like: https://api.sharpnode.com/v2/?command=user-devices&access_token=ZpPqdinofvBsvuDGE4Ab67lS3QH7YyNCoV17jzZxii

Any hint will be very helpful to get start. Thanks!

This should be posted as a new thread.

Assuming your API doesn’t require special headers (e.g. access tokens in the header) the HTTP binding and sendHttp*Request Actions will work. If they do require special headers, you would be better off creating a new binding. Honestly, even if it doesn’t, a new binding will be less work and far easier for users to configure and set up.

Thanks.

I try to explore more details in documentation related how to implement binding.

Right now, I have no idea and seems difficult for me.

Could we write curl based command in http binding?
Like:
curl https://api.particle.io/v1/devices/1800330xxxxxxxxxxxxx/light -d access_token=12345678974521 -d “args=L4,HIGH”

I’m assuming that is a POST since it has the -d options. You can send a sendHttpPostRequest with content which I think lets you pass data, but I’m not sure exactly how to format that.

If not, you can use the Exec binding to call curl.

1 Like