Round all values of an object

Hi,
is it possible to round all values of an object with one function ?

[{"id":"6f07f889.bda788","type":"calculate","z":"9f6752b0.b55988","name":"Durchschnitt pro Minute","pauseType":"timed","calculation":"average","timeout":"1","timeoutUnits":"minutes","rate":"10","x":1610,"y":1120,"wires":[["6fa4b617.d9038"]]},{"id":"6fa4b617.d9038","type":"join","z":"9f6752b0.b55988","name":"sammeln","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"8","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1660,"y":1160,"wires":[["1347a9a4.bb4856"]]},{"id":"1347a9a4.bb4856","type":"debug","z":"9f6752b0.b55988","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1910,"y":1160,"wires":[]}]

{"PAC3":212.36363636363637,"PAC2":404.45454545454544,"Pein":0.7272727272727273,"Pbez":2.5454545454545454,"PAC1":211,"Pges":615.4545454545455,"Verbrauch":829.6363636363636,"SoC":10}

with parseFloat:

var temp = msg.payload[".."];
msg.payload = parseFloat(temp.toFixed(0));
return msg;

sure try

Object.entries(msg.payload).forEach((v) =>{
    msg.payload[v[0]] = Math.round(v[1]);
})
return msg;
1 Like

What are you doing with the values?

Modbus query every 5s --> Node Red Dashboard

I want to feed influxdb every 1 min with rounded values as an object:

why would you want to remove precision? Normally you store accurate values then only when you present them to a webpage or report do you apply rounding.

-Storage
-Resource / CPU load
-Clarity
-Unnecessary

It won't make any difference to the amount of stored data or cpu load, in fact rounding it will increase the cpu load.

If you don't want it visible when you display it then round it when you display it.

Moot - all Js numbers are same size

Moot - negligible

The only unnecessary part is the rounding you are doing.


I hope you never have to sum up or summarise your data - cos it will be inaccurate.

Your choice.

And heaven help you if you ever need to know the rate of change of a value.

:face_with_raised_eyebrow:
I noticed a very high RAM/SWP consumption/occupancy of Influxdb

and that's why I first save all values instead of in different measurements as an object in an measurement:
grafik

now:
grafik
(I will summarize WMZt into one)

grafik

That is nothing to do with the rounding. I am sure that influx stores values as floating point binary values, not as strings, so whether you round them to some decimal precision will make no difference.

As for the display, it is not normal to use the influx command line for displaying data, that is just for diagnosing problems and such like. If you are displaying values in grafana or node red it is generally better to round at the point of display, which both node red and grafana will do for you.

Is it possible to reduce this from 11 functions to 1 or 2 ?

[{"id":"ac586536.94f728","type":"join","z":"42651355.aa1f14","name":"sammeln der LOG.SET Werte","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"11","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":530,"y":680,"wires":[["a7bb7f9d.2c971","e671d252.de5308","1b50fd4f.d7de1b","adf165d0.f5f98","23d44ada.be3786","6748251.e9060dc","1f9656cf.0d9d31","2ba7d4c8.19f814","786d7f8e.e0b228","a3f1f55d.4e7ae","9853b949.b54b4"]]},{"id":"58c72b8a.cc137c","type":"calculate","z":"42651355.aa1f14","name":"Durchschnitt pro Minute","pauseType":"timed","calculation":"average","timeout":"1","timeoutUnits":"minutes","rate":"10","x":510,"y":720,"wires":[["ac586536.94f728"]]},{"id":"e671d252.de5308","type":"function","z":"42651355.aa1f14","name":"RLT runden","func":"var temp = msg.payload[40012];\nmsg.payload = parseFloat(temp.toFixed(1));\nmsg.topic = \"RLT\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":280,"wires":[["cd2af0c2.da42d8"]]},{"id":"1b50fd4f.d7de1b","type":"function","z":"42651355.aa1f14","name":"WWo runden","func":"var temp = msg.payload[40013];\nmsg.payload = parseFloat(temp.toFixed(1));\nmsg.topic = \"WWo\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":320,"wires":[["cd2af0c2.da42d8"]]},{"id":"adf165d0.f5f98","type":"function","z":"42651355.aa1f14","name":"WW runden","func":"var temp = msg.payload[40014];\nmsg.payload = parseFloat(temp.toFixed(1));\nmsg.topic = \"WW\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":360,"wires":[["cd2af0c2.da42d8"]]},{"id":"23d44ada.be3786","type":"function","z":"42651355.aa1f14","name":"KTe runden","func":"var temp = msg.payload[40015];\nmsg.payload = parseFloat(temp.toFixed(1));\nmsg.topic = \"KTe\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":400,"wires":[["cd2af0c2.da42d8"]]},{"id":"6748251.e9060dc","type":"function","z":"42651355.aa1f14","name":"KTa runden","func":"var temp = msg.payload[40016];\nmsg.payload = parseFloat(temp.toFixed(1));\nmsg.topic = \"KTa\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":440,"wires":[["cd2af0c2.da42d8"]]},{"id":"1f9656cf.0d9d31","type":"function","z":"42651355.aa1f14","name":"Hz runden","func":"var temp = msg.payload[43136];\nmsg.payload = parseFloat(temp.toFixed(0));\nmsg.topic = \"Hz\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":480,"wires":[["cd2af0c2.da42d8"]]},{"id":"2ba7d4c8.19f814","type":"function","z":"42651355.aa1f14","name":"Pth runden","func":"var temp = msg.payload[\"Pth\"];\nmsg.payload = parseFloat(temp.toFixed(0));\nmsg.topic = \"Pth\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":520,"wires":[["cd2af0c2.da42d8"]]},{"id":"a7bb7f9d.2c971","type":"function","z":"42651355.aa1f14","name":"VLT runden","func":"var temp = msg.payload[40008];\nmsg.payload = parseFloat(temp.toFixed(1));\nmsg.topic = \"VLT\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":240,"wires":[["cd2af0c2.da42d8"]]},{"id":"786d7f8e.e0b228","type":"function","z":"42651355.aa1f14","name":"GM runden","func":"var temp = msg.payload[\"43005\"];\nmsg.payload = parseFloat(temp.toFixed(0));\nmsg.topic = \"GM\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":560,"wires":[["cd2af0c2.da42d8"]]},{"id":"a3f1f55d.4e7ae","type":"function","z":"42651355.aa1f14","name":"WT runden","func":"var temp = msg.payload[\"43437\"];\nmsg.payload = parseFloat(temp.toFixed(0));\nmsg.topic = \"WT%\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":600,"wires":[["cd2af0c2.da42d8"]]},{"id":"9853b949.b54b4","type":"function","z":"42651355.aa1f14","name":"KT runden","func":"var temp = msg.payload[\"43439\"];\nmsg.payload = parseFloat(temp.toFixed(0));\nmsg.topic = \"KT%\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":640,"wires":[["cd2af0c2.da42d8"]]},{"id":"cd2af0c2.da42d8","type":"join","z":"42651355.aa1f14","name":"sammeln aller Werte für InfluxDB","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"14","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":860,"y":280,"wires":[["2bfda1bf.fbc7ce"]]},{"id":"2bfda1bf.fbc7ce","type":"influxdb out","z":"42651355.aa1f14","influxdb":"72e5611c.85f388","name":"Sensordaten","measurement":"Sensordaten","precision":"","retentionPolicy":"","database":"db","precisionV18FluxV20":"s","retentionPolicyV18Flux":"","org":"organisation","bucket":"bucket","x":1090,"y":280,"wires":[]},{"id":"72e5611c.85f388","type":"influxdb","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"database","name":"InfluxDB","usetls":false,"tls":"","influxdbVersion":"1.8-flux","url":"http://localhost:8086","rejectUnauthorized":true}]

I´m giving each value a topic + round.

var temp = msg.payload[40016];
msg.payload = parseFloat(temp.toFixed(1));
msg.topic = "KTa";
return msg;

toFixed(1) or (0).

you can reduce that to 1 function

Replace them all with one function node & put this in it (you will need to do the remaining items)


function sendIt(topic, address) {
    //make a new msg with the topic and formatted value
    var m = {
        topic: topic,
        payload: parseFloat(msg.payload[address].toFixed(1))
    } 
    node.send(m); //send topic/payload
}

sendIt("VLT", 40008);
sendIt("RLT", 40012);
sendIt("WWo", 40013);
sendIt("WW", 40014);
//sendIt("xxx", yyy);  // todo
//sendIt("xxx", yyy);  // todo
//sendIt("xxx", yyy);  // todo
//sendIt("xxx", yyy);  // todo

1 Like

Can take rounding also into play

function sendIt(topic, address, roundTo) {
    //make a new msg with the topic and formatted value
    var m = {
        topic: topic,
        payload: parseFloat(msg.payload[address].toFixed(roundTo))
    } 
    node.send(m); //send topic/payload
}

sendIt("VLT", 40008, 1);
sendIt("RLT", 40012, 0);
2 Likes

very nice guys,
I´ve only one problem with a value without adress:
"Pth":

{"topic":"Pth","payload":2149,"raw":{"register":"43375","factor":1,"size":"s16","mode":"R","titel":"compr. in power mean","info":"Mean power delivered from the inverter to the compressor. Mean is calculated every 10 seconds.","unit":"W","min":"0","max":"0","logset":true,"data":30,"raw_data":30,"timestamp":1613503622201},"_msgid":"c9905cba.e9fd9"}

ReferenceError: sentIt is not defined (line 19, col 1)

the other values a raw from Nibe like this:

{"topic":"40016","payload":1.4,"raw":{"register":"40016","factor":10,"size":"s16","mode":"R","titel":"EB100-EP14-BT11 Brine Out Temp","info":"Brine out temperature, BT11","unit":"°C","min":"0","max":"0","logset":true,"data":1.4,"raw_data":1.4,"timestamp":1613503622201},"_msgid":"95b57052.68264"}

grafik

And what is expected output in this case?

:face_with_monocle:
the output should just be rounded, but the input has no address in case of "Pth" (it is a calculated value from me).

this comes into the function:

{"topic":"Pth","payload":{"40008":26.48928571428571,"40012":23.100000000000012,"40013":40.60000000000001,"40014":29.399999999999984,"40015":4.700000000000002,"40016":1.4071428571428566,"43005":-200.1178571428572,"43136":27.99285714285714,"43437":33,"43439":1,"Pth":2132.035714285714},"raw":{"register":"43375","factor":1,"size":"s16","mode":"R","titel":"compr. in power mean","info":"Mean power delivered from the inverter to the compressor. Mean is calculated every 10 seconds.","unit":"W","min":"0","max":"0","logset":true,"data":30,"raw_data":30,"timestamp":1613504149059},"_msgid":"602a1c1b.b51cd4","pushedAt":1613504149076,"points":28,"firstPushedAt":1613504091096,"calculation":"average"}

sorry forget it, my misstake :woozy_face:

sendIt not sentIt

Then it takes to check against is the address a number

function sendIt(topic, address, roundTo) {
    //make a new msg with the topic and formatted value
    var m = {
        topic: topic
    }
    if(isNaN(address)){
        //if the address is not a number then the address is topic
        m.payload = parseFloat(msg.payload[topic].toFixed(roundTo))
    }else{
        m.payload = parseFloat(msg.payload[address].toFixed(roundTo))
    }
    node.send(m); //send topic/payload
}

Foolish me.

sendIt("Pth", 'Pth', 0); is more than enough

No need to change anything

1 Like