How to limit temp display on dashboard to 2 decimals

New so maybe this is simple but I can't seem to figure it out or find it anywhere.

I am reading temperature via MQTT the msg comes as 10th degree C. I want it displayed on my dashboard in F. So first I convert the 10th degree C to C then convert C to F. Issue I have is sometimes my dashboard will show many decimal places and I want it limited to 1 or 2 decimals places.

MQTTin linked to a function linked to a Dashboard Text

This works good to do the conversion but shows to many decimal places at times.
var o = msg.payload
msg.payload = msg.payload /10 * 9 / 5.0 + 32
return msg

This limits the decimal places but then never refreshes whatever temp it was when I deploy stays.
var o = msg.payload
msg.payload = (msg.payload /10 * 9 / 5.0 + 32).msg.toFixed(2);
return msg

This limits the decimal places but then never refreshes whatever temp it was when I deploy stays.
var o = msg.payload
msg.payload = Number((temp /10 * 9 / 5 + 32).toFixed(2));
return msg

Any help appreciated.

Thanks

Hi 007Bond,

Welcome to the community of node-red enthusiasts :slight_smile:

Give it a try like this way to round a number to two decimals. So value is, what you have to change to your calculation:

var number = Math.round(Number.parseFloat(value) * 100)/100;
node.warn(number);

Or maybe do first your calculation in a variable and then use the code, to see, if it works. Afterwards you can shrink the code step by step:

var value = <your calculation in here>;
var number = Math.round(Number.parseFloat(value) * 100)/100;
node.warn(number);

Cheers
Ranki

Use your working formula then use angular decimal pipe {{msg.payload|number:2}} to limit the display on a dashboard item.

2 Likes

Oh yes @Steve-Mcl,

Just wanted to add that, if you only have the value field as the whole formula will not work in the dashboard input. Only in a function.

Cheers
Ranki

Can you explain in more detail what you mean. Give an example where it will not work.

Hi Colin,

Maybe my English was not that well :upside_down_face:

My formula does only work in a function, but not directly in the dashboard item, where you specify the {{msg.payload}} like {{msg.payload|number:2}}.

I hope I made it more clear?!

Cheers
Ranki

Edit: I did not have my first coffee while posting the first answer :grin:

Understood, I thought you meant that the dashboard technique did always work.

Thank you both for your replies.

I tried a few variations of the above in the formula node but could not get it error free. Probably since I am new I had some typo or missing this or that.

I was wondering what that field could be used for in the dashboard item. So putting the angular pipe there seems to have done the trick. So far it seems to be limited as it should and be updating as it should.

Thank you both as each time I learn a little more.

If it isn't updating add a debug node showing what is going to the dashboard and check that update messages are arriving and look right.

OK learning something new this is helpful but I have a question.

I added the debug node after the function but that is showing the converted data with all the decimals.

Reason is what worked to limit the decimal places was {{msg.payload|number:1}} in the dashboard test node. So the final transformation is taking place in the dashboard node. I can't seem to figure out how to see this final output in the debug window. I did try to put {{msg.payload|number:1}} in the debug node but did not work out.

Thanks again for the help everyone.

Why do you need to see it in the debug node when you can see it on the dashboard?

You had said "If it isn't updating add a debug node showing what is going to the dashboard and check that update messages are arriving and look right."

So I was trying out what you said so I could learn this so that in the future I could use it to help me if I am having an issue.

But yes right now it seems to be displaying on the dashboard ok.

The reason for adding the debug node was to find out why it was not updating. I assumed that by 'not updating' you meant that the dashboard value was not changing when it should. Is it updating correctly?

Yes originally I was trying to do this rounding in the function node this way.

This limits the decimal places but then never refreshes whatever temp it was when I deploy stays.
var o = msg.payload
msg.payload = (msg.payload /10 * 9 / 5.0 + 32).msg.toFixed(2);
return msg

This limits the decimal places but then never refreshes whatever temp it was when I deploy stays.
var o = msg.payload
msg.payload = Number((temp /10 * 9 / 5 + 32).toFixed(2));
return msg

In this case it would update once and then not again.

I see so you are saying the debug node may have pointed to the issue in the function node of why it was not updating after the first time.

OK, so it is all working well, good.

should be

msg.payload = (msg.payload /10 * 9 / 5.0 + 32).toFixed(2);

Yup that works in the function node and updates as it should.

Thanks all

Hi 007bond,

Just another trick, which might help you in the future. With the following function you can debug intermediate results within the function to see, if everything is working well:

node.warn(variable);

This helps me quite often to locate the error, where sth. is going wrong. Furthermore it makes sense to cut bigger coding lines/ transactions in more little pieces. Then it is also easier to locate, which part does not work and makes research easier. If everything is working you can shrink it to a more efficient code line. I tried to do it with your code (a little bit excessive, but to show what I mean):

var o = msg.payload
msg.payload = msg.payload /10;
node.warn(msg.payload);
msg.payload = msg.payload * 9;
node.warn(msg.payload);
msg.payload = msg.payload / 5.0;
node.warn(msg.payload);
msg.payload = msg.payload + 32;
node.warn(msg.payload);
msg.payload = msg.payload.toFixed(2);
node.warn(msg.payload);
return msg

Cheers
Ranki

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.