Quantity

Quantity

Class allowing easy Units of Measurement/Quantity handling by wrapping the openHAB QuantityType.

Throws QuantityError when Quantity creation or operation failed due to wrong quantity or unit. Throws TypeError when wrong argument type is provided.

Members

dimension :string

Source:

Dimension of this Quantity, e.g. [L] for metres or [L]² for cubic-metres

Type:
  • string

float :number

Source:

Float (decimal number) value of this Quantity

Type:
  • number

int :number

Source:

Integer (non-decimal number) value of this Quantity

Type:
  • number

symbol :string|null

Source:

Unit symbol of this Quantity, e.g. m, or null if not available

Type:
  • string | null

unit :string|null

Source:

Unit of this Quantity, e.g. Metre, or null if not available

Type:
  • string | null

Methods

add(value) → {Quantity}

Source:

Add the given value to this Quantity.

Parameters:
Name Type Description
value Item | string | Quantity

Quantity-compatible Item, string consisting of amount and unit or a Quantity

Returns:

result as new Quantity

Type
Quantity

divide(value) → {Quantity}

Source:

Divide this Quantity by the given value.

Example
Quantity('20 W').divide(4); // is 5 W
Quantity('20 W').divide('4 W') // is 5
Parameters:
Name Type Description
value Item | number | string | Quantity

usually a number; may also be an Item which is either Quantity-compatible or holds a number, a string consisting of amount and unit or a Quantity, but be careful: 1 W / 5 W = 0.2 which might not be what you want

Returns:

result as new Quantity

Type
Quantity

equal(value) → {boolean}

Source:

Checks whether this Quantity is equal to the passed in value.

Parameters:
Name Type Description
value Item | string | Quantity

Quantity-compatible Item, string consisting of amount and unit or a Quantity

Returns:
Type
boolean

greaterThan(value) → {boolean}

Source:

Checks whether this Quantity is larger than the passed in value.

Parameters:
Name Type Description
value Item | string | Quantity

Quantity-compatible Item, string consisting of amount and unit or a Quantity

Returns:
Type
boolean

greaterThanOrEqual(value) → {boolean}

Source:

Checks whether this Quantity is larger than or equal to the passed in value.

Parameters:
Name Type Description
value Item | string | Quantity

Quantity-compatible Item, string consisting of amount and unit or a Quantity

Returns:
Type
boolean

lessThan(value) → {boolean}

Source:

Checks whether this Quantity is smaller than the passed in value.

Parameters:
Name Type Description
value Item | string | Quantity

Quantity-compatible Item, string consisting of amount and unit or a Quantity

Returns:
Type
boolean

lessThanOrEqual(value) → {boolean}

Source:

Checks whether this Quantity is smaller than or equal to the passed in value.

Parameters:
Name Type Description
value Item | string | Quantity

Quantity-compatible Item, string consisting of amount and unit or a Quantity

Returns:
Type
boolean

multiply(value) → {Quantity}

Source:

Multiply this Quantity by the given value.

Example
Quantity('20 W').multiply(4); // is 80 W
Quantity('20 W').multiply('4 W') // is 80 W^2
Parameters:
Name Type Description
value Item | number | string | Quantity

usually a number; may also be an Item which is either Quantity-compatible or holds a number, a string consisting of amount and unit or a Quantity, but be careful: 1 W * 5 W = 5 W^2 which might not be what you want

Returns:

result as new Quantity

Type
Quantity

subtract(value) → {Quantity}

Source:

Subtract the given value from this Quantity.

Parameters:
Name Type Description
value Item | string | Quantity

Quantity-compatible Item, string consisting of amount and unit or a Quantity

Returns:

result as new Quantity

Type
Quantity

toUnit(unit) → {Quantity|null}

Source:

Convert this Quantity to the given unit.

Parameters:
Name Type Description
unit string
Throws:

when unit cannot be parsed because it is invalid

Type
QuantityError
Returns:

a new Quantity with the given unit or null if conversion to this unit is not possible

Type
Quantity | null