Unit conversion from hWh to kWh

Hi,

I’m trying to convert an item that is read out as hWh to kWh in the sitemap.
My configuration is as follows:

Items

Number:Energy MB_Charge_Today 					"Lading [%.2f hWh]"		{channel="modbus:data:battery-inverter:power:charge_today:number"}

Sitemap

Text item=MB_Charge_Today label="Lading [%.2f kWh]"

The problem is that hWh does not seem to be recognized, the item also appears as a unitless number in the log.
When changing the item unit to kWh, the unit appears in the log.
Any ideas?

That’s it. It’s not a unit recognised in the openHAB units of measurement library. (Nor by me - what do you think it means?)

kWh is Wh with the kilo prefix.
k(kilo)Wh → 1000Wh
h(hecto)Wh → 100Wh

The battery inverter puts out an integer value as 0.1kWh units, that’s why I need the conversion.

The units of measurements manual states it’s possible to combine metric prefixes with all units:

I’m also using a conversion of cV to V which is working fine.

Okay, never seen that used or wanted in real life.
Log a github issue on OH core for that.
I suppose h may be ambiguous, hecta- or hours-?

Meantime, let’s fix that.
What binding is this data comng from, may we see channel config etc.?

Ok will do.
The data comes from the modbus binding:

Bridge modbus:tcp:battery-inverter  	
{
Bridge poller power						[start=33161, length=20, refresh=4000, type="input"] {
		Thing data charge_today			[readStart="33163", readValueType="uint16"]
		Thing data charge_yesterday		[readStart="33164", readValueType="uint16"]
		Thing data discharge_today 		[readStart="33167", readValueType="uint16"]
		Thing data discharge_today 		[readStart="33168", readValueType="uint16"]
	}
}

I tried using a javascript transformation at first but the unit of the item doesn’t show up in the sitemap when doing this.
This is the setup that I used:

(function(i) {
    var nVal = parseFloat(i) / 10;
	return nVal.toFixed(2)
})(input)
Number:Energy MB_Charge_Today 					"Lading [JS(divideBy10.js):%.2f kWh]"		{channel="modbus:data:battery-inverter:power:charge_today:number"}
Text item=MB_Charge_Today

The sitemap then shows this:
image

I think you put it in the wrong place.
In the Item [presentation] means the Item state remains as it is, and the transform tries to work with real state - which will have units. Your JS won’t work with input string “9.8 kWh”.

As per Modbus binding docs, do it in your data Thing. At this point, it’s just an integer Modbus register.
Use the same little script, add Thing parameter readTransform="JS(divideBy10.js)"
Now the Modbus data Thing will output your value scaled in kWh, but just as a numeric (units don’t exist for Modbus).

That’s okay, openHAB will use what you have put in the Item [presentation] as a default unit.
Number:Energy MB_Charge_Today "Lading [%.2f kWh] ..."

1 Like

That’s working perfectly.
A lot cleaner to, doing the conversion at the thing level.
Thanks a lot for the help!

Recap on my current config:

divideBy10.js

(function(i) {
    var nVal = parseFloat(i) / 10;
	return nVal.toFixed(2)
})(input)

modbus.things

Bridge modbus:tcp:battery-inverter {
	Bridge poller battery-info				[start=33139, length=8, refresh=4000, type="input"] {
		Thing data charge_today				[readStart="33163", readValueType="uint16", readTransform="JS(divideBy10.js)"]
	}
}

item

Number MB_Charge_Today 							"Lading [%.2f kWh]"		{channel="modbus:data:battery-inverter:power:charge_today:number"}

sitemap

Text item=MB_Charge_Today

An alternative approach to using readTransform= and [Item default units] is to use the binding’s modbus:gainOffset profile on the Item-channel link instead.

The advantage here is that you specify the unit before it arrives at the Item state, and do not rely on the [presentation] to assume a default.

1 Like