Simplify Mqtt string temperature

I receive the temperature from a “simulated sensor” that publish the next message:

in/A_Thermostat_Room_Temperature/state 15,5 (it is a string)

I have a string item and it shows the number but the problem it that the number string is, for example, 23,75342 and I would like to show only 2 decimals.

Should I program a rule? How?

Looking into the Documentation would show an example with one digit, adhusting to two digits should be easy.

I’m not able to declare a text item in .item file. When I use [%.1f] with string item, it shows error

Assuming you don’t want to change the string stored in your Item, and only change the string displayed in your UI.
You could use a javascript transform in the label [ ] format part.
Your JS would need to convert string to number, format with two decimal places, and return the result for display.

In response to DM - no I don’t know exactly how to do that.

You need to write a little javascript to manipulate your string.
You need to install the transformation service if you haven’t already.
You call the js within your Item label in this way for display only

String myMysteryItem "My text label [JS(twopoints.js):%s]"

That says to pass the Item state as a string to your script in conf/transforms.

The script will be something like (untested)

(function(i) {
// i is the string value passed in
var d = parseFloat(i);
// get the string as a decimal
d = Math.round(d * 100);
d = d / 100;
// get to two decimal places 
return d;
// transforms always return that as a string
})(input)

I’m not sure if that works with comma as decimal separator instead of point

Do I have to include any library in .item or .js file ?

I tried this:

Item declaration (.item file):
String Temperature_GF_Living “Living Room [JS(twopoints.js):%s °C]” (GF_Living) [“Temperature”, “Measurement”] {channel=“mqtt:topic:LR_Temperature:LivingRoom_Temperature”}

Thing declaration (.things file):
Thing topic LR_Temperature “LR_Temperature” (mqtt:broker:mosquitto)
{
Channels:
Type string : LivingRoom_Temperature “LivingRoom Temperature” [stateTopic=“in/A_Thermostat_Room_Temperature/state”]

}

twopoints.js file in conf/transform folder:
(function (i) {
return 99;
})(input)

(I tried item always shows 99 but it is always show the number from the mqtt channel: 15,545853 if topic publish: in/A_Thermostat_Room_Temperature/state 15,545853)

What do you find in openhab.log when you display your Item?

What is the sitemap entry? (If you have specified an optional label in there, it overrides the Item label)

In openhab.log:

08:44:46.435 [INFO ] [ome.event.ThingStatusInfoChangedEvent] - ‘mqtt:topic:LR_Temperature’ changed from OFFLINE (BRIDGE_OFFLINE) to ONLINE
08:46:30.919 [INFO ] [smarthome.event.ItemStateChangedEvent] - Temperature_GF_Living changed from NULL to 15
08:46:56.129 [INFO ] [smarthome.event.ItemStateChangedEvent] - Temperature_GF_Living changed from 15 to 15.63
08:47:28.786 [INFO ] [smarthome.event.ItemStateChangedEvent] - Temperature_GF_Living changed from 15.63 to 15,212522

(NOTE: I tried different messages published by myself. It should be always 99)

My .sitemap file:
sitemap default label=“Main Menu”
{
Frame
{
Group item=gGF label=“Ground Floor” icon=“groundfloor”
Group item=gGarage label=“Garage” icon=“garage”
Group item=gGarden icon=“garden”
}

Frame label="Lights" 
{
	Switch item=Lights mappings=[OFF="All Off"]
}

}

Well, transformation works for everyone else.
If I introduce a deliberate error in the transform filename, I get an error in openhab.log at the time of display on UI. Maybe try that to see if your transformation service is working.

Your sitemap does not even show the Temperature_GF_Living item… The transformation is only applied to the displayed label text, not to the value of Temperature_GF_Living (see https://www.openhab.org/docs/configuration/transformations.html#usage). The value of Temperature_GF_Living will always be what it receives via MQTT.
You can find hints on how to transform the incoming value before it is assigned to the item in the MQTT binding’s documentation: https://www.openhab.org/addons/bindings/mqtt.generic/#incoming-value-transformation.

Okay, I don’t know why (I think it is because I didn’t restart openhab) it didn’t work, but now it is, and with the code from rossko57. Thank you very much.

Is it any way to update the value in real time? I mean, when a new message arrive, I have to go back in basicUI and come back to reload the value showed. I would like it happens in real time.

I tried this:
rule “Living Room Temperature”
when
Item Temperature_GF_Living received command
then
postUpdate(Temperature_GF_Living,receivedCommand)
end

The OP has chosen to display his Item via a sitemap Group entry. That’s fine, the transform in the Item label will still get applied.

None of the UIs are that good at instant refresh. Be sure there are no errors in your sitemap.

That rule is pointless. The MQTT binding updates the Item without any help. The MQTT binding is not issuing commands, so the rule will never be triggered. Just delete it.