How to write a binding to be connected to my server?

Tags: #<Tag:0x00007f617002ae08> #<Tag:0x00007f617002aa48>

I am new in OpenHAB. Please lead me in case my question is not formalized in a best way.

I have my server which is including the status of my home items (in JSON format).

As far as I understood, I can write a binding to read the status of the items (listen for changes) and changing the status of them via a REST API.

Is there any tutorial/sample in this regard that I can find how to integrate my idea into a real outcome?

Thanks for your time and help in advance

welcome !

Could you please describe a bit more in detail, what you’re about to do?
I get, you have a server, which provides information in JSON format?

You probably don’t need a binding for something like this. Take a look at Comprehensive Wunderground using HTTP Binding Example and see if something like that would work for you. It polls Wunderground and parses out the data from the returned JSON to populate various OH Items.

If you want to push the data rather than poll for it, you will need to make changes to your server that generates the JSON to publish the data in some way. One of the easiest ways would be to post the JSON to an OH Item using OH’s REST API and write a Rule to parse out and update Items as appropriate.

Especially if you are not super familiar with OH and/or Java programming, doing the above will be WAY less work.

You can learn and experiment with the OH REST API through the REST API Docs which not only documente the API but let you exercise and test the REST API right from the docs. You can install it from the Misc tab in Add ons in PaperUI.

hi @binderth and thanks for your reply.

Yes I have a server provides information in JSON format.

I want to read the status of each item (e.g. I have “light01” with the status of “ON”) using OpenHAB2 and I also need to be able to change the status (e.g. I have “light01” change the status to “OFF”) using OpenHAB2.

ok. all right - then I wanted to tell you exactly that, what Rich already wrote - but he wrote it better than me! :wink:
use the weather-binding example and retrieve your JSON with http-binding and then split it in items.

hi @rlkoshak and thanks for your reply.

I am sorry in advance, but I kind of lost up to now :frowning:

for example, my server provide this JSON code below in http://localhost:8090/status address:

[{“publisher”:“Pose2D”,“name”:“tb3”,“x”:0.0,“y”:0.0,“theta”:0.0}]

I can change the the value of X, Y, and THETA via sending this request:

http://localhost:8090/poseBot?x=1&y=1&theta=10

Now, I want to make an item in OH2 to read the status (X, Y, and THETA) of tb3 and be able to change them.

I read your explanation and I admit that it is well explained but I could not go far more than Items section.

I will really appreciate, if you can help me in this matter.

JSON is very easy to understand, if you write with some indents:

[{
    “publisher”:“Pose2D”,
    “name”:“tb3”,
    “x”:0.0,
    “y”:0.0,
    “theta”:0.0
}]

in fact, that one is very simple, because it has only one hierarchy. What I find weird, but it could be your browser changing the " to “ ”
Ok. So I guess, you want at least three items, then you grab it like this:

in your http.cfg

yourname.url=http://localhost:8090/status 
yourname.updateInterval=60000 // one minute

then you can add the items:

String MyPublisher      "Publisher [%s]"  { http="<[yourname:60000:JSONPATH($.publisher)]" }
String MyName           "Name [%s]"       { http="<[yourname:60000:JSONPATH($.name)]" }
Number MyNumberX        "x [%.0f °C]"     { http="<[yourname:60000:JSONPATH($.x)]" }
Number MyNumberY        "x [%.0f °C]"     { http="<[yourname:60000:JSONPATH($.y)]" }
Number MyNumberTheta    "theta [%.0f °C]" { http="<[yourname:60000:JSONPATH($.theta)]" }

Bonus:
if your JSON consists of more than one hierarchy, you can add this one simply:

[{
    "publisher":"Pose2D",
    "name":"tb3",
    "x":0.0,
    "y":0.0,
    "theta": {
        "x": 0.0,
        "y": 0.0
    }
}]
String MyPublisher       "Publisher [%s]"   { http="<[yourname:60000:JSONPATH($.publisher)]" }
String MyName            "Name [%s]"        { http="<[yourname:60000:JSONPATH($.name)]" }
Number MyNumberX         "x [%.0f °C]"      { http="<[yourname:60000:JSONPATH($.x)]" }
Number MyNumberY         "x [%.0f °C]"      { http="<[yourname:60000:JSONPATH($.y)]" }
Number MyNumberThetaX    "theta y[%.0f °C]" { http="<[yourname:60000:JSONPATH($.theta.x)]" }
Number MyNumberThetaY    "theta x[%.0f °C]" { http="<[yourname:60000:JSONPATH($.theta.y)]" }

you can add more hierarchy with “.”

Hi,
maybe this link could be helpful regarding JSON, http://www.jsonschema2pojo.org/
Please, let me know if you find something ‘no good’ about the produced code, I have sometimes used it and I have not found anything suspected so far, the code has just worked, so just in case.

Basse03

@binderth items should be defined in the http.cnf or in another file like binding.xml?

I am asking the question because I am not sure that how and where to get this item in my openhab panel.

the JSON code I have made actually, the complete one is something like this:

[{“publisher”:“Pose2D”,“name”:“tb3”,“x”:0.0,“y”:0.0,“theta”:0.0}, {“publisher”:“PoseStamped”,“name”:“tb3”,“x”:0.5,“y”:0.0,“z”:0.0,“w”:1.0}, {“publisher”:“Pose”,“name”:“tb3”,“x-position”:0.0,“y-position”:0.0,“z-position”:0.0,“x-orientation”:0.0,“y-orientation”:0.0,“z-orientation”:0.0,“w-orientation”:0.0}]

but I did not want to post all to make it simpler to reply and to understand.

Thanks again for your helps, I really appreciate it

@Basse_03 thanks

I always use this one:

https://jsonformatter.curiousconcept.com/

http.cfg sets the HTTP binding up to periodically poll the HTTP address and cache the address. In Thomas’s example it is configured to poll the URL once per minute.

This is so you can have multiple Items populated with the results from a single poll on the URL. Otherwise there would be a separate HTTP GET to the server for each Item.

Items are not defined in cfg files.

There is no binding.xml

Please review the Beginner’s Tutorial, the Concepts section of the User’s Guide, and the Items page under Configuration. To be successful with OH and to make progress you must have a basic understanding of Items, Things, and how they are related to each other and to Bindings.

Then review the HTTP Binding README which explains how the HTTP binding works and how to configure it.

In this particular case, the thing that is holding you back the most is a lack of understanding of some basic OH concepts. It will be hard for you to progress and hard for us to help until you have this basic understanding.

To provide high-level answers to some of your questions:

  • HTTP binding is a 1.x binding so there is nothing you can do to manage it in PaperUI beyond installation.
  • Items that use 1.x bindings must be defined in .items files.
  • The HTTP Binding README fully documents how to set up polling on an HTTP address (see Thomas’s example) as well as how to issue a command on a URL on demand.
  • One puts Items in .items files and then the Items can be put on your User’s UI (NOT PaperUI) based on how the UI works. In the case of BasicUI this means writing a .sitemap file. In HABPanel you can build up the UI in the browser.
1 Like