Note: this plugin has grown significantly in scope for version 1.0.0. It has previously been known as 'Temperature Scale Adapters Plugin' or 'Scale Adapters Plugin' or similar, but I've renamed it to simply 'Adapters' to reflect its broader capabilities.
---
This plugin allows you to add thin "adapters" on top of your devices to perform all kinds of mathematical or formatting transformations.
For example, suppose you have devices that report temperatures in Fahrenheit but you prefer Celsius, or the other way around. This plugin can generate "device adapters" that "wrap" around your native device and convert the temperature to the scale you prefer.
I have a Aeon Labs Multisensor in my attic that only reports temperature in Fahrenheit. That's slightly a bummer because I'm trying to learn Celsius and I'd prefer all of my temperature sensors to report that way. Using this plugin, I create a fake "adapter" device that "wraps" the Aeon Labs sensor and converts incoming temperature reports from F to C.
Here is a screenshot snippet showing this example; you can see the "native" Aeon Labs device (reporting temperature in Fahrenheit) as well as the "adapter" fake device (reporting the converted temperature in Celsius):
This plugin lets you do temperature, power, or length conversions using predefined settings, or you can specify your own mathematical transformation and formatting to produce almost anything you can imagine.
Four device types are provided:
- Predefined Scale Adapters. These allow simple conversions for devices that report temperature, length, or power. This version adds the capability to choose the precision (number of decimal places) shown, between zero and three.
- Custom Linear Adapter. This device (thanks kw123 for the inspiration) lets you specify an arbitrary multiplier, offset, and output format to essentially build your own adapter. For example, if the plugin didn't have temperature conversion already built in, then you could convert from Celsius to Fahrenheit by specifying multiplier=1.8 and offset=32 (since the conversion formula is F = 1.8 * C + 32). You can also write your own formatting string (using new-style Python syntax), which lets you choose the number of decimal places, add spaces between the value and the units, or whatever you may want to do.
- Custom Formula Adapter. This allows you to specify an arbitrary mathematical formula for the conversion, plus a format specifier. You could do the above temperature conversion example with a formula value of "1.8 * x + 32". You can also use exponents (with "**") , parentheses, and even simple if-else expressions, e.g. "x ** (1/2) if x >= 0 else (-1 * x) ** (1/2)".
- Custom Formula Adapter, String value. This is the same as the above, but with a string output rather than numeric. This lets you do something like "'on' if (x > 0) else 'off'".
The plugin is available in GitHub here.
Let me know if you give it a shot, especially if you have any issues with it. I'm not sure if this is a completely niche plugin or if it will find broader interest.
Cheers!
----
UPDATE 2016-06-22, version 1.0.0:
* New 'precision' attribute on predefined adapters
* New 'linear' adapter type that allows you to specify a custom multiplier, offset, and format
* New 'custom' adapters that allow arbitrary formulas (using Python 'simpleeval' library) and format strings
UPDATE 2016-04-04:
* Now supports rescaling units of length (e.g. feet, meters) and power (e.g. kilowatts, horsepower)