Change item names in json using grep and sed OH4 (see warnings)


I was wanting to rename over 100 items because I have moved house and wanted to have new location names and also items had long names which I wanted to shorten. All of the names I wanted to change are greyed out in the UI which means I have to edit the json files.

Seeing as I upgraded to OH4 as well I may as well go all the way and change the names.

I am running on Debian bookworm Linux.

Yes, I could have deleted and recreated all of them but I would also have to find then items in rules and sitemaps (which I don’t use but have an old one for reference), but where is the fun in that?

NOTE:I don’t have any DSL rules and just use the UI.

I wrote a script which uses grep and sed.
It checks to see if the old item name is in the json files and then checks to see that the new item name is NOT in the json files.

The script will find and change items in things, items, rules, sitemap. Then it will rename rrd files to the new name so you will still have the graphs.

The old name and new name are put in a file (namechange.txt) so you can do as many item changes as you want. You do have to be mindful of the order of changes if there is a whole word that is used in a few items.
Doorgarage_Doorgaragelastupdate Door_garage_lastupdate
Doorgarage_Doorgarage Door_garage
Doorgarage gDoor_garage

The above is correct.
and the results would be:

Whereas the example below:
Doorgarage gDoor_garage
Doorgarage_Doorgaragelastupdate Door_garage_lastupdate
Doorgarage_Doorgarage Door_garage

Would give you the result:

So a big warning you can do damage…be warned.
You can do a few items at a time and see what happens and if it is not what you expect you can restore as the script does an openhab backup.

So, if you are feeling brave here is the script.

WARNING: If I was you and want to try this script I would set up another Openhab installation and really test it out as editing json files is a last resort according to this:

Also I would not recommend changing binding assigned names. Only change name you have created.

That said…be warned. I have done it and survived and I changed 100 items in one go.


#echo this will change the names in openhab database
#echo original name is the first parameter and name to change it to is the second parameter
#echo these parameters are in the namechange.txt file which is in the same folder as this script
#echo do not put spaces in the names as the space is the delimiter of the parameters and can stop OH from starting
#echo just put ONE space between the items

#check if data IS in the json files
while read oldname newname ; do
check_json () {

grep -w $oldname /var/lib/openhab/jsondb/*.json >/dev/null 2>&1
if [ $? -eq 1 ] ;then
echo json file does not exist original: $oldname

#check if data is NOT in the json files
grep -w $newname /var/lib/openhab/jsondb/*.json >/dev/null 2>&1
if [ $? -eq 0 ] ;then
echo "json file name exists >>>>>>> " $newname


done < namechange.txt

echo If nothing shows then all good to do the change
read crap

echo stop openhab.
sudo systemctl stop openhab.service
#Stop reverting to older backups
sudo rm /var/lib/openhab/jsondb/backups/*
echo make a backup just in case
sudo openhab-cli backup ~/OH4-namechange-$(date +%F-%T).zip

while read oldname newname ; do
change_json () {

sudo sed -i "s/$oldname/$newname/g" /var/lib/openhab/jsondb/*.json
sudo sed -i "s/$oldname/$newname/g" /etc/openhab/sitemaps/jedwood.sitemap
#rename the persistence files so we do not lose the data and graphs
if test -f "/var/lib/openhab/persistence/rrd4j/$oldname.rrd"; then
sudo mv "/var/lib/openhab/persistence/rrd4j/$oldname.rrd" "/var/lib/openhab/persistence/rrd4j/$newname.rrd"


done < namechange.txt

Here is an example of the namechange.txt file

Zigbee_door_front_lh_Door_front_LH Zigbee_Door_front_LH
Zigbee_door_front_lh_Door_front_LH_battery Zigbee_Door_front_LH_battery
Zigbee_door_front_lh_Door_front_LH_lastupdate Zigbee_Door_front_LH_lastupdate

If there are any real programmers out there who can make this script better or find any errors feel free to post up changes. I am sure there are better ways to do this but it worked for me.


Indeed because the Item name is the Items UUID. The only way to change the UUID using normal processes is to delete and recreate the Item.

Just for clarification, the distinction is whether rules are defined in separate text files in $OH_CONF or are managed through the UI. You can use Rules DSL in either case.

The one big thing your instructions are missing is that the script should only be run while OH is stopped. Running this while OH is running is likely to cause problems.

Correct. The script does stop Openhab before making changes. But glad you brought that point up.

1 Like