Number Item Offset in state description?

Does anyone know of a way that I could offset a item in the state description metadata or if it’s even possible? I know I could just use 2 items and a rule just wanted to see if their was another way.

You could use a profile with transformation if this item is linked to a channel. The script transformation comes to mind which lets you do any calculation you desire.

So I already have the calculation in the rule dsl working. I’m struggling to understand how to use the new transformation menu in openhab4. I’ve never used transformations before. How would you use the script transformation in the metadata of the item?

Are you using text based items in .items file or UI?

With .items file

Number MyNumberItem { channel="xxxx" [profile="transform:DSL", toItemScript="| Double.valueOf(input) - 100" ] }

For example.

If it’s an item with dimension, then use QuantityType.valueOf

Ui configuration for pretty much everything is the only way to use the transformation properly through .items file?

Do you really only want to see the offset but not have that offset applied to the Item’s state? If so you can use a transform on the Item’s State Description.

If you want the Item’s actual state to include the offset, @JimT’s approach using a profile is the best one.

Many transformations require a separate configuration.

For example, MAP requires a table with key=value pairs that maps a given state to the desired value.
image

If you have a complicated script based transformation, you can define it there as well.

Note, this last transformation really isn’t necessary. It’s better to use a Number:Time Item and use the State Description Pattern to transform it to HH:MM:SS.

To use these go to the Link between an Item and a Channel, the Item’s State Description, or anywhere else a transformation is supported and if you cannot simply select it like so:

or enter the ID of the transform if selecting one isn’t possible (selecting the transform has not be implemented everywhere in MainUI yet):

image

If it’s a simple script transformation that fits on one line of code, you can enter the transformation directly on the line as shown in @JimT’s example.

Yes this is what I’m trying to do but the item is not linked to any channels so i don’t think profiles would work.
I want the number Item to show it’s value with +1 added to it
Item state displayed = item actual state + 1

@rlkoshak how can one do this using UI based item definition? I tried looking it up in the docs but can’t find it within 10 minutes of looking around. It seems that stateDescription metadata format doesn’t take transformations.
EDIT: I was wrong. stateDescription format does take the transformations, however, it seems that it would convert Numeric to String after doing a transformation. So any “formatting” or the numeric data needs to be done within the transformation itself.

Example (assuming it’s a Number:Power item):

DSL(| (QuantityType.valueOf(input) + 1|W).format("%.2f")):%s

For a plain Number item:

DSL(| String.format("%.2f", Double.parseDouble(input) + 1)):%s

Yes, that’s going to be the case as all transforms always return a String. So if you have additional formatting you’ll need to handle that in the transform script and not be able to use %.1f and the like in the label.

As for where it’s documented, that needs to be fix (one of the growing list of things to rework in the docs). For now the State Description Pattern follows the same rules as the Item Label: Items | openHAB. Further details about using each individual transformation can be found on that add-on’s page (e.g. look at the Map Transformation add-on’s readme page).

For SCRIPT add-ons, there should be a section in the add-on readme for that automation add-on that discusses transformations. (JavaScript Scripting - Automation | openHAB, JRuby Scripting - Automation | openHAB). Additional information can be found under Transformations | openHAB.

Unfortunately the syntax for using a transform varies from one place to the next which is frustraiting. You use DSL:| QuantityType.valueOf(input) + 1|W).format("%.2f") in the HTTP or MQTT binding, DSL(| QuantityType.valueOf(input) + 1|W).format("%.2f")) on an Item’s label or sitemap element’s label, and in a profile the selection/definition of DSL and the reference to the script are separated (even in the UI).

It’s kind of a mess but I don’t know what can be done about it.

Were I king, I’d eliminate transformations from the bindings entirely and just use the transform profile. Binding authors shouldn’t be required to implement something as core as transformations. But the ability to chain transforms is vital for some bindings and there is no way to chain profiles.