How to get temperature values below zero

Hello,
using Modbus to get the outdoor temperature value with the range of negative numbers too.
Reading the Modbus address works fine and gets the data as:

Kessel (1) : msg.payload : Object
object
data: array[1]
0: 65533
buffer: buffer[2]
0: 0xff
1: 0xfd

The connected device sends the temperature value multiplied by 10 (1,5 °C -> 15) as INT16 (signed value).
But how have this value be handled in Node-RED to devide by 10 and get negative numbers too?

msg.payload = msg.payload[0] / 10;
return msg;

Node-RED seems to use an unsigned value, how to use as signed value or cast / convert to signed?
Thanks!

As it is only 1 field, a call to the buffer functions is really all it takes to solve this, however, there is a node that simplifies this - node-red-contrib-buffer-parser

Demo...

[{"id":"cad1ac8d.36dc","type":"function","z":"1f9c7e1a.6fad92","name":"fake the modbus data","func":"//fake the modbus data\nmsg.payload = {};\nmsg.payload.data = [65533];\nmsg.payload.buffer = Buffer.from([0xff, 0xfd])\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1680,"y":140,"wires":[["3126af75.ff23a","32cbecbd.e13de4"]]},{"id":"3021ca58.1ecc16","type":"inject","z":"1f9c7e1a.6fad92","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1660,"y":80,"wires":[["cad1ac8d.36dc"]]},{"id":"3126af75.ff23a","type":"buffer-parser","z":"1f9c7e1a.6fad92","name":"","data":"payload.buffer","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16be","name":"temperature","offset":0,"length":1,"offsetbit":0,"scale":"/ 10","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1910,"y":140,"wires":[["c82c506a.a5897"]]},{"id":"c82c506a.a5897","type":"debug","z":"1f9c7e1a.6fad92","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2090,"y":140,"wires":[]},{"id":"32cbecbd.e13de4","type":"function","z":"1f9c7e1a.6fad92","name":"manual calulation","func":"var temp = msg.payload.buffer.readInt16BE(0);\ntemp /= 10;\nmsg.payload = {\n    temperature: temp\n} \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1890,"y":180,"wires":[["8b66d58d.9ae8b8"]]},{"id":"8b66d58d.9ae8b8","type":"debug","z":"1f9c7e1a.6fad92","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2090,"y":180,"wires":[]}]

Thanks, "fast as the wind"!
"buffer parser" shows this msg:
"TypeError: scale.matchAll is not a function"
What is missing?
Norbert

What version of NODE are you running? enter node -v in a terminal or cmd window

what version of buffer parser do you have installed? (check in the "palette" -> installed)

NOTE: I am the author of that node so would appreciate figuring that out. :slight_smile:

Cheers.

EDIT...

I suspect that you are using NODE V10 (or less) as matchAll was introduced in Node v12.

NOTE: NODE10 is End of Life in in April so I am not overly keen to alter the code.

Let me know either way.

Running as docker container @RPi 4 with:

image: nodered/node-red:latest

and yes, it's v10.23.1

I don't use docker (more trouble than its worth imo)

I am surprised latest is targeting node V10. Perhaps there is an update to that image? How long ago did you install it?

Ps, I'm case you were not aware (I'm sure you are), there is a simple setup script for installing node-red natively on the pi?

Changed container to
image: nodered/node-red:latest-12
and it works great! Thanks!

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