Skip to content

Units Of Measurement ​

To express measured values in a scientific correct unit the framework supports units of measurement. By using quantified decimal values in state updates and commands, the framework is able to automatically convert values to a desired unit which may be defined by the system locale or on a per-use-basis.

QuantityType ​

Bindings use the QuantityType to post updates of sensor data with a quantifying unit. This way the framework and/or the user is able to convert the quantified value to other matching units:

A weather binding which reads temperature values in °C would use the QuantityType to indicate the unit as °C. The framework is then able to convert the values to either °F or Kelvin according to the configuration of the system. The default conversion the framework will use is locale-based: The framework tries to convert a QuantityType to the default unit of the configured locale. This is the imperial system for the United States (locale US) and Liberia (language tag "en-LR"). The metric system with SI units is used for the rest of the world. This conversion will convert the given QuantityType into a default unit for the specific dimension of the type. This is:

Dimensiondefault unit metricdefault unit imperial
AccelerationMeter per square second (m/s²)Meter per square second (m/s²)
Amount of SubstanceMole (mol)Mole (mol)
AngleDegree (°)Degree (°)
AreaSquare Meter (m²)Square foot (ft²)
Areal DensityDobson unit (DU)Dobson unit (DU)
Catalytic ActivityKatal (kat)Katal (kat)
CurrencyUser Defined Currency (DEF)User Defined Currency (DEF)
Data AmountByte (B)Byte (B)
Data Transfer RateMegabit per second (Mbit/s)Megabit per second (Mbit/s)
DensityKilogram per cubic meter (kg/m³)Kilogram per cubic meter (kg/m³)
DimensionlessAbstract unit one (one)Abstract unit one (one)
Electric CapacitanceFarad (F)Farad (F)
Electric ChargeCoulomb (C)Coulomb (C)
Electric ConductanceSiemens (S)Siemens (S)
Electric ConductivitySiemens per meter (S/m)Siemens per meter (S/m)
Electric CurrentAmpere (A)Ampere (A)
Electric InductanceHenry (H)Henry (H)
Electric PotentialVolt (V)Volt (V)
Electric ResistanceOhm (Ω)Ohm (Ω)
Emission IntensityGram per kilowatt hour (g/kWh)Gram per kilowatt hour (g/kWh)
EnergyKilowatt hour (kWh)Kilowatt hour (kWh)
EnergyPricePrice per kilowatt hour (DEF/kWh)Price per kilowatt hour (DEF/kWh)
ForceNewton (N)Newton (N)
FrequencyHertz (Hz)Hertz (Hz)
IlluminanceLux (lx)Lux (lx)
IntensityIrradiance (W/m²)Irradiance (W/m²)
LengthMeter (m)Inch (in)
Luminous FluxLumen (lm)Lumen (lm)
Luminous IntensityCandela (cd)Candela (cd)
Magnetic FluxWeber (Wb)Weber (Wb)
Magnetic Flux DensityTesla (T)Tesla (T)
MassKilogram (kg)Pound (lb)
PowerWatt (W)Watt (W)
PressureHectopascal (hPa)Inch of mercury (inHg)
Radiant ExposureJoule per square meter (J/m²)Joule per square meter (J/m²)
Radiation Absorbed DoseGray (Gy)Gray (Gy)
Radiation Effective DoseSievert (Sv)Sievert (Sv)
RadioactivityBecquerel (Bq)Becquerel (Bq)
Solid AngleSteradian (sr)Steradian (sr)
SpeedKilometers per hour (km/h)Miles per hour (mph)
TemperatureCelsius (°C)Fahrenheit (°F)
TimeSeconds (s)Seconds (s)
VolumeCubic Meter (m³)US Gallon (gal)
VolumePricePrice per litre (DEF/l)Price per litre (DEF/l)
Volumetric Flow RateLiter per minute (l/min)US Gallon per minute (gal/min)

In some cases the system default unit may not be the most useful unit for a given quantity. For measuring precipitation km/h would be a quite uncommon unit, while mm/h would be the expected unit. You can set the unit metadata to a different unit to change the item's unit:

dsl
Number:Speed "Rainfall" { unit="mm/h" }

This unit is then also used for persistence. Setting the unit metadata to kW would persist 5.0 for a value of 5.0 kW (while 5000 would be persisted without because the system default is W). Attention: Changing the unit of an item may corrupt your already persisted data, no automatic conversion takes place.

The unit of the item is independent of the state description. The state description is used for display purposes only, it can contain any compatible unit and will not affect what is persisted or used in events.

Number item with dimension and DecimalType ​

A DecimalType state update or command to a Number item that contains a dimension is considered to have the item's unit. So updating a Number:Length item with 5.0 will set the item's state to 5 m or 5 in. (depending on your locale). In case you set a different unit (see above, e.g. km) that is taken and the item will set it's state to 5 km.

Number item without dimension and QuantityType ​

A QuantityType state update or command to a non-dimensional Number item will result in a state update that consists of the numeric part only. So updating a Number item with 5.0 kW will set the item's state to 5.0, updating the same item with 5.0 W will also set the item's state to 5.0. Linking dimension channels to non-dimensional items is therefore discouraged and will not be permitted in future versions of openHAB.

Defining ChannelTypes ​

In order to match NumberItems and Channels and define a default state description with unit placeholder the Channel also has to provide an Item type which includes the dimension information:

xml
<channel-type id="temperature">
    <item-type>Number:Temperature</item-type>
    <label>Temperature</label>
    <description>Current temperature</description>
    <state readOnly="true" pattern="%.1f %unit%" />
</channel-type>

The state description pattern "%.1f %unit%" describes the value format as floating point with one decimal place and also the special placeholder for the unit.

Implementing UoM ​

When creating QuantityType states the framework offers some useful packages and classes: The org.openhab.core.library.unit package contains the classes SIUnits, ImperialUnits and Units which provide units unique to either of the measurement systems and common units used in both systems.

The MetricPrefix class provides prefixes like MILLI, CENTI, HECTO, etc. and the BinaryPrefix class provides prefixes like KIBI, MEBI, etc. both of which are wrappers to create derived units.

The org.openhab.core.library.dimension and javax.measure.quantity packages provide interfaces which are used to type the generic QuantityType and units.

List of Units ​

All units which are currently supported by default are listed in the tables below.

Imperial (base unit symbols) ​

TypeUnitSymbol
DensityGrain per cubic footgr/ft³
LengthInchin
LengthFootft
LengthYardyd
LengthChainch
LengthFurlongfur
LengthMilemi
LengthLeaguelea
MassGraingr
PressureInch of MercuryinHg
PressurePound per square inchpsi
SpeedMiles per Hourmph
TemperatureFahrenheit°F
VolumeGallon (US)gal
VolumetricFlowRateGallon (US) per minutegal/min

SI (base unit symbols) ​

TypeUnitSymbol
AccelerationMetre per Second squaredm/s²
AccelerationStandard Gravityɡₙ
AmountOfSubstanceMolemol
AmountOfSubstanceDeutscher Härtegrad°dH
AngleRadianrad
AngleDegree°
AngleMinute Angle'
AngleSecond Angle''
AreaSquare Metrem²
ArealDensityDobson UnitDU
CatalyticActivityKatalkat
DataAmountBitbit
DataAmountByteB
DataAmountOcteto
DataTransferRateBit per Secondbit/s
DensityGram per cubic Metreg/m³
DimensionlessPercent%
DimensionlessParts per Millionppm
DimensionlessDecibeldB
ElectricPotentialVoltV
ElectricCapacitanceFaradF
ElectricChargeCoulombC
ElectricChargeAmpere HourAh
ElectricConductanceSiemensS
ElectricConductivitySiemens per MetreS/m
ElectricCurrentAmpereA
ElectricInductanceHenryH
ElectricResistanceOhmΩ
EnergyJouleJ
EnergyWatt SecondWs
EnergyWatt HourWh
EnergyVolt-Ampere HourVAh
EnergyVolt-Ampere Reactive Hourvarh
EnergyCaloriecal
ForceNewtonN
FrequencyHertzHz
FrequencyRevolutions per minuterpm
IlluminanceLuxlx
IntensityIrradianceW/m²
IntensityMicrowatt per square CentimeterµW/cm²
LengthMetrem
LuminousFluxLumenlm
LuminousIntensityCandelacd
MagneticFluxWeberWb
MagneticFluxDensityTeslaT
MassGramg
PowerWattW
PowerVolt-AmpereVA
PowerVolt-Ampere Reactivevar
PowerDecibel-MilliwattsdBm
PressurePascalPa
PressureHectopascalhPa
PressureMillimetre of MercurymmHg
PressureBarbar
RadioactivityBecquerelBq
RadiantExposureJoule per square meterJ/m²
RadiationDoseAbsorbedGrayGy
RadiationDoseEffectiveSievertSv
RadiationDoseRateSievert per HourSv/h
RadiationSpecificActivityCurieCi
SolidAngleSteradiansr
SpeedMetre per Secondm/s
SpeedKnotkn
TemperatureKelvinK
TemperatureCelsius°C
Temperature^Mired or MirekMK⁻¹
TimeSeconds
TimeMinutemin
TimeHourh
TimeDayd
TimeWeekweek
TimeYeary
VolumeLitrel
VolumeCubic Metrem³
VolumetricFlowRateLitre per Minutel/min
VolumetricFlowRateCubic Metre per Secondm³/s
VolumetricFlowRateCubic Metre per Minutem³/min
VolumetricFlowRateCubic Metre per Hourm³/h
VolumetricFlowRateCubic Metre per Daym³/d

^: This unit is used to indicate the color temperature of lights. The name 'mired' is a colloquial name for 'micro reciprocal degree'. The official SI unit symbol is 'MK⁻¹', but the alias unit symbol 'mired' is also permitted. The unit is also known as 'micro reciprocal Kelvin', so a second alias unit symbol 'mirek' is permitted too. Technically, mireds are the reciprocal of Temperature, but QuantityType and NumberItem will transparently convert between mireds and Kelvin. This technicality might be a problem if you are using mireds in rules. Be sure to use the toInvertibleUnit method on QuantityType, rather than toUnit.

Currency ​

TypeUnitSymbol
CurrencyUser Defined CurrencyDEF
EnergyPricePrice per kilowatt hourDEF/kWh
VolumePricePrice per litreDEF/l
VolumePricePrice per cubic meterDEF/m³
VolumePricePrice per gallon (US)DEF/gal

Currently the support for currencies is pretty basic and just one currency is supported. In the future there is a plan to support additional providers with connections to exchanges where values can be converted between currencies using the current exchange rates.

This means that you have to configure a provider to start. Otherwise your Items will just appear to have unit DEF.

There are two providers supported right now. The FixedCurrencyProvider will let you just define what currency you want to use everywhere. The LocaleBasedCurrencyProvider will use your regional settings to determine the default currency. The units follow ISO 4217 three letter designations (e.g. USD, EUR, etc.).

Metric Prefixes ​

NameSymbolFactor
YottaY10²⁴
ZettaZ10²¹
ExaE10¹⁸
PetaP10¹⁵
TeraT10¹²
GigaG10⁹
MegaM10⁶
Kilok10³
Hectoh10²
Decada10
Decid10⁻¹
Centic10⁻²
Millim10⁻³
Microµ10⁻⁶
Nanon10⁻⁹
Picop10⁻¹²
Femtof10⁻¹⁵
Attoa10⁻¹⁸
Zeptoz10⁻²¹
Yoctoy10⁻²⁴

Binary Prefixes ​

NameSymbolFactor
Kibiki2¹⁰
MebiMi2²⁰
GibiGi2³⁰
TebiTi2⁴⁰
PebiPi2⁵⁰
ExbiEi2⁶⁰
ZebiZi2⁷⁰
YobiYi2⁸⁰

To use the prefixes simply add the prefix to the unit symbol - for example:

  • milliAmpere - mA
  • centiMetre - cm
  • kiloWatt - kW
  • KibiByte - kiB