Hy
Almost 1.5 months ago I switched to OH3 from my OH2 machine. That worked great and at the same time I had a first hand on the semantics model and all that belongs to it. In my OH2 times I manged all my configurations in my text files since it was way easier to implement a new sensor by copy & paste and adjusting the name.
But as I saw the whole semantics and the Model thing I decided to switch to full UI configuration and only leave my rules text based.
Now 1.5 months later I really struggle to edit several items at once since it needs so many mouse clicks where in the text based files a replace command can do this in seconds.
So Iâm conveting back all my Items to the *.items text files. Ohhhh boyâŠ
First I tried to do it manually by looking at the *json files in the userdata folder but this took too long so I wrote a small python script which does the job for me.
The script is far from finished but it might help others if they have the same feelings about UI inputs as me.
Currently it converts:
- itemType
- name
- label
- category / icon
- Group
- Tag
- Channel Link (only tested mqtt)
- stateDescription
- expire
- widget
- cellWidget
- autoupdate
- listWidget
So as you can see it does not convert Alexa, HomeKit, GA, Synonyms etc.
Since I donât use them I canât verify if theyâre working but I think itâs rather easy to add those to the script if you need them.
So here is the script:
PLS ONLY USE IT WHEN YOUâRE SURE WHAT YOUâRE DOING
It needs a copy of those for files located in the same directory as the script:
org.openhab.core.items.Item.json
org.openhab.core.items.Metadata.json
org.openhab.core.thing.link.ItemChannelLink.json
org.openhab.core.thing.Thing.json
When finished it writes all Items to the export.items file where you can sort them take a few out and make your own *.items files as you desire.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
print("pls wait...")
exportStr = []
with open('org.openhab.core.items.Item.json') as json_file:
data = json.load(json_file)
for p in data:
# itemType
itemType = (data[p]['value']['itemType'] + " ")
# name
name = str(p)
# label
label = ""
if 'label' not in data[p]['value']:
label = (" \"" + name + "\"")
else:
label = (" \"" + data[p]['value']['label'] + "\"")
# category
category = ""
if 'category' in data[p]['value']:
category = data[p]['value']['category']
if category:
category = (" <" + category + ">")
# groupNames
groupNames = ""
if 'groupNames' in data[p]['value']:
groupNames = ""
for erg in data[p]['value']['groupNames']:
groupNames = groupNames + erg + ", "
if groupNames:
groupNames = (" (" + groupNames.rstrip(', ') + ")")
# tags
tags = ""
if 'tags' in data[p]['value']:
tags = ""
for ergTags in data[p]['value']['tags']:
tags = tags + "\"" + ergTags + "\", "
if tags:
tags = (" [" + tags.rstrip(', ') + "]")
# ItemChannelLink
channel = ""
uid = ""
with open('org.openhab.core.thing.link.ItemChannelLink.json') as json_file_links:
dataLinks = json.load(json_file_links)
# print(dataLinks)
for pLinks in dataLinks:
if name == dataLinks[pLinks]['value']['itemName']:
uid = dataLinks[pLinks]['value']['channelUID']['uid']
with open('org.openhab.core.thing.Thing.json') as json_file_things:
dataThings = json.load(json_file_things)
# print(dataThings)
for pThings in dataThings:
if pThings in uid:
channel = uid
# Metadata
metadata = ""
stateDescription = ""
expire = ""
widget = ""
cellWidget = ""
autoupdate = ""
listWidget = ""
with open('org.openhab.core.items.Metadata.json') as json_file_meta:
dataMeta = json.load(json_file_meta)
# print(dataMeta)
for pMeta in dataMeta:
if name in pMeta:
if "stateDescription:" in pMeta:
stateDescription = "stateDescription=\"\"["
if 'pattern' in dataMeta[pMeta]['value']['configuration']:
stateDescription = stateDescription + "pattern=\"" + dataMeta[pMeta]['value']['configuration']['pattern'] + "\", "
if 'min' in dataMeta[pMeta]['value']['configuration']:
stateDescription = stateDescription + "min=\"" + dataMeta[pMeta]['value']['configuration']['min'] + "\", "
if 'max' in dataMeta[pMeta]['value']['configuration']:
stateDescription = stateDescription + "max=\"" + dataMeta[pMeta]['value']['configuration']['max'] + "\", "
if 'step' in dataMeta[pMeta]['value']['configuration']:
stateDescription = stateDescription + "step=\"" + dataMeta[pMeta]['value']['configuration']['step'] + "\", "
if 'options' in dataMeta[pMeta]['value']['configuration']:
stateDescription = stateDescription + "options=\"" + dataMeta[pMeta]['value']['configuration']['options'] + "\", "
stateDescription = stateDescription.rstrip(", ") + "], "
# print(stateDescription)
if "expire:" in pMeta:
expire = "expire=\""
if 'value' in dataMeta[pMeta]['value']:
expire = expire + dataMeta[pMeta]['value']['value']
expire = expire + "\", "
# print(expire)
if "widget:" in pMeta:
widget = "expire=\""
if 'value' in dataMeta[pMeta]['value']:
widget = widget + dataMeta[pMeta]['value']['value']
widget = widget + "\", "
# print(widget)
if "cellWidget:" in pMeta:
cellWidget = "cellWidget=\""
if 'value' in dataMeta[pMeta]['value']:
cellWidget = cellWidget + dataMeta[pMeta]['value']['value']
cellWidget = cellWidget + "\", "
# print(cellWidget)
if "autoupdate:" in pMeta:
autoupdate = "autoupdate=\""
if 'value' in dataMeta[pMeta]['value']:
autoupdate = autoupdate + dataMeta[pMeta]['value']['value']
autoupdate = autoupdate + "\", "
# print(autoupdate)
if "listWidget:" in pMeta:
listWidget = "listWidget=\"" + dataMeta[pMeta]['value']['value'] + "\"["
if 'title' in dataMeta[pMeta]['value']['configuration']:
listWidget = listWidget + "title=\"" + dataMeta[pMeta]['value']['configuration']['title'] + "\", "
if 'subtitle' in dataMeta[pMeta]['value']['configuration']:
listWidget = listWidget + "subtitle=\"" + dataMeta[pMeta]['value']['configuration']['subtitle'] + "\", "
if 'icon' in dataMeta[pMeta]['value']['configuration']:
listWidget = listWidget + "icon=\"" + dataMeta[pMeta]['value']['configuration']['icon'] + "\", "
if 'min' in dataMeta[pMeta]['value']['configuration']:
listWidget = listWidget + "min=\"" + dataMeta[pMeta]['value']['configuration']['min'] + "\", "
if 'max' in dataMeta[pMeta]['value']['configuration']:
listWidget = listWidget + "max=\"" + dataMeta[pMeta]['value']['configuration']['max'] + "\", "
if 'step' in dataMeta[pMeta]['value']['configuration']:
listWidget = listWidget + "step=\"" + dataMeta[pMeta]['value']['configuration']['step'] + "\", "
if 'vertical' in dataMeta[pMeta]['value']['configuration']:
listWidget = listWidget + "vertical=\"" + dataMeta[pMeta]['value']['configuration']['vertical'] + "\", "
if 'label' in dataMeta[pMeta]['value']['configuration']:
listWidget = listWidget + "label=\"" + dataMeta[pMeta]['value']['configuration']['label'] + "\", "
if 'scale' in dataMeta[pMeta]['value']['configuration']:
listWidget = listWidget + "scale=\"" + dataMeta[pMeta]['value']['configuration']['scale'] + "\", "
if 'scaleSteps' in dataMeta[pMeta]['value']['configuration']:
listWidget = listWidget + "scaleSteps=\"" + dataMeta[pMeta]['value']['configuration']['scaleSteps'] + "\", "
if 'scaleSubSteps' in dataMeta[pMeta]['value']['configuration']:
listWidget = listWidget + "scaleSubSteps=\"" + dataMeta[pMeta]['value']['configuration']['scaleSubSteps'] + "\", "
if 'unit' in dataMeta[pMeta]['value']['configuration']:
listWidget = listWidget + "unit=\"" + dataMeta[pMeta]['value']['configuration']['unit'] + "\", "
listWidget = listWidget.rstrip(", ") + "], "
# print(listWidget)
metadata = (stateDescription + expire + widget + listWidget + cellWidget + autoupdate).rstrip(", ")
lastEnt = ""
if channel:
lastEnt = "channel=\"" + channel + "\", "
if metadata:
lastEnt = lastEnt + metadata + ", "
if lastEnt:
lastEnt = (" { " + lastEnt.rstrip(", ") + " }")
exportStr.append(channel + " " + itemType + name + label + category + groupNames + tags + lastEnt)
# export all the data to a file and sort it
f = open("export.items", "w")
for line in sorted(exportStr):
f.write(line.split(" ", 1)[1] + "\n")
f.close()
print("done")
EDIT: exported Items list is now sorted after ItemType and then after channel so same channels stay together itâs therefore easier to separate them into different *.items files.
Andy