Hi guys,
I have an issue with my battery levels and it seems I cannot figure out how to make it right.
I have some Xiaomi Temperature, Humidity and Pressure Sensors and want to show the level of the Battery in Percentage (%) in my sitemap.
The value always gets shown with 2 Percentage signs %%.
Everything I tried so far resulted in wrong formatting or wrong values shown. I use OpenHab 4.2.0.
Could someone please point out what is wrong?
The battery level gets into OpenHab via the MQTT Binding. In MQTT the values are between 0 and 100; that’s why I divide the number /100 with a transformation.
The Thing Definition which includes the Battery Level is:
Thing topic XHTP01 "Xiaomi Temperature, Humidity and Pressure Sensor 01" {
Channels:
Type number : battery [ stateTopic="zigbee2mqtt/0x00158d00025d8b50", transformationPattern="JS:format_BatteryLevelToPercent.js" ]
}
My Item Definition for the Battery Level is:
Number:Dimensionless XHTP01_Battery "HTP Sensor #1 Battery [%s %%]" <battery> ["Measurement", "Energy"] { channel="mqtt:topic:MQTTBroker:XHTP01:battery" }
In my Sitemap:
Text item=XHTP01_Battery
The result looks like this:
If I change [%s %%] to [%s] I get:
How can I make it that it just shows “100%” with the correct Battery Icon?
Just for completeness, here is my format_BatteryLevelToPercent.js:
(function(i) {
var data = JSON.parse(i);
batterylevel = data.battery;
return parseFloat(batterylevel) / 100;
}) (input)





