OH3: Item state UNDEF -> convert to zero

  • openHAB version: 3.1.0 M2

An item providing AC power (Watts) gets state “UNDEF” when the equipment goes into idle mode.
I would like the item state to remain on 0W (zero Watts) in such case.

I’d like to avoid writing a rule for that - especially since I’d need another item to receive the final state information.

Can this be done via the item’s metadata or in the channel the item is linked to?

Here is the specific case:

2021-03-06 17:55:51.926 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘KostalWechselrichter_ACPower’ changed from 0 W to UNDEF


Hope someone can help.

First be clear about whether you want this to amend the Item actual state,or merely prettify the display of UNDEF as “zero” instead.

The basic approach would be to use a transformation, probably a JS javascript, to examine state “incoming”, replace UNDEF with “0.0 W”, and pass everything else to “output”.
You will need to take care around the state being a quantity type, coming with units (W).

You may be able to apply this transform at the channel, depending on the mystery binding, or using a profile applied to the channel-Item link.
Both those methods will affect Item state.
Or use the transform in Item state presentation-pattern metadata for prettified display only.


I’d like to understand both approaches…

I was successful with…

Eventually I managed to create the transformation file (replace_UNDEF_0.js)

(function(i) {
    if (i = "UNDEF") return 0;
    return parseFloat(i);

However, I failed to do …

In the items metadata I currently set the following:

Where an how can I do this when the pattern is already set?
(it says “pattern” or “transformation”)

You need to edit the pattern. Your transformation scripts will need to take account that “incoming” data migh be of the form “10.2 W”.


is of the following format

"state": "19.0"

when the equipment is sending measurement data.

Wouldn’t it be logical to use the same script as for the channel-item link also in the items metadata?


(function(i) {
    if (i = "UNDEF") return 0;
    return parseFloat(i);

I was trying with:

JS(replace_UNDEF_0.js):%.0f W



just for running the script … but didn’t work.

Whatever I do, the string “UNDEF” remains as items state…

What is to be entered to the “Pattern” field in the items metadata to run this script?
… and add the “%.0f W” ?

Thank you!

Yes, it is supposed to.

Using a transformation in an Items pattern is expressly for that purpose.
Showing, displaying, some transformed version of the state without changing the Item state in any way.

You can’t parseFloat an input like “10 W”
It’s a Number:Power type Item,its state has units, the units are part of the state, that’s what gets passed to your transformation.
So far as the transformation used in an Item pattern goes, the ‘incoming’/‘input’ is the Item state. How and where and why you set that state is irrelevant.


Let me get back one step.

I understand that this is for prettified display only.

Let’s assume I had a javascript with the following filename stored in /transform:


… and let’s ignore the content of the script for a moment.

What is to be entered to the “Pattern” field in the items metadata to:

  1. just run this script ?
  2. to also add “%.0f W” for changing the number of decimals displayed?

Once this is clarified, I can work on the script in case it requires some further adjustment.

Thank you for your patience!

It’s an Item property, so that depends how you define your Items.
If from xxx.items file, the old fashioned way (sticking it in [ ] in the label) is documented-
Items | openHAB.
If you use the UI, you do whatever you do to add/edit metadata for state description / pattern. This will be of the form JS(replace_x_y.js):%s

You can’t “also”. When a transform is used, it returns a string. You can’t format a string with decimal formatter %.0f
About the only useful choice to use with a transform here is format %s, for string.

You could still add on string “constants” to that, so “%s W” should work to append space-W to the transform output.

But to format decimal points, you need to do that in your own transform script before returning the string.