[script] find items or other objects in OH environment

Hi!

I wanted to give something back to the community, as I have already received a lot of help here in the forum, for which I am very grateful!

I have written a Python3 script that searches for items, things or other objects in the conf files or in the jsondb.
Reason:
i just wanted to find referenced items or things that i don’t use in OH anymore. also i wanted to rename item names with my second script “python_find_replace” and wanted to know where the old item name occurs.

my repo:
OH find items or objects

Any suggestions or commits are welcome :slight_smile:

4 Likes

This is a nice script! I had a plan to write something similar but now I don’t have to. Just a couple of thoughts off the top of my head:

  • Item metadata is an important thing that is missing. Given how metadata is handled, particularly with UI created Items, it becomes very easy for lots of cruft to build up there if you go and start changing Item names or deleting Items without going through the REST API.

  • As a minor coding structure comment, given how you are using them, I’d split your read_json_or_file into two separate functions, one for JSON and the other for the other configs.

  • One use case that is challenging is that you can have Items with metadata or Links defined in JSONDB but the Items and/or Things are defined in text configs. It’s not clear to me how easy that will be to correlate given the output. But I need to play with the script some more, I might just be missing something obvious.

  • Bringing in more information from the REST API could be useful here I think. The REST API is going to give you a full listing of everything no matter how it’s defined in a unified JSON format. I wonder if it’d be possible to query for everything that way and do the search through the fields first, and then determine where they are defined where one has a hit. For example, one of the fields in the JSON will be whether or not that entity is “managed” or not. If it’s not “managed” that means its defined in a text config somewhere and the Python script could search for it there and tell you what file it’s in. If it is managed, the script could output a URL that takes you straight to the page to edit that entity. Though you might not need to do that using the REST API but that would be the first step to potentially adding the ability to the script to change the name of an Item for you.

Thanks for posting!

thank you for the answer!
i will soon revise the code again :slight_smile:

see my updated script:
now it is possible to list all active items or list all orphaned items from jsondb and conf files which are existing but aren´t used.