MQTT: Automatic quantitytype conversion (kWh -> Wh) does not work

I use MQTT2 generic binding with a Sonoff POW, which provides todays and total energy consumption in kWh:

Type number : today "Energy today" [ stateTopic="sonoff3/tele/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Today", unit="kWh"]
Type number : total "Energy total" [ stateTopic="sonoff3/tele/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Total", unit="kWh"]

With the configuration unit="kWh" I try to tell the MQTT binding, that this is measured in kWh, not Wh, MWh or the like.
Since I use OpenHAB 2.5.6 (and the 2.5.6 mqtt binding), this should be supported.

Now I try to use different units in for both values via items configuration:

Number:Energy Lueftung_E  "Verbrauch heute [%.3f Wh]" <energy> {channel="mqtt:topic:luv:sonoff3:today"}
Number:Energy Lueftung_Et "Verbrauch gesamt [%.3f kWh]" <energy> {channel="mqtt:topic:luv:sonoff3:total"}

Please note, that the first line uses “Wh”, while the second uses “kWh”.
I expected that the first line should automatically convert from “kWh” (from the channel definition) to “Wh” (in the item definition), but only the unit was changed but not the value, so 0.163 kWh (from MQTT) is “converted” to 0.163 Wh instead of 163 Wh.

Changing the item definition from Wh to kWh shows correct data (0.163 kWh), but isn’t what I want to see, since such small values make much more sense to show in Wh instead of kWh.

Is there any other way to let OpenHAB handle this conversion job?
I expected to address this issue, but it still doesn’t work as I expected.


I think the underlying issue is that the MQTT number channel remains a number channel, not a true quantity type.

Hi Roland - I’m curious if you ever got this working. I’m attempting something similar and am very confused why the binding allows you to set the unit on a particular channel, but it doesn’t appear to actually use that setting.


Apparently it is only used for outbound commands in this binding at present.

If you either chain on another transformation in channel stateTransformation, or apply a profile to the link to Item, that adds text " kWh" you should be able to properly update a quantity type Item.

Thanks for the feedback. I ended up having the mqtt server publish the number with the unit label (i.e. “200 kW” and had mqtt channel type as a string. I then bound that to a Number:Power item and all seems to be good.