REST: bulk create things?!

I need to create 100 plus things (other than text files, but directly injecting into the REST API)… and was hoping to script this, as the things are sequentially named; e.g., smart_bulb_001 … 120.

Ideally, I’d like to add the channels too, which are also simple:
smart_bulb_001_voltage

smart_bulb_001_current
with number = [001…120]

Yes, there are other bulk categories: temperature, humidity, bulb, switch, plug, etc.

Any smart approaches to this?


While this post is a couple of years old, it sheds some light on the issue:

The things and items in question are all MQTT driven… hence, Rich’s moral here is use text files. (Easy as…)

The tricky part will probably be to create the correct json object to send to the rest api, but if you can create a template for that (which according to your info should be pretty easy) then it’s just about inserting the correct numbers and send it. Should be possible using shell scripting or any other scripting language you prefer. Even creating a JavaScript file to run in your browser should be quite easy.

Thanks…

I am new to this, not the scripting/formatting, but have to figure out how this jsondb business works; e.g., does OH have exclusive access, or can I just pipe stuff into the DBs w/o killing it.

I am in the process of setting up another machine to play with :slight_smile:

No, you need to stop openHAB before editing the JSONDB files!

1 Like

If you want to add directly to the jsondb you need to be more careful, but it’s possible as long as OH is shut down when making the edits. Note though that the whole file is a json object, so just appending things to the end wouldn’t work. The things would need to be added as a nested object (inside the outer {}). Going through the rest api is much safer.

1 Like

Why not make them have meaningful names?

I use the following approach:

  1. Create one example Thing through the UI through normal means
  2. Query the REST API using the API Explorer for that Thing to get it’s JSON (you could also just look at the JSONDB file directly
  3. Using the API Explorer paste/edit/submit the modified JSON for each Thing to create that is basically the same as the example.

Using find and replace you can create dozens of similar Things in a matter of minutes. It’s fast enough that I’ve never bothered creating a script as that would take longer than just creating the Things in this way.

You could of course script it with bash and curl commands or the like.

I agree. In addition, going through the REST API will do at least some modest checking on the JSON so if you mess it up somehow (e.g. accidentally delete a comma) you’ll get an error without completely destroying the whole file.

It’s also worth noting that it is possible to create Things through jRuby scripts. It might be possible through other JSR223 style scripting but you’ll need to work with the raw OH APIs and the ThingRegistry.

1 Like