openHAB 4 M2 includes an API to add custom semantic tags with some caveats:
- You can add custom semantic tags, but it won’t persist through openhab restart. This simply means you need to re-create them. So the ideal (and currently only) way to do this is by writing a script to create them. Your script gets loaded on startup so it works out fine.
- Once a custom tag is added, it cannot be deleted or modified - until openhab is restarted.
To create a custom semantic tag:
Using JS Scripting
const SemanticTags = Java.type('org.openhab.core.semantics.SemanticTags');
SemanticTags.add('YourCustomTag', 'ExistingParentTag');
SemanticTags.add('Balcony', 'Outdoor');
Or to specify a custom label, e.g. in your own language:
SemanticTags.add('YourCustomTag', 'ExistingParentTag', 'Your custom label', 'Comma,Separated,List,Of,Synonyms', 'Description');
The label, synonyms and description are all optional and can be null.
Using JRuby
This feature was added in the openhab-scripting helper library version 5.1.0
Semantics.add(YourCustomTag: Semantics::ParentTag) # Semantics::ParentTag is a placeholder for an actual existing tag
Semantics.add(StorageRoom: Semantics::Room)
# Once added, `Semantics::StorageRoom` became a valid semantic tag
# and can be used as the parent tag
Semantics.add(SecretStorageRoom: Semantics::StorageRoom)
# You can also use strings
Semantics.add("ExamRoom" => "Room")
# Or
Semantics.add("ConferenceRoom" => Semantics::Office)
To specify extra details for the tag:
Semantics.add(MyCustomTag: Semantics::Location,
label: "My custom label",
synonyms: "Foo,Bar",
description: "A longer description, currently not used")
You can also find out a semantic tag’s extra details
logger.info Semantics::Kitchen.label # The localized label for the `Kitchen` tag
logger.info Semantics::Kitchen.synonyms # An array containing the list of localized synonyms
Here’s a fictitious star ship with custom semantic tags:
Semantics.add(FoodReplicator: Semantics::Equipment)
Semantics.add(VoiceInput: Semantics::Point)
Semantics.add(MessHall: Semantics::Room)
Semantics.add(TransporterRoom: Semantics::Location)
Semantics.add(TransporterConsole: Semantics::Equipment)
Semantics.add(Holodeck: Semantics::Room)
Semantics.add(HolodeckControls: Semantics::Equipment)
Semantics.add(SafetyProtocols: Semantics::Control)
Semantics.add(StarShip: Semantics::Location)
Semantics.add(GalaxyClassShip: Semantics::StarShip)