How to add meta data while creating an item via REST API

  • Platform information:
    • OS: Ubuntu 22.04 LTS
    • openHAB version: openHAB 3.4.4

Hallo everyone,

I try to create a series of items via RESTapi. However, I found that the metadata will not be accepted.
For example I try with:

import requests
import json

url = "137.226.248.161:8080/rest/items"

payload = json.dumps([
  {
    "type": "Number",
    "name": "HP_building11",
    "label": "HP_building11",
    "stateDescription": {
      "pattern": "%.2f",
      "readOnly": False,
      "options": []
    },
    "category": "",
    "tags": [],
    "groupNames": [],
    "function": {}
  }
])
headers = {
  'Content-Type': 'application/json',
  'Accept': '*/*',
  'Authorization': 'Bearer oh.api1.p3OWdd71XnuM5mkU2758Jy1avG40Pw24qEFsLpcK8dq15INP2bfKuLB2qAuXzZgzzDwxU6t32aDPOeIiJ19w'
}

response = requests.request("PUT", url, headers=headers, data=payload)

print(response.text)

But when I check the item, the meta data statedescription just disappears:

{
    "link": "http://137.226.248.161:8080/rest/items/HP_building11",
    "state": "NULL",
    "editable": true,
    "type": "Number",
    "name": "HP_building11",
    "label": "HP_building11",
    "category": "",
    "tags": [],
    "groupNames": []
}

How can I add meta data with RESTapi? Or is it even possible?

Hi,

PUTting metadata requires another endoint:

PUT
/items/{itemname}/metadata/{namespace}

So in your case that would be putting just the metadata for the namespace:
/items/HP_building11/metadata/stateDescription with data in form of:

{
  "value": "string",
  "config": {
    "additionalProp1": {},
    "additionalProp2": {},
    "additionalProp3": {}
  }
}

I generally test this by adding data for one item manually and then checking the names of the additionalProp stubs in the example with proper values.

I do think however that this data is filled upon linking with a thing, I fill my system via API as well and never added this metadata myself, it seems to turn up after linking.

Wouldn’t you have to set the label to
HP_building11 [%.2f]
to create the state description accordingly?

Otherwise it’s a second request to the metadata endpoint

Hi Jos, thanks a lot! That work for me. In my case, I need to send another request to

PUT
/items/HP_building11/metadata/stateDescription

with payload

{
  "value": " ",
  "config": {
      "pattern": "%.2f"
  }
}