Export hybrid configuration on .things and .items files

Hello,
is there any way if I have Items and Things, some created from PaperUI and some in .things and .items files, to have all in files, a kind of export? Target is to install a new openhab (actually I have the 3.x and I’d like to install the 4.x but upgrading, is impossible and keep freezing). So I just need to have a new fresh installation, I’d prefer because I’d like to change persistence to Postgres and some other thing, but I need all the things and items, (rules are all on files)
Is there any procedure?
Thank you

Did you check this :

Ohadm - A simple script to manipulate openHAB objects and sync them to YAML files - Tutorials & Examples / Solutions - openHAB Community

Thank you for suggestion, but it is an export to yaml, it could be very useful to have .things and .items files so it can be easily exported in a new installation. Because with the cli I tried to export/import but really there is no way, it freeze and no success. I prefer a fresh installation with fresh bindings and import one by one thing and items. Is there any suggestion?

Thanks

OK, there is another solution, at least for items:

Converting UI items back to *.items File - Setup, Configuration and Use / Scripts & Rules - openHAB Community

I have used it and posted a little change I had to make, it is post Nr. 22 I think…

Well, I went the other way. Exported my old Items from openHAB 3, edited the files to correct some minor mistakes and imported all, Binding by Binding through the developer tools in MainUI.
No more textual config in my openHAB 4 installation.

For the Things, I always let the Bindings autodiscover them, so there will be no config errors which can be hard to find.

1 Like

As should be clear from @hmerk’s answers there is no native support to export from managed configs to the Xtend text file configs. There are some script and such posted to the forum here and there but that’s it.

However, this might be an XY Problem.

If your end goal is “a fresh installation with fresh bindings and import one by one thing and items” then it might be less work overall and give you a better result in the long run if you:

  1. install the binding
  2. configure the bridge Things when/if necessary
  3. discover the Things
  4. use “add equipment to model” to create anew the Items

If you are not bound and determined to have .things files and .items files, this approach minimizes the amount of work you need to do and gives you Items already situated in the semantic model giving you all the benefits that come from that.

Even if you already had .items files, it’s less work to recreate your Items using 4 than it is to try to retrofit your existing Items to a semantic model.

1 Like

The reason for me using the import function was to have a strict naming scheme for the Items, as this is used within the semanticHomeMenu project. Item names provided by creating equipment from Thing gives wrong Items, as there is no consistency within the channel names accross the Bindings.
I could have edited while creating the Items, but it was easier to import. At the end, all my Items are managed.

import json
import requests
import os

# Configurazioni
openhab_ip = "openhab-ip" 
openhab_port = "8080"
api_token = "token-API" 
output_dir = "./things" 

# Assicurati che la directory di output esista
os.makedirs(output_dir, exist_ok=True)

# Scarica il file JSON dalle API REST di OpenHAB
url = f"http://{openhab_ip}:{openhab_port}/rest/things"
headers = {"Authorization": f"Bearer {api_token}"}

response = requests.get(url, headers=headers)

if response.status_code == 200:
    things_data = response.json()
    print("File JSON scaricato con successo.")
else:
    print(f"Errore durante il download del file JSON: {response.status_code}")
    exit(1)

# Funzione per convertire una Thing in formato testo corretto
def convert_thing_to_text(thing):
    thing_type = thing["thingTypeUID"]
    thing_uid = thing["UID"]
    label = thing["label"]
    configuration = thing["configuration"]

    config_lines = [f"{key}=\"{value}\"" for key, value in configuration.items()]
    config_text = ", ".join(config_lines)

    thing_text = f'Thing {thing_type}:{thing_uid.split(":")[-1]} "{label}" [{config_text}]\n'

    return thing_text

# Organizza le Things per tipo principale
things_by_main_type = {}
for thing in things_data:
    main_type = thing["thingTypeUID"].split(":")[0]  # Usa solo il tipo principale (es. zwave, mqtt)
    if main_type not in things_by_main_type:
        things_by_main_type[main_type] = []
    things_by_main_type[main_type].append(convert_thing_to_text(thing))

# Scrivi ogni tipo principale di Thing in un file separato
for main_type, things_text in things_by_main_type.items():
    output_file_path = os.path.join(output_dir, f"{main_type}.things")
    with open(output_file_path, 'w', encoding='utf-8') as f:
        f.write('\n'.join(things_text))
    print(f"Configurazioni delle {main_type} salvate in {output_file_path}")

print("Processo completato.")

Solved with this little python script…and exported all to openhab 4 seems all working for now

1 Like