OpenHAB 4.04 duplicate things and linked items

I set up a Tasmota Smart Plug using MQTT.
Works fine.
So I bought 4 more.
Since its quite a lot of work to set them up, I was wondering, if there is a way to copy the thing and change the name.

I did that with the thing code. So far thats a success, but I didn’t find a way to copy also the items linked to the channel.

The general process using the UI is:

  1. Open the “Code” tab for the Thing

  2. Copy the YAML you find there to the clipboard after “label” (the second line)

  3. Create a new Generic MQTT Thing

  4. Open the “Code” tab for the newly configured Thing and paste the contents over everything after “label”.

  5. Modify what ever needs to be modified.

You could also use the API Explorer.

  1. Query for the example Thing. Copy the JSON you get back.

  2. Got to the create Thing endpoint and paste the JSON into the body.

  3. Make what ever edits you need (don’t forget to at least change the UUID).

  4. POST the modified JSON.

  5. Repeat the edit and POST steps for each subsequent Thing.

Using the API Explorer can be a bit faster as there is less clicking around. However the JSON is a little harder to read and understand. You’ll have to decide which is more of a pain.

Yeah, I guess I wasn’t clear.
I did it that first way. Just created a new thing with just an UID and then past the “code” into it and changed the names of the chanels.
Thats ok, but I didn’t figure out how to copy the things items.
Since the Smart Plug has 10 channels/Items I would love to copy that as well.

I just had a quick look at the API Explorer.
Am I right, that I can query the things items? I will get the response with all items code. Then I change the code according to the new thing and “post” it?
This will create the new items under the new thing?

Thanks!

Items are not Things. They are their own thing (pun intended).

The easiest thing to do would be to use “Add equipment to model” or “Add points to model” from the Channels tab. That will let you create Items automatically linked to the Channels you choose.

No, Items are independent entities and their only association with the Thing is the ItemChannelLink.

Well, I think I get the concept of Things and Items and Channels.
My question is: how to duplicate the things’s items (channel links)

Thing1

  • channel1.1
    • item1.1
  • channel1.2
    • item1.2

Thing2

  • channel2.1
    • item2.1
  • channel2.2
    • item2.2

I am able to copy the thing and it’s channel, but still have to make a manual link and create each item.
If I have 100 Things of the same kind, I want to copy also the links and and change a suffix to make it unique.

This cannot be done easily for Items.

The easiest would be to copy the Things and then use “add equipment to model” to create the Items linked to the new Thing. Just about anything else is going to just as much if not more work than that.

To duplicate the Things though you can:

  1. Create the first Thing
  2. Open the code tab and copy the YAML you find there
  3. Create a new Thing
  4. Open the code tab and paste the YAML there, edit as necessary

Alternatively:

  1. Create the first Thing
  2. Go to Developer Tools → API Explorer → things
  3. Query for the Thing created in 1, copy the JSON returned
  4. Open the “create thing” endpoint, paste in the JSON, edit as necessary (be sure to change the UID) and submit

I read quite often about things and item files.
Is that still a way in OH4 to edit items?
Where can I find the item file?

Yes.

The file locations and syntax is documented in the Configuration section of the docs. For Items see Items | openHAB

Hi @rlkoshak, @satoshi

I know this is a very old thread but I found it when I had a similar challenge. With recent OH5 I found an easy and robust solution. I used that to “clone” 20 snmp targets which I use to monitor my PIs.

  1. Create and configure a good template thing and associated items. Spend some time to find a good naming scheme which makes it easy to replicate, e.g. use a common prefix with numbering… Spend some time to carefully define and configure all items. This will be used as a template for all “clones”.
  2. Copy the Thing to a new one via the UI. I keep the random uid but this won’t make any difference - it needs to be unique anyway.
  3. In Developer Tools there is an option to Copy all Items' file definitions to clipboard. Select DSL to generate all item definitions as DSL, copy it to the clipboard and paste it into a file all.items. A simple bash script searches all items with a common prefix and replaces the Prefix and Thing uid with sed with the new values. The result is stored as cloned.items. I then Copy&Paste the result into the UI Developer Tools → Add Items from Textual Definition and voila!

Please find below the bash I used to clone RPI6 items into a new RPI7 items. It is ugly and hard-coded but it may give you the idea. The comments are in German, but you will get it.
You need to replace the config values for old and new item-prefixes and old and new thing uid. Please note that the script also fixes the bug in the extract appending the icon’s file extension. This is not accepted by the UI in creating the new items.

#!/bin/bash

# ==========================================
# KONFIGURATION
# ==========================================
SOURCE_FILE="all.items"			# Die Datei mit dem DSL-Export aus der UI
OUTPUT_FILE="cloned.items"		# Die fertige Import-Datei für die UI

# Präfixe
OLD_PREFIX="RPI6_"			# Das Präfix deines Vorlagen-Geräts
NEW_PREFIX="RPI7_"			# Das Präfix des neuen Geräts

# Thing-IDs (Exakt mit Doppelpunkt vorne und hinten!)
OLD_THING_ID=":8e83a2a1c1:"		# Die ID des alten Things aus dem Channel-Pfad
NEW_THING_ID=":613deee8de:"		# Die ID des neu kopierten Things
# ==========================================

# Prüfen, ob die Quelldatei existiert
if [ ! -f "$SOURCE_FILE" ]; then
    echo "Fehler: Die Datei '$SOURCE_FILE' wurde nicht gefunden!"
    exit 1
fi

echo "Starte Extraktion und Ersetzung..."
echo "Präfix:   $OLD_PREFIX -> $NEW_PREFIX"
echo "Thing-ID: $OLD_THING_ID -> $NEW_THING_ID"

# Filtern der Vorlagen-Items und Ersetzen beider Werte (Präfix + Thing-ID)
grep -E "(^|[[:space:]]|\(|\[)$OLD_PREFIX" "$SOURCE_FILE" | \
sed -e "s/$OLD_PREFIX/$NEW_PREFIX/g" \
    -e "s/$OLD_THING_ID/$NEW_THING_ID/g" \
    -e 's/\.svg>/>/g' > "$OUTPUT_FILE"

# Zählen der extrahierten Items zur Kontrolle
ITEM_COUNT=$(wc -l < "$OUTPUT_FILE")

echo "--------------------------------------------------"
echo "Fertig! Es wurden $ITEM_COUNT Items erfolgreich verarbeitet."
echo "Die Datei '$OUTPUT_FILE' steht bereit für den UI-Import."

I use the same manually to find certain items in all.items and manually fix/update the definitions i.e. adding icons, harmonizing names or state descriptions etc. The good thing is, that the DSL item definitions are much easier to read and update as a human. One item → one line. I never got around parings complex JSONs.

Hope that helps openHAB friends.

Michael

Since this post was created a lot has changed in OH. In particular, the addition of the new YAML file formats and the https://openhab.koshak.us/addons/misc/misc-yamlcomposer add-on which lets you create templates. You could create a template that does your step 1, instantiate the Things by applying the template and create the Items by applying a template. There’s no need for a separate script or a lot of copy and paste.

This is particularly useful for low level Things where you have to define the Items like MQTT, SMNP, KNX, Modbus, etc.

Hi @rlkoshak,

Many tanks for the hint with yaml-composer. I will take a look at it. The doc looks interesting and the Add-on Store is always a good place to search.

As we all know there are always multiple solutions to achieve the same.

My objective was to quickly clone a set of 20 different items attached to an existing thing. These have been customized and fine tuned to my needs over the time. I wanted to “Paste” the configuration over a) already existing siblings to harmonize and b) creating a new set for another thing. I achieved that with a few commands on a text file which I can read and without the need to develop and debug YAML templates by reverse engineering the information I already have in a perfect template i.e. the crafted items itself. I even didn’t need to look at the existing items configuration.

Cheers