Auto scale units

image

Allows one to set a lower unit, upper unit, and threshold above which the transformation returns the state in the upper unit.

There are three arguments:

  • lowerUnit: unit to use when the value is below the threshold
  • upperUnit: unit to use when the value is above the threshold
  • threshold: value above which the upperUnit is used

For example, if the lowerUnit is W and the upperUnit is kW and the threshold is 1 kW, an input of 750 W will be returned unchanged. An input of 0.75 kW will be returned as 750 W. An input of 1025 W will be returned as 1.025 kW.

See below for an example on how to call the transformation.

Changelog

Version 0.1

  • initial release

Resources

{
  "uid": "config:js:ohrt_ScaleUnits",
  "label": "Scale Units",
  "type": "js",
  "configuration": {
    "function": "(function(data, lowUnit, highUnit, threshold) {\n  console.loggerName \u003d \u0027org.openhab.automation.rules_tools.ScaleUnits\u0027;\n  console.trace(\u0027ohrt_scaleUnit invoked with: \"\u0027 + data + \u0027\" \"\u0027 + lowUnit + \u0027\" \"\u0027 + highUnit + \u0027\" \"\u0027 + threshold + \u0027\"\u0027);\n  let rval \u003d Quantity(data).toUnit(lowUnit);\n  console.trace(\"To low unit result \" + rval);\n  if(rval.greaterThan(Quantity(threshold))) { \n    console.trace(\"Over threshold, converting...\")\n    rval \u003d rval.toUnit(highUnit);\n  }\n  console.debug(\"Before conversion \" + data + \" after conversion \" + rval);\n  return rval.toString();\n})(input, lowUnit, highUnit, threshold)"
  }
}

Calling the transformation with these arguments would look like:

JS(config:js:ohrt_ScaleUnits?lowUnit="W"&highUnit="kW"&threshold="1000 W")
3 Likes