However I would like to display kWh (divide Wh by 1000), as the Wh number would be quite large.
I tried Incoming Value Transformations in Advanced Channel configuration (I added “/1000”), which leads to the right value, but I get a “EROOR:Config” on the Gerneric MQTT Thing.
“/1000” isn’t a transformation. You won’t see it listed under “Data Transformation” as an add-on to install.
You need to use an actually supported transformation. The only transformation that can handle changing a numeric value is the JS transformation. JavaScript - Transformation Services | openHAB
It does but it’s kind of broken right now when you want to convert from one unit to a different one. The problem is no matter what you put into the Channel, the State Description will override it so instead of converting the 1000 Wh to 1 kWh you’ll end up with 1000 kWh.
Because of this bug the only way to actually change the units of the value coming from MQTT is through a JS Transformation.
Is your Item of Number type? Then you must pass it a number from MQTT, divided by 1000. You can add text “kWh” in your Item pattern metadata for display.
Is your Item of Number:Energy type? Then you must pass it a value with units. You can give Item a default unit text “kWh” in your Item pattern metadata. You can transform the number coming from MQTT by appending the text " Wh" to it. No need to divide, if you use the proper units then openHAB takes care of conversions.
I didn’t either. That’s cool! I should follow my own advice and reread the docs periodically. NOTE: that’s a new feature in OH 3.2 and not available in OH 3.1. And I don’t know when that feature was added during OH 3.2 development. So @Daniel_Hermann1, which version of OH are you running?
It must be a recent enough 3.2 if it’s not exploding on the expression and it appears to be doing the math at least and removing the decimal places. And I’m guessing you have the syntax right there as well.
So what is the type of the Item now and what is the State Description on that Item? Do you want to just show that Item as kWh but let it remain stored as Wh? Do you want it stored as kWh too? This has implications to things like charting and the like.
Thanks to javascript “sloppines” with types, you can just have expressions like input/100 + " Wh" as the transformation – and as a result you have number-with-units coming out, suitable for UoM-aware Number item. No need to even to convert the string input to javascript number!
Of course if you want to handle UNDEFs etc, it might get more tricky…
That will just pass the UNDEF/NULL through unchanged. You’ll want to do that anyway if there is any chance that the Channel will return anything that cannot be parsed into a Number.
But now that I think about it, an MQTT Channel is unlikely to ever return UNDEF or NULL anyway so that check would probably better be something like
(isNaN(input)) ? input : input/100 + " Wh"
That will just pass through any value that isn’t a number and do the math when the value is a number.
Edit: fixed the first example which was totally wrong.
Thanks for the correction regarding the whitespace, will correct the above post for posterity.
Nice trick with isNaN, once again good example of javascript “sloppiness” when it comes to typing, examples of “nan” according to isNaN include "foobar", "NULL" Unlike I expected, I thought only javascript literal NaN or 0.0/0.0 would be considered NaN.
So i Tried (isNaN(input)) ? input : input/1000 + " kWh" as Transformation, leaving the field “Unit of Measurement” as kWh. Unfortunately I get 645.898 kWh as an answer (or just 645.898 if i leave the Unit of Measurement, even thogh I have the + "kWh " in the JS command).
The right answer would be 0.65… kWh.
In the Log I get the message
2021-12-17 09:02:49.545 [WARN ] [t.generic.ChannelStateTransformation] - Transformation service (ISNAN(INPUT)) ? INPUT for pattern input/1000 + " kWh" not found!
.