Semantic Tagging

Mastering the Semantic Model in openHAB: A Definitive Guide

Introduction to the Semantic Model

The semantic model is the backbone of openHAB’s organizational framework. It defines relationships between locations, equipment, and points, making your smart home intuitive, structured, and easy to manage. Proper tagging ensures:

  • Clean UI integration in MainUI.
  • Optimized voice assistant compatibility (e.g., Alexa, Google Home).
  • Dynamic rule creation, leveraging relationships between items.

This guide explains how to use the semantic model, avoid common mistakes, and create a clean, scalable smart home setup.


Key Components of the Semantic Model

The semantic model consists of three core elements: Locations, Equipment, and Points. Here’s how they interact:

  1. Locations

    • Represent physical areas in your home (e.g., Living Room, Kitchen, Basement).
    • Define where equipment and points are located.

    Example:

    Group gLocLivingRoom "Living Room" <sofa> ["Location"]
    
  2. Equipment

    • Represent physical devices or systems (e.g., Lights, Motion Detectors, TV).
    • Group points related to the device or system.

    Example:

    Group gEquipLivingRoomLights "Living Room Lights" <lightbulb> (gLocLivingRoom) ["Lightbulb"]
    
  3. Points

    • Represent individual functionalities of equipment (e.g., a switch, dimmer, or sensor).
    • Each point has one role (Point) and one characteristic (Property).

    Example:

    Switch LivingRoomLightSwitch "Living Room Light Switch" <light> (gEquipLivingRoomLights) ["Switch", "Power"] {channel="mqtt:homie300:Queen:light-12345:switch#power"}
    

Semantic Tags: Points and Properties

Each item must have:

  1. One Point Tag: Defines its functional role.
    • Examples: Switch, Control, Measurement, Status.
  2. One Property Tag: Defines the characteristic being controlled or measured.
    • Examples: Power, Brightness, Presence, Temperature.

Common Point Tags

Point Description
Switch Binary control (e.g., on/off).
Control General control role (e.g., dimmer, setpoint).
Measurement Observes or measures a value (e.g., temperature, presence).
Status Represents a state or alert (e.g., low battery).

Common Property Tags

Property Description
Power Controls or measures power state (on/off).
Brightness Controls or measures brightness level (0-100%).
Presence Detects motion or presence.
Temperature Measures temperature.
LowBattery Indicates low battery state.

Examples of Correct Semantic Tagging

1. Switch Light (Non-Dimmable)

  • Point: Switch (binary control role).
  • Property: Power (on/off state).
Switch LivingRoomLightSwitch "Living Room Light" <light> (gEquipLivingRoomLights) ["Switch", "Power"] {channel="mqtt:homie300:Queen:light-12345:switch#power"}

2. Dimmer Light

  • Point: Control (adjustable control role).
  • Property: Brightness (light intensity).
Dimmer LivingRoomLightDimmer "Living Room Light Dimmer" <lightbulb> (gEquipLivingRoomLights) ["Control", "Brightness"] {channel="mqtt:homie300:Queen:light-12345:dimmer#level"}

3. Motion Sensor

  • Point: Measurement (measuring motion detection).
  • Property: Presence (motion detected or not).
Switch FrontDoorMotion "Motion Detected" <motion> (gEquipFrontDoorMotion) ["Measurement", "Presence"] {channel="mqtt:homie300:Queen:motion-12345:motion#detect"}

4. Door Contact Sensor

  • Point: Measurement (measuring state).
  • Property: OpenState (open/closed).
Switch FrontDoorContact "Door Open State" <door> (gEquipFrontDoor) ["Measurement", "OpenState"] {channel="mqtt:homie300:Queen:door-12345:contact#open"}

5. Battery Level

  • Point: Measurement (measuring state).
  • Property: Battery (battery level as a percentage).
Number MotionSensorBattery "Battery Level" <battery> (gEquipMotionSensor) ["Measurement", "Battery"] {channel="mqtt:homie300:Queen:battery-12345:battery#level"}

6. Low Battery Warning

  • Point: Status (state representation).
  • Property: LowBattery (battery low state).
Switch MotionSensorLowBattery "Low Battery Warning" <lowbattery> (gEquipMotionSensor) ["Status", "LowBattery"] {channel="mqtt:homie300:Queen:battery-12345:battery#low"}

Best Practices for the Semantic Model

  1. Points Belong to Equipment:

    • Points should be part of an equipment group for clear hierarchy and UI structure.
  2. One Point, One Property:

    • Each point can have only one Point tag and one Property tag.
  3. Avoid Redundancy:

    • Use equipment tags to describe the device. Avoid duplicating this information in point tags.
  4. Reflect Real-World Usage:

    • Model locations and equipment based on user expectations, not hardware placement.
  5. Custom Tags Caution:

    • Only define custom tags when absolutely necessary, as some behaviors (e.g., UI display) depend on default tags.
  6. Not Everything Belongs in the Model:

    • Technical or intermediate items (e.g., rule triggers) need not be included in the semantic model.

Advanced Use: UI Semantics

To enhance UI presentation, use uiSemantics metadata. This adds natural-language context for locations, equipment, and points.

Example:

Group gEquipLivingRoomLights "Living Room Lights" <lightbulb> (gLocLivingRoom) ["Lightbulb"] {uiSemantics="uiSemantics"[preposition=" in the", equipment="Lights", location="Living Room"]}

Benefits:

  • Improved natural-language descriptions in the UI.
  • Customizable equipment and location naming.

Learn more: Semantic UI: Using Enriched Semantic to Ease UI Creation.


Dynamic Rules Using Semantic Tags

Semantic tags can be used in rules to dynamically interact with related items.

Example: Equipment and Status Check

var equipment = actions.Semantics.getEquipment(items[alertItem]);
if (equipment !== null) {
    var statusItem = equipment.members.find(i => i.tags.includes('Status') && i.tags.includes('LowBattery'));
    if (statusItem && statusItem.state === 'ON') {
        logInfo("Alert", "Low battery for: " + equipment.label);
    }
}

11 Likes

Great tutorial! Thanks for posting!

I’ve a couple of suggestions which you can take or not as you see fit.

To avoid confusion, I’d take the “Rules” out of this heading.

I’d use the word “should” here. It is allowed and reasonable to assign Points directly to a Location. I don’t recommend this as it kind of messes with the way they appear on the Location cards in the UI in particular, but doing so does not break the model nor does it break the UI.

I’d also add rule 5, since this trips a bunch of people up:

“Any Location, Equipment, or Point can only be a member of exactly one semantically tagged Group. You cannot have the same Item be in two locations at once, or a part of more than one Equipment at the same time.”

A rule 6 might be “The location of an Equipment in the model should be the location as seen by members of your household. Even if the actual relay that controls the kitchen lights is in a cabinet the garage, the location for the kitchen lights in the model should be the kitchen. That’s where users of the home will expect to find them to control them.”

And a rule 7 is “Not all Items need to be in the semantic model.”

I’m not sure the model supports more than one semantic tag at a time. And “Brightness” isn’t one of the deffault set of property tags, is it? To avoid confusion I would remove it from here.

I also think it may make sense to separate the Point tags and Property tags in the table since they are two separate types of tags and the Properties tag is what controls how the Items are shown on the Properties tab.

Beyond that, a link to OH 4.1: Tutorial to manage custom semantic tags (will hopefully be in the main docs soonish, issue is open) along with a mention that if the default set of tags are not sufficient one can define their own tags. This works best for locations, equipment and properties. Care should be taken when defining custom Points as a lot of stuff (like which type of control to present in the UI by default) is driven by the Point tag. You have to create a file to define new tags but once defined and created, you can select the custom tags in the UI same as the default ones.

This isn’t one of the well known metadata options in the UI. Do you know if there is an issue to add this to MainUI and the docs?

I know about synonyms which lets you provide additional names for entities outside the Item name and label but this is the first I’ve encountered uiSemantics. I’ll need to play with that.

Another benefit is the semantic model information is available in rules through Actions | openHAB. For example, for my sensor Equipment I’ve added a “status” Item. When ever one of sensors in the equipment stop responding, I use these Actions to get the parent Equipment Item and then find the Item tagged with “Status” and “Availability” to find that status Item and set it to ON/OFF as appropriate. This is an alternative to the Associated Items design pattern that uses Item names instead of the model.

Example code:

var {alerting} = require('rlk_personal');
var logger = log('Sensor Alert');
var equipment = actions.Semantics.getEquipment(items[alertItem]);
var statusItem = equipment.members.find( i => i.tags.includes('Status') && i.tags.includes('Availability'));

if(isAlerting && statusItem.state != 'OFF') {
  statusItem.postUpdate('OFF');
  alerting.sendAlert('Offline: ' + equipment.label + ' has stopped reporting', logger);
}
else if(!isAlerting && statusItem.state != 'ON') {
  statusItem.postUpdate('ON');
  alerting.sendAlert('Online: ' + equipment.label + ' is back', logger);
}
else {
  console.info('Sensor status update alerting ' + isAlerting + ' initial ' + isInitialAlert + ' equipment ' + equipment.label + ' status ' + statusItem.state);
}

I have a Script Condition that also uses the semantic model to make sure that the Equipment actually exists and whether the status Item is in the wrong state.

var equipment = actions.Semantics.getEquipment(items[alertItem]);
var statusItem = equipment.members.find( i => i.tags.includes('Status') && i.tags.includes('Availability'));
if(equipment === null || statusItem === null) {
  console.warn(alertItem + ' does not belong to an equipment or equipment doesn\'t have a Status Item!');
  false;
}
else {
  var statusItem = equipment.members.find( i => i.tags.includes('Status') && i.tags.includes('Availability'));
  console.debug('Sensor status reporting called for ' + alertItem + ', equipment ' + equipment.label + ', is alerting ' + isAlerting + ', and is initial alert ' + isInitialAlert 
               + ', current equipment state is ' + statusItem.state);
  // Sensor is offline                         Sensor back online
  (isAlerting && statusItem.state != 'OFF') || (!isAlerting && statusItem.state != 'ON');
}

isAlerting and alertItem come from Threshold Alert and Open Reminder [4.0.0.0;4.9.9.9] in this example. The alerting import comes from a personal library.

1 Like

We use it in semanticHomeMenu a lot and there was a post long time ago to use it for better presentation in lists within widgets. You can define a label, location and whstever you like…

Thank you for the suggestions. The edits are done.

3 Likes

Nice work!

In fact the Property tag is optional. You can just have a Point, or Switch, or Measurement item without specializing it with a Property tag if for example you didn’t find any suitable.

Likewise you can just tag an Equipment item “Equipment” instead of “Lightbulb” or another specialized class if you didn’t find any.

1 Like

Though now that we can define our own tags I would recommend creating a new tag that is suitable if you don’t find what you need among the defaults. One imact in leaving the tag out or using the generic tags is they lead to less than satisfying Overview tabs.

Hi Rich, can we define our equipment tags, property tags, or both?

You can define any type of tag.

The following shows examples for Locations, Equipment, Points, and Properties:

version: 1


tags:
  - uid: Location_Home
    label: Home
    description: A house or home.
    synonyms:
      - House
      - Building

  - uid: Location_Indoor_Room_LivingRoom_Den
    label: Den
    description: A living room with a TV.
    synonyms:
      - Game room
      - TV room


  - uid: Location_Indoor_Room_LivingRoom_FrontRoom
    label: Front Room
    description: The front room of the house where guests are received.
    synonyms:
      - Sitting room
      - Music room

  - uid: Location_Indoor_Room_UtilityRoom
    label: Utility Room
    description: Room with HVAC, hot water heaters, laundry, etc.

  - uid: Equipment_Service
    label: Service
    description: A software service that provides something relevant to the home automation.
    synonyms:
      - Software
      - API
      - Daemon

  - uid: Equipment_MediaPlayer
    label: Media Player
    description: A device that can play media.
    synonyms:
      - Player
      - Speaker
      - Display

  - uid: Equipment_Occupancy
    label: Occupancy
    description: An equipment that monitors occupancy in a space.

  - uid: Point_BatteryProperty
    label: Battery Property
    description: A property of a battery.

  - uid: Property_Availability
    label: Availability
    description: Indicates the availability of a service or device.
    synonyms:
      - Online status
      - Offline status
      - Online/Offline status

  - uid: Property_Radon
    label: Radon
    description: Having to do with radon gas.

  - uid: Property_VOC
    label: VOC
    description: Having to do with volitile organic chemicals.

  - uid: Property_Media
    label: Media
    description: Streaming audio or video.
    synonyms:
      - Video
      - Music
      - Stream

  - uid: Property_Cost
    label: Cost
    description: The amount of money something requires.

  - uid: Property_Cloudiness
    label: Cloudiness
    description: Having to do with clouds.

  - uid: Property_Weather
    label: Weather
    description: Having to do with weather conditions.

  - uid: Property_Time
    label: Time
    description: Having to do with time.

  - uid: Property_Image
    label: Image
    description: Consisting or related to an image, perhaps from a camera.

I put this in a file under $OH_CONF/tags though I think the file watcher will pick it up from anywhere in $OH_CONF. See the link I posted above for details but at a high level you tags can inherit from existing tags so, for example, my FrontRoom tag is a LivingRoom which is a Room which is an Indoor which is a Location.

Once defined they will generate cards as expected. The gray ones are the newly defined tags (I’ve not got around to customizing the cards yet):



And they show up in the list when selecting tags through the UI.

The only think missing is being able to define these through the UI in the first place.

In our house, the lights in the hallway on the 1st and 2nd floor are on the same circuit. So there’s only one thing for it, and thus one item. So now, these lights are only for the 1st floor, as far as the semantic model is concerned.

What would be the best approach to have it also linked to the 2nd floor? A dummy item and a rule to sendCommand from one switch item to the other and postUdate in the reverse direction?

To have it in both places you’d need two Items. But I think you can just link the Channel to both Items and not have to mess with rules or anything like that.

2 Likes

@mjcumming
Would you please also link the full list of available types and properties?

these is incomplete I assume:

I have in mind, that I found one full list somewhere on github and that I have saved it into my favs, but I’m not able to find it :frowning:
For a definitive guide it would be great to have it included.

Thank you!

Linked from a similar post: [OH3] Semantic Model setup via tags in configuration Items files

2 Likes

If anyone else has some idea or additional information on tagging, please include it here, and I will try and integrate it into the first post. Thank you!

1 Like