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:
Event:
2021-03-06 17:55:51.926 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘KostalWechselrichter_ACPower’ changed from 0 W to UNDEF
Ye-es.
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.
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.