First be clear about whether you want this to amend the Item actual state,or merely prettify the display of UNDEF as “zero” instead.
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.
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.
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.