Very simple formula in OH items

Although I have already quite some experience with OH and item, rules and so on.

I can’t seem to find how I need to solve this rather benign problem.

I have a mqtt item that gives me a number (in cm) for a water level of a “well”

Now off course the level is not the number returned but the height of the well minus the number returned.

So if I get the 10cm from my distance sensor through mqtt I want to show 65-10 so 55.

I would think it is rather overkill to use a rule for this, because it is just very basic.

So I tried the following item line, which off course doesn’t work as expected:

Number itm_uber1_well_mqtt “Well level [65-%.1f cm]” (gC,Well_value) { mqtt="<[mymosquitto:1253:state:default]" }

This doesn’t output “55 cm” but instead shows me “65-10 cm”

How about using the Javascript transformation service for this: https://github.com/openhab/openhab/wiki/Transformations#java-script-transformation-service

That would be actually what I’m trying to avoid.

There is the solution of just writing a rule that on every change updates the value from x to 65-x which is about a simple as it gets.

But as time conversion and mapping is build in the item description I kinda hoped that very simple math conversions are too.

I think the best you can do is something like:

Number itm_uber1_well_mqtt "Well level [%.1f cm]" (gC,Well_value) { mqtt="<[mymosquitto:1253:state:JS(well.js)]" }

with a configurations/transform/well.js like:

(function(i) { return 65 - i; })(input)

It is exactly the situation that the transforms system is designed for! :wink: Writing a rule would be significantly more complex as you won’t be able to transform the data in place, you would need to write the new data to another item. The transformation will convert the data, so that your item value is the processed version.

I don’t think the item description can do any conversions outside of standard Java string formatting (http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html), so numerical conversions are probably out.

@watou’s solution above looks good and is nice and simple. I would go with that!

That’s the perfect solution, thx a lot.

@robconnolly I indeed found that explanation too about the java standard conversions.

I’m slowly starting to figure out the finer things of the openhab definitions.