Virtual Device to OpenHAB

Tags: #<Tag:0x00007f4348f87f40> #<Tag:0x00007f4348f879f0>

I got some problem when try to create an virtual device for openhab 3. In my project, i have to create an virtual device on website and send datas to openhab server by REST API. Anybody have an idea about that ?

You’ve not really explained what your trying to achieve.

But if your trying to get data into your solution then just create an Item.

i’m trying to make an IoT Home Simulator website. So all IoT device is a virtual device, and add them to openhab by REST API. U said just create a Item, do u have any documentation about it ? Follow what i know, i need make a custom binding to conncet my virtual device to openhab.

Welcome to the openHAB forum.

Creating a such “virtual device” is beyond the scope of this community. The community that appointed you to such task should have given you the needed informations and/or skills.
When you are trying to use the data from this device in openHAB we could help.

IMHO the HTTP binding would be your needed binding.

i think i need a help to use REST API to create a thing completely into openhab. i don’t know how to create a standard post request to openhab.

And i got error when try send data. i receive “POST http://localhost:8080/rest/things 401 (Unauthorized)” in console.

You’ll find all the documentation here.

I think your trying to over complicate things. If your just trying to simulate Automation then you just need to create the associated items for the devices you want to add data to.

maybe. i read that doc but it doesn’t have what i need. can u give me some example about request body of thing data.

When you go to developer tools → Api Explorer, you will find all the examples you need, even curl syntax is shown.

1 Like

yepp, i did. But i need some example value for it. and when i try to make POST request, i got this error “POST http://localhost:8080/rest/things 401 (Unauthorized)”

1 Like

The 401 error means you have not provided login credentials. Sending a POST to http://localhost:8080/rest/things will achieve nothing.

If your getting a response back then we can assume that your making the call from the same server that’s running OH.

We’re trying to assist you with this problem, but we’re not fully aware of what your end goal happens to be and what you’ve done so far. Can you explain, no need to be fully detailed, but something that gives us an idea of your problem statement.
Also explain in more detail what you’ve done so far to resolve your problem.

Let’s get the basics sorted.

An example of a Virtual Devices should be very straight forward. Let’s use a Switch as an example. So for context, you’ll go with a Switch. Let’s say it’s a Z-Wave plugin module that provides an ON/OFF function. Firstly you would add the device to your Z-Wave network which would create a new Thing. As we’re talking Virtual Devices we aren’t do that. So we have to move onto the next stage and that is to device the Items that are associated with the Switch. In our example, this is a Binary switch item as we only need to be able to switch is ON and OFF.

Here’s is where you can read about how Items are defined.

Switch Livingroom_Light "Livingroom Ceiling Light" <switch>

This is broken down as follows.

**Device Type** - Device Name - "Friendly/displayed name" - <Icon type used> 

This would be associated with your Thing by adding on the channel details at the end. But as we’re trying to create a Virtual device we don’t need to do that here.

Now to update this new Virtual device via the REST API you now need to perform the following, below this would change the state to be OFF.

curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "OFF" "http://{openHAB_IP}:8080/rest/items/Livingroom_Light"
1 Like

i want to send post request to create thing of openhab from my client by vuejs with axios for api.

const headers = {
“Authorization”: “oh.test…”,
“Content-Type”: “application/json”,
“Access-Control-Allow-Origin”: “*”
};
HTTP.post(things, thing, {headers:headers}).then(response=>this.thingID=response.data.id)

This is my test code to do it. when server run, status code return 401 like what i said. Do u have some solution ?

No I don’t have a solution for you as I still don’t get what your trying to achieve.

As I’ve said above, you are more than likely trying to over complicate a solution and ignoring the basics. I gather your trying to run a Marathon the day over you’ve learnt how to walk.

Also OH is designed to work with Real devices/things and not Virtual devices, that’s not to say it can’t be done. But you’ll have to look at the problem in a different way in order to find your solution.

As I’ve also said above, the 401 error is an Authentication issue. OH requires that you create an account and that allows you to update your instance in the way you require. For example, creating a new Item or a Sitemap. How you use this authentication via the REST API is explained in the documentation.

It’s really important that we understand exactly what you are trying to achieve over all so we can understand things like “why are you interacting with the Things rest endpoint?” Things are created and managed by bindings. You don’t have a binding. No binding, no Things.

You probably need to be looking at the Items REST endpoint.

You also need to tell us what you’ve tried, and what you get back. Problems need to be worked in order. For example, you’ve not really even acknowledged Garry’s post concerning the authentication/authorization problem. Until you get that part right, none of your attempts will work.

i fixed error 401 but now, serve return status error 500. And i need some example for json file or request body for POST request. thing data like this in openhab api explore but i need an exapmle to understand more

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

yeah, i know it designed to work with real device but i think if we have an json file with all data like way the device send to openhab so i don’t need an real device to make home automation system.

Creating a Thing needs a Binding that Thing belongs too. This is a fact!
If you read the docs carefully, you will see that the ThingUID is build of the Binding ID + the Thing ID.
Only virtual Items can be created without any channels/things/bindings bound to them.

that’s right, i fixed all error i said. I will close this post.

That’s fine.

You do not need to create Things and Items over API to do any of that.

Instead you can create an Item in the openHAB UI, just as you would for a real device. This is a one-time configuration.

When the Item exists, you can send it data over API.