Adapters Plugin

Posted on
Sun Mar 20, 2016 6:50 pm
dustysparkle offline
Posts: 54
Joined: Dec 14, 2015

Adapters Plugin

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):

Screen Shot 2016-03-16 at 4.25.07 PM.png
Toasty.
Screen Shot 2016-03-16 at 4.25.07 PM.png (32.15 KiB) Viewed 4810 times


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:

  1. 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.
  2. 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.
  3. 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)".
  4. 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)
Last edited by dustysparkle on Wed Jun 22, 2016 8:51 pm, edited 2 times in total.

Posted on
Sun Mar 20, 2016 7:46 pm
matt (support) offline
Site Admin
User avatar
Posts: 18918
Joined: Jan 27, 2003
Location: Texas

Re: Temperature Scale Adapters Plugin

This is a great idea and it looks like a solid implementation. It'll be super useful for some folks – thanks for sharing it!

Image

Posted on
Mon Mar 21, 2016 5:09 am
howartp offline
Posts: 3385
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Temperature Scale Adapters Plugin

You beat me to it :-(

Was going to start on exactly that plugin this weekend!


Sent from my iPhone using Tapatalk

Posted on
Mon Mar 21, 2016 9:05 am
durosity offline
User avatar
Posts: 3682
Joined: May 10, 2012
Location: Newcastle Upon Tyne, Ye Ol' England.

Re: Temperature Scale Adapters Plugin

howartp wrote:
You beat me to it :-(

Was going to start on exactly that plugin this weekend!


On well you can put that spare time towards something else.. Perhaps you drive up here and paint my garage floor for me? Oh.. Bring the paint too.


Sent from my iPad using Tapatalk

Computer says no.

Posted on
Mon Mar 21, 2016 9:41 am
howartp offline
Posts: 3385
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Temperature Scale Adapters Plugin

Sorry, the range on my leaf won't get me that far. ;-)

Dusty - have you considered expanding it to distances (m/km) and power (w/kw) etc?


Sent from my iPhone using Tapatalk

Posted on
Mon Mar 21, 2016 10:01 am
dustysparkle offline
Posts: 54
Joined: Dec 14, 2015

Re: Temperature Scale Adapters Plugin

howartp: I hadn't considered that, but it's an obviously good idea that would be rather straightforward to implement, I think. I'll give it a go.

Also, if you know of any esoteric scales (for temperature or otherwise), I can toss those in there, because it's super fun! :D I'm pretty sure no one actually needs temperature reported in Rankine, for example.

Posted on
Mon Mar 21, 2016 11:11 am
howartp offline
Posts: 3385
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Temperature Scale Adapters Plugin

Do, re, mi, fa, so, la, ti, do?


Sent from my iPhone using Tapatalk

Posted on
Mon Mar 21, 2016 12:39 pm
kw123 offline
User avatar
Posts: 6261
Joined: May 12, 2013
Location: Dallas, TX

Re: Temperature Scale Adapters Plugin

You could do a configurable scale, offset and format function so that you can do any linear conversion with free text / unit


Sent from my iPhone using Tapatalk

Posted on
Mon Mar 21, 2016 12:59 pm
kw123 offline
User avatar
Posts: 6261
Joined: May 12, 2013
Location: Dallas, TX

Re: Temperature Scale Adapters Plugin

Code: Select all
                   
                    ss, ssUI, unit = self.addmultOffsetUnit(data[input], props)
                    if dev.states[input] != ss:
                        dev.updateStateOnServer(input, ss)
                    if upState == input:
                        if dev.states["status"] != ssUI + unit:
                            dev.updateStateOnServer(u"status", ssUI + unit)

        except  Exception, e:
            self.myLog(255, u"in Line '%s' has error='%s'" % (sys.exc_traceback.tb_lineno, e))

        ####-----------------  updateINPUT ---------

    def addmultOffsetUnit(self, data, props):

        try:
            ui = float(data)

            if "resistorSensor" in props and props["resistorSensor"] == "1":
                feedVolt = float(props["feedVolt"])
                feedResistor = float(props["feedResistor"])
                ui = feedResistor / max(((feedVolt / max(ui, 0.1)) - 1.), 0.001)

            if "offset" in props and props["offset"] != "":
                ui = float(ui) + float(props["offset"])

            if "mult" in props and props["mult"] != "":
                ui = float(ui) * float(props["mult"])

            if "unit" in props and props["unit"] != "":
                unit = props["unit"]
            else:
                unit = ""


            if "format" in props and props["format"] != "":
                ui = props["format"] % ui
            else:
                ui = unicode(ui)
        except:
            ui=data
            unit=""
        return unicode(data), ui, unit

Code: Select all
           <Field id="label" type="label"> <Label>    define parameters for state column:</Label> </Field>
            <Field id="unit"   type="textfield"             defaultValue="[mV]">    <Label>unit:</Label></Field>
            <Field id="offset" type="textfield"             defaultValue="0">       <Label>add offset:</Label></Field>
            <Field id="mult"   type="textfield"             defaultValue="1">       <Label>multiply number by :</Label></Field>
            <Field id="format" type="textfield"             defaultValue="">        <Label>format number (eg %3.1f):</Label></Field>
            <Field id="infodispl" type="textfield" defaultValue="(v+off)*mult"  readonly="YES">


and in device you get the properties format offset mult from the user

Posted on
Tue Mar 29, 2016 8:11 pm
Bollar offline
Posts: 516
Joined: Aug 11, 2013

Re: Temperature Scale Adapters Plugin

Good work!

Insteon / Z-Wave / Bryant Evolution Connex /Tesla / Roomba / Elk M1 / SiteSage / Enphase Enlighten / NOAA Alerts

Posted on
Wed Mar 30, 2016 4:49 am
Shutter offline
Posts: 343
Joined: Mar 07, 2014
Location: London, UK

Re: Temperature Scale Adapters Plugin

Really like this plugin. Allows me to display my data in a constant and accurate manner. I'm using it to convert Celsius to Celsius. Why? Because my Netatmo outputs its data without a ºC suffix, which for consistency I like to use on my control page. Using this plugin fixes this.

Which is why I would like to suggest expanding this plugin for additional conversions and custom fields, so one can add things like % signs when needed. I'm sure one can use scripts to do a lot of this, but I for one don't know much about Python or AppleScript, and as such avoid using them only as a last resort.

Many thanks.

Posted on
Wed Mar 30, 2016 6:32 am
Bollar offline
Posts: 516
Joined: Aug 11, 2013

Re: Temperature Scale Adapters Plugin

My OCD suggests you add a space between the number and degrees to match Indigo's format.
Attachments
indigo F spacing.tiff
indigo F spacing.tiff (29.73 KiB) Viewed 4376 times

Insteon / Z-Wave / Bryant Evolution Connex /Tesla / Roomba / Elk M1 / SiteSage / Enphase Enlighten / NOAA Alerts

Posted on
Wed Mar 30, 2016 11:26 am
dustysparkle offline
Posts: 54
Joined: Dec 14, 2015

Re: Temperature Scale Adapters Plugin

Yikes, now that you point it out, the spacing difference is insufferable. I'll include that change in the next version.

BTW (all), I now have a working version that supports both temperature and distance measurements. As soon as I polish a little, I'll post.

Posted on
Wed Mar 30, 2016 12:00 pm
Albatros offline
Posts: 132
Joined: Feb 07, 2015

Re: Temperature Scale Adapters Plugin

Thanks very much for the plugin.

Not sure if this is a stupid question. But why isn't this a standard functionality in Indigo?

Posted on
Wed Mar 30, 2016 12:05 pm
matt (support) offline
Site Admin
User avatar
Posts: 18918
Joined: Jan 27, 2003
Location: Texas

Re: Temperature Scale Adapters Plugin

It definitely is on the feature request / ToDo list. Our implementation would be a bit different (not requiring additional devices) since we are not restricted to the plugin API, but this is a terrific solution for the time being.

Image

Who is online

Users browsing this forum: No registered users and 1 guest

cron