Number formatting

Ubuntu 19.10 running openhab.

I have a power meter setup, wich sends the total kwh used in MQTT wich is read and posted on my sitemap. Thing is, the numbers look like this 12227297 KWh and i would really like to format it into something more readable, like 12.23MWh.

I know i can adjust the number of decimals, but formatting this i am completely unsure of how to go about. Did not find much on google to help me out either, so i hope someone here can assist me :slight_smile:


I had a similar situation with an energy monitor.

In the end I created another Item and used a rule to do the math and post the new valve into the new Item.

It works quite well, as now I can show

kWh and Wh in the same UI.


do you have the chance to convert before sending to MQTT?

To process an Item state value for display, without altering the state itself,use a transform in Item or sitemap label format (the [part]).
A transformation can multiply, round etc. as you wish.

Text item=AMS_Malerstand label=“Målerstand [%.0f KWh]”

This is how my item looks now, wich works. But again i would like to divide the number by 1000 to get Mwh instead. Im not sure i quite understand how to apply a transformation here.

How is your item defined? As a UoM (Unit of Measurement) Energy? Then you can easy convert to Mwh just by using [%.0f MWh] in your sitemap.

This thread is about using transforms in [labels] to modify displayed Item states.

Have been reading thru that thread for a while now, and others. And i cant get it to work.
This is where i am at:

Text item=AMS_Malerstand “Målerstand [JS(kwh-to-mwh.js):%.2f]”

JS file:

(function(i) {
return parseFloat(i) / 1000;

So, what happens, or doesn’t happen? What does openhab.log have to say about it, if anything? Have you installed the JS transformation service? What is your Item type?

Get this error, js transformation service is installed yes.
2020-02-03 09:10:40.372 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘default.sitemap’ has errors, therefore ignoring it: [24,27]: extraneous input ‘“Målerstand [JS(kwh-to-mwh.js):%.2f]”’ expecting ‘}’

Okay, something wrong with the sitemap, not the transform (yet).

The å makes me think of character encoding, perhaps.
Or maybe the wrong kind of quotemarks, cannot tell without use of code fences in posts.

This is how it looks in my sitemap, even changing out the å. Made no difference.
Error message is:

==> /var/log/openhab2/openhab.log <==
2020-02-03 11:54:44.118 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configu ration model ‘default.sitemap’ has errors, therefore ignoring it: [100,27]: mism atched input ‘“Maaalerstand [JS(kwh-to-mwh.js):%.2f]”’ expecting ‘}’
[103,1]: extraneous input ‘}’ expecting EOF

Frame label="AMS" {
Text item=AMS_Effekt label="Watt [%.1f W]"
Text item=AMS_Volt label="Volt [%.1f V]"
Text item=AMS_Strom label="Ampere [%.2f A]"
Text item=AMS_Malerstand "Malerstand [JS(kwh-to-mwh.js):%.2f]"
Webview url="/static/HideHeader.html" icon="none"

Compare to

Unless that’s a copy-paste error, there’s still a strange character in there

I just tried changing between a few ways of writing it, copy paste error indeed. But what are u referring to when u say strange character? the quotation marks appear that way when i paste it here for some reason, they are " in the sitemap

What I mean is that you showed us an error with Maaalerstand in it and a sitemap line with Malerstand. Obviously,not the same. We can only work on what you tell us when you say “I have this, and get that error”.

Take the whole line out; does your sitemap still have an error?
If not, add it back piece by piece.
If it does, it’s probably a layout problem about { } braces.

Right, that was just a copy paste error on my end. The error message was the same as Malerstand . So that can be ignored.

If i take the whole line out, i have no errors.
With it in i get this:

2020-02-03 13:49:09.255 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘default.sitemap’ has errors, therefore ignoring it: [100,27]: mismatched input ‘“Malerstand [JS(kwh-to-mwh.js):%.2f]”’ expecting ‘}’
[103,1]: extraneous input ‘}’ expecting EOF

I do think you might be right about a layout problem.
Therefore, here is the entire sitemap. I cant spot the error, but maybe you can :slight_smile:

sitemap default label="MustachePalace" {

Frame label="" {
    Text item=Date icon="calendar"
    Text item=Time  icon="time"
Frame label="" {
Switch item=gLights label="Alle lys"
Text item=OpenHabServer_Cpu_Load label="Server Stats" {
	Text item=OpenHabServer_Network_IPAddress label="IP [%s]"
	Text item=OpenHabServer_Cpu_Load label="CPU Bruk [%s]"
	Text item=OpenHabServer_Storage_Available label="Lagringsplass [%sMB]"
	Text item=OpenHabServer_Memory_Available label="Ledig Minne [%sMB]"
	Text item=OpenHabServer_Memory_Total label="Totalt Minne [%sMB]"
	Text item=SpeedtestResultDown
	Text item=SpeedtestResultUp
	Text item=SpeedtestResultPing
	Text item=SpeedtestResultDate
	Text item=SpeedtestRunning label="Speedtest [%s]" visibility=[SpeedtestRunning != "-"]
	Switch item=SpeedtestRerun mappings=[ON="Start"]
Frame label="Lys" {
	Switch item=TrippelLampe_onoff label="Stå Lampe"
	Slider item=TrippelLampe1_Brightness label="Dimmer"
    Switch item=SofaLys_OnOff1 label="Sofakrok"
	Slider item=SofaHjoRne1_Brightness label="Dimmer"
	Switch item=MasterBedroom_onoff label="Master Bedroom"
	Slider item=MasterBedroom_Dimmer label="Dimmer"
	Switch item=Kontor_OnOff label="Kontor"
	Slider item=KontorTaklys_Brightness label="Dimmer"
	Switch item=Lys_Bod label="Bod"
Frame label="Utelys" {
    Switch item=gLightsOutside label="Alle utelys"
	Switch item=Utelys_Bod label="Bod"
	Switch item=UtelysHovedinngang1_onoff label="Inngangsdør" icon="switch"
	Slider item=UtelysHovedinngang1_Brightness label="Inngangsdør" icon="LightBulb"
	Switch item=UtelysMellomHus_onoff label="Mellom Hus" icon="switch"
	Slider item=UtelysMellomHus_Dimmer label="Mellom Hus" icon="LightBulb"
Frame label="Vær og Vind" {
		Text item=localCurrentCondition
		Text item=localCurrentWindSpeed
		Text item=localCurrentRainVolume icon="rain"
		Text item=localCurrentSnowVolume icon="snow"
		Text item=localDailyForecastTomorrowCondition
Frame label="Temperatur" {
    Text item=Bod_Temp label="Bod [%.1f °C]" icon="temperature" {						     
		Image refresh=30000 url=""
	Text item=Outside_Temp label="Ute [%.1f °C]" icon="temperature" {
	    Image refresh=30000 url=""
	Text item=localDailyForecastTomorrowMinTemperature
	Text item=localDailyForecastTomorrowMaxTemperature
Frame label="TV" {
    Slider item=TV_Volume label="Volum"
	Switch item=TV_Mute label="Mute"
	Text item=TV_SourceName label="HDMI [%s]" icon="hdmi"
	Text item=TV_ProgramTitle label="Program: [%s]"
	Switch item=TV_Power label="Strømbryter"
	Switch item=TV_ArtMode label="Art Mode"
Frame label="Kontor Vegg" {
    Switch item=KontorVegg_OnOff label="Lysbryter"			
	Slider item=KontorVegg_Dimmer label="Dimmer"	
	Colorpicker item=KontorVegg_ColorTemperature label="Farge" icon="colorwheel"
Frame label="MagicMirror" {
    Switch item=MagicMirror_OnOff label="Lysbryter"
	Slider item=MagicMirror_Color label="Dimmer"
	Colorpicker item=MagicMirror_ColorTemperature label="Farge" icon="colorwheel"
Frame label="Bod" {
	Switch item=Bod_Relay label="Varmeovn"
	Text item=DoorSensor_Bod label="Dør [MAP(]" icon="Door"
Frame label="Pi Hole"{
	Switch item=PiHole_Disable label="Disable Pi-Hole"
	Text item=PiHole_DomainsBeingBlocked
	Text item=PiHole_AdsBlockedToday
		Text item=PiHole_Disable label="Pi-Hole Stats" {
		Text item=PiHole_DnsQueriesToday
		Text item=PiHole_AdsPercentageToday
		Text item=PiHole_UniqueDomains
		Text item=PiHole_QueriesForwarded
		Text item=PiHole_QueriesCached
		Text item=PiHole_ClientsEverSeen
		Text item=PiHole_UniqueClients
		Text item=PiHole_DnsQueriesAllTypes
Frame label="AMS" {
Text item=AMS_Effekt label="Watt [%.1f W]"
Text item=AMS_Volt label="Volt [%.1f V]"
Text item=AMS_Strom label="Ampere [%.2f A]"
Text item=AMS_Malerstand "Malerstand [JS(kwh-to-mwh.js):%.2f]"
Webview url="/static/HideHeader.html" icon="none"

If you can’t spot any false character, you might try to completely retype the whole line (retype not copy and paste!) . Such could get rid of any hidden/unseen character.

You forgot the label= part

Correct, i swear sometimes i think i am going blind by looking at this. That was indeed the problem, and thank you for pointing it out. :slight_smile:

Me too