I am trying to find out, how configuration of items can be speeded up. For example I have 30 batterylevel items and I want to set semantec class and semantec properties for all of them at the same time. Is that possible?
And I am trying to find out how creation of items can be speeded up. I have learned, that to get the last timestamp of an item which has been updated I have to create a special item. I have lots of channels, where I would like to have the last updated timestamp. In Pimatic, this is a default setting. Every persisted item has a last updated timestamp. In Openhab you have to create it manually. This is very time consuming.
It is only sort of possible at the moment. Bulk editing of items is one of the reasons some people still prefer file-based item creation instead of UI managed items. But, there are some ways you can achieve this even though there is no direct bulk editing in the UI yet.
Add Items from Textual Definition - This is an option under the developer tools and although the name just says ‘Add’ it also will modify existing items if you give a basic textual definition with an item name that already exits. All you need to do is paste in lines of code that would go in a .items file and press the add button. Pros: All the speed and bulk modification of your preferred text editor, Cons: It doesn’t really save you any time at all if you don’t already have that text configuration created and once you have (re)created the UI managed items there is no way to sync UI changes with the text you have if you ever want to try this again.
Rule - many of the newer rule languages (e.g., JS scripting, or JRuby) give you helper libraries that come with easier methods for modifying items including tag management. For example, in JSScripting it might look something like this:
I’m sure @jimtng can show you the JRuby equivalent (one of these days I’ll learn ruby, I promise). Pros: Very rapid if you know the scripting language, Cons: virtually no checking so it will be very easy, especially with semantic tags where there are particular restrictions, to break something.
API - you can always use direct calls to the API to modify items. You can download the entire list of items you want to modify from the Get /items endpoint, make the bulk modifications to the resulting JSON structure, and then re-upload that modified JSON via the Put /items endpoint. Pros: There is at least checking that the JSON structure is correct when you use the PUT endpoint so truly damaging changes to your config are not likely. Cons: Many might not find editing the JSON particularly rapid or easy.
Direct jsondb editing - If you stop OH first (and backup any files you intend to edit), you can then directly edit the files in the jsondb folder where all the UI managed data is stored. For item tags this would be in the org.openhab.core.items.Item.json file. Pros: very few (really…don’t do this unless you have no other options). Cons: unless you are very careful and know what you’re doing you can do a lot of damage.
All of the above listed methods for modification of items also apply to creation of items. In the case of creating items, in particular though, if you can construct the items file text definition for the item then point 1 is a very effective choice because it essentially eliminates both of the major cons while keeping the pros. If you are good with rule scripting, then point 2 can also be very useful (in fact, I know some OH uses use rules to dynamically create all their items).
I’m OH 5 reach Item will have a lastUpdated and lastChanged property so this will no longer be necessary, unless you want to display this information on a UI in which case an Item will still be required.
Only changing the tags this way will not be persisted into the jsondb. You’ll also need to tell the managed provider that you’ve updated the item, so that it will persist the changes.
This is done in jruby, i.e. the changes will be persisted* (but there’s a bug I’ve just discovered when testing this - so it’ll be fixed in the next helper library version!)
# you have to set _all_ the tags here. All previous tags will be removed first
MyBatteryGroup.members.each { |item| item.tags = ["Tag1", "Tag2"] }
If these are semantic tags, you can use this syntax (although plain string like the above works too)
# you have to set _all_ the tags here. All previous tags will be removed first
MyBatteryGroup.members.each { |item| item.tags = [Semantics::Status, Semantics::Level] }
I think I saw a feature request, forgot where, probably in the 5.0 wishlist thread, for bulk editing multiple items / things in a table.
The bug in jruby to modify tags has been fixed in v5.35.1. Users can update to this version by restarting openhab or restarting the jrubyscripting addon.
To update several different properties, you can wrap the changes in a modify block like this:
MyBatteryGroup.members.each do |item|
item.modify do
item.tags = Semantics::Status, Semantics::Level
item.label = "#{item.equipment.label} Battery Level"
item.icon = "battery" # This is an alias for item.category
end
end
This will ensure that only one update call is made at the end of all the modifications.