It is a general question regarding the transformation of values of things in items (here openhab and openhabian on raspi4, OH V.5.1.3)
If a thing has/creates a value e. g. in W and I want to convert it in kW, it is somtimes possible to just link a second item with kW (while the first item displays W) and sometimes with other things a second item to show kW (or MW or kWh or whatever) doesn’t work this way and I have to set up a rule for conversion. For me it’s just try and error.
But I want o understand the reason for this behaviour! Can someone help?
I’m pretty sure that there is no ‘sometimes’ but a difference in the channel, i.e. one channel has a unit set and the other doesn’t, resulting in the first Item being a QuantityType Item and the other a simple Number Item. And while QuantityType items are unit-aware the simply Number Items aren’t.
The Item needs the correct Dimension, e.g. “Energy (kWh)” and Unit, e.g. “kWh”.
Then you can add a State Description as Metadata, e.g. “%.2f MWh” and it should work.
A Number Item never has a dimension. If an update is sent to a Number Item, the unit is stripped off.
A Number:Dimension Item always has a unit. The unit is determined as follows:
a. if the Item doesn’t have a defined unit the system default unit for that type is assumed. See the docs for the details.
b. if the Item does have a defined unit the Item’s state will always be in that unit.
c. If an update without a unit is sent to the item, the Item’s unit will be assumed and tacked on to the value.
d. if an update with a unit different from the Item’s unit is sent to the item, the value will be converted to the Item’s unit.
The unit that the Item’s state is displayed as can be different from the unit stored in the Item through the state description pattern metadata. But this only affects display, not the actual state of the Item.
Given the rules above, you almost never need to have more than one Item to show a value in different units. And you almost never need a separate transformation nor a rule to transform between compatible units.
Am I too stupid to understand that? Here ist the code of an item definition that always shows W instead of kW. It looks equal to another one where it works as expected.
version: 1
items:
DC_Strom_30773_kW:
type: Number
dimension: Power
label: DC_Strom 30773 kW
icon: energy
unit: kW
tags:
Point
metadata:
stateDescription:
value: ’ ’
config:
pattern: ‘%.1 %unit%’
I think I got it! I forgot, that between THING and ITEM there ist the CHANNEL! And if the CHANNEL has no DIMENSION but only a NUMBER I have to change the number with a rule/transformation if I want to see i. e. kW instead of W. Correct?
It depends. If it’s MQTT or http or a few other bindings, you should be able to set the unit that the channel sends as updates in the channel config. Then OH will know the value is being reported as W and concert it to the kW since that’s what the Item’s metadata is set to use.
However, if the binding doesn’t support that, you’ll need a transformation profile. But all the profile really needs to do is tack on " W"v to the value and then the UoM will handle things from there.
Here I’m talking about http binding but the links deliver json code. So the thing is a string and I first have to parse the field with the value with a json transformation (JSONPATH:$.state) and then I need to parse the number out of the thing with a regex (.|(.\d).*) to get the clean value as number.
That supports setting the unit. There’s a property when you check “show advanced”.
HTTP binding also supports transformation chaining. The description under the state transformation tells you how:
Transformation pattern used when receiving values. Multiple transformations can be chained by listing each transformation on a separate line, or by concatenating them with “∩”.
So just chain the two together:
JSONPATH:$.state∩REGEX:.|(.\d).*
You might need to escape the |. And obviously both the jsonpath and regex add-ons must be installed. Then just set the unit if the channel to what unit the number is in and the unit on the Item to the unit you actually want.