Temperature sensor to round in Node-RED

Dear All!

I have a temperature sensor value coming from S7-300 PLC. I get the value in a long decimal format, which does not look good on a dashboard. I would like to round it to 1 decimal, like 23,5 Celsius. I have no Java experience, I tried to googled it, but after a few hours I decided to ask for help. At the moment my node looks like this:


I am planning to use a function to round the value to 1 decimal, like 28,6.

In the function I tried several things, without success. My latest attempt looks like the following:

Can somebody give me a good example how to do that from the beggining untill the end?I did not find any good solution. In which tab should I write it? On start or Message?

My other problem is that at the moment the Gauge shows nothing: 0.

you dont need a function to round it .. you could but its not needed because that functionality is there in the Gauge config.

image

where | number: 1 defines the decimal points to use

possibly because you used toFixed in the function that converts payload to a string and the gauge expects a number (not tested)

Test:

[{"id":"5a4ee787702b045b","type":"ui_gauge","z":"5847b7aa62131d37","name":"","group":"2f14c49684858048","order":0,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{msg.payload | number:1 }}","min":0,"max":"50","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","className":"","x":580,"y":1060,"wires":[]},{"id":"58308cf632b9808f","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"23.3245435","payloadType":"num","x":360,"y":1060,"wires":[["5a4ee787702b045b"]]},{"id":"2f14c49684858048","type":"ui_group","name":"Default","tab":"8b65008e9fad5be6","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"8b65008e9fad5be6","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

In addition to the solution posted above, you could use the Smooth node (needs to be installed in Manage Palette -> Install). It can be used for rounding values, and I use them frequently in conjunction with DS18B20 sensors.

Add a debug node showing what you have going into the function node that you showed.
For the formatting, toFixed(1) should work, but the best way is to do it in the gauge as suggested. Generally you should not throw away accuracy until you need to.