Hi,
Just wanted to share a very crude and simple (and potentially buggy) script I ended up writing, which allows to fetch some of your managed openHAB objects and store them locally as YAML files. You can then edit them and update them back, or create new objects. It can come handy when you want for instance to duplicate stuff easily, or use more powerful tools/editors, while the objects once updated back remain editable in the UI.
You can use it for these types of objects for now:
- UI pages
- UI widgets
- HABPanel panel configurations
- Rules
- Things
- Items
Prerequisites
The following tools must be available - note that as it interacts using the REST API you can work remotely, like on your workstation or laptop; these donât have to be on your openHAB system:
-
 curlÂ
(you probably already have it) -
 yqÂ
installation instructions: https://github.com/mikefarah/yq#install
docs: https://mikefarah.gitbook.io/yq/
The Script
https://gist.github.com/ghys/071cf630ede8e61634f4f7b2d90df260
For instance use (preferably in an empty directory):
wget https://gist.githubusercontent.com/ghys/071cf630ede8e61634f4f7b2d90df260/raw/34d7e664d463825bfefe964ef62f66d2182d597f/ohadm
chmod +x ohadm
Usage
Initial configuration
Edit the script and change the OH_HOST & OH_API_TOKEN variables at the beginning of the script.
Create an API token by navigating to http://$OH_HOST:8080/createApiToken
. Enter your admin credentials, choose a name (e.g. ohadm
) and specify admin
for the scope. Copy the resulting token (oh.ohadm...
) into the script before leaving the page. You can revoke the token at any time from the UI when you donât need it anymore.
Operations
Examples:
./ohadm fetch pages
./ohadm fetch widgets
./ohadm fetch panelconfigs
./ohadm fetch rules
./ohadm fetch things
./ohadm fetch items
For object types, singular & plural forms are equivalent (widget[s]
, page[s]
).
This will create a directory and put every objects of that type in its own .yml file:
$ ./ohadm fetch widgets
$ find widgets/
widgets/
widgets/widget_91ad261b1f.yml
widgets/UniversalRemote.yml
widgets/mode_test.yml
widgets/thermostat.yml
widgets/widget_980ed7320e.yml
widgets/printer_status_list_v1.yml
...
You can then edit these files with an editor or perhaps process them in bulk, for instance yq
has a lot of operators to manipulate YAML.
Examples:
./ohadm update page mypage1
./ohadm update widget widget_91ad261b1f
./ohadm update thing "exec:command:command1"
This will convert the <type>/<uid>.yml
file to JSON and update the corresponding object in openHAB.
Youâll get the raw response from the API, with the status code, inspect it to make sure the operation has succeeded. Usually when you get your object back as JSON it means it was updated successfully, otherwise youâd get a non-HTTP 200 OK, sometimes with an error message.
Examples:
./ohadm create page mypage2
./ohadm create rule rule1_copy
The <type>/<uid>.yml
file must already exist. This will create the corresponding object in openHAB.
IMPORTANT: the uid
(or UID
for things) property in the YAML must match the name of the file, otherwise it wonât work.
Beware of channels UIDs with things when duplicating a thing as well, they might still have the UID of the original thing.
Examples:
./ohadm delete widget old_widget
./ohadm delete panelconfig kitchen_backup
The corresponding <type>/<uid>.yml
doesnât have to exist on the filesystem in this case, but if it does exist, it will be removed!
Examples:
./ohadm get widgets
./ohadm get items | grep Rollershutter
./ohadm get rules | grep ERROR$
./ohadm get things | grep OFFLINE
Useful to quickly get a status of your objects in a tabular layout.
Filtering can easily be done with grep
.
Examples:
Get a YAML representation of the specified item:
./ohadm describe item MyItem1
Output the date of the last update of the specified widget:
./ohadm describe widget widget_4064e9fb1e | yq e ".timestamp" -
Output the status of the specified thing:
./ohadm describe thing hue:0210:001234567abc:4 | yq e ".statusInfo.status" -
Displays all rules which are currently not idle along with their status:
./ohadm describe rules | yq e '[.[] | select(.status.status != "IDLE")] | map({.uid: .status})' -
Displays all things not online along with their status:
./ohadm describe things | yq e '[.[] | select(.statusInfo.status != "ONLINE")] | map({.UID: .statusInfo})' -
Display the names of all items that have the tags âControlâ and âLightâ:
./ohadm describe items | yq e '[.[] | select(.tags | contains(["Control", "Light"]))] | .[].name' -
Dump the object (or all objects if <uid>
is omitted) in YAML. Especially useful for filtering with yq
as shown in the examples above.
Notes
You use this tool at your own risk
Since there are no checks on the object structures apart from those performed by the API, you can end up with invalid objects!
Some objects will have additional informations (for example rules and things will have a statusInfo
), normally itâs safe to upload those back as they will be ignored.
Hope itâs useful for you, and contributions are welcome.