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.
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
(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)
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.
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.