Scripted Thing creation

Does it currently exists a way (apart from REST calling) in JSR223 to create and delete things ?

How would that work? Even the Paper UI uses the API for its functions.

I guess you could have the script create an OH1 style Thing file but that seems like an ugly way to accomplish this.

Currently it is possible to create Items with Jython in the helper libraries. I wonder if the same exists for Things.

The short answer is yes, but I haven’t seen a need to create a library for it yet.

Scripted automation is currently setup to allow dynamic imports of shared OH packages. You can also manually import from a file. This is true for all the supported JVM languages. Jython is special in a number of ways though… one of which is that it can be setup to access private and protected instance methods and attributes.

Let me know if you need any help or an example!

2 Likes

JSR223 has direct access to the core parts of OH that are exposed through the REST API, in some cases. The OP is a pretty reasonable question.

My question to @glhopital, what use case do you have in mind?

I’m intending to create a module that automatically generate things and items for a given list of network equipments (stored in a csv file).

1 Like

I thought the goal for OH2 bindings was wherever possible, have the binding generate the Things.

In some use cases that doesn’t always make the binding feasible or reasonable. For example, let’s use this use case. If you do a scan with the Network binding, you will get everything that responds to a ping on your network. That can be many hundreds of devices you don’t care about to sort through. And even that will only pick up devices, not services.

OK, now let’s imagine it from the other side. I have dozens of services I want to monitor with Things and for some reason I already have them in a csv file. My choices are:

  • hand click to create them in PaperUI
  • create .things files which may become deprecated
  • use the REST API directly

None of these are really good choices and I hope in OH 3 something comes along to make this better.

But with JSR223, especially the Jython language, we have access to core parts of OH to do things using code that are not available in Rules DSL. From OH’s perspective, there is no difference between clicking together a Thing using PaperUI and writing a few lines of Python code to do it. It is all interacting with the Thing Registry (or what ever they call it).

1 Like

That is awesome!