How to divide 2 values from 2 separate messages

I am trying to do some very simple division of two values and continue to get a 'NULL' result. I believe this is due to different data types. (Int and Float) I have previously converted one of these data types from Modbus outputs to Float, and now have tried multiple things to convert both numbers to Float, and both to Int, but have had no luck. I know it is difficult to determine if you can't see the inputs but I will try to add as much detail as possible.

[{"id":"d2198a8d.1bb648","type":"function","z":"4e5adae1.869064","name":"","func":"\nmsg.topic = \"SEC_TPH\";\nmsg.SEC_TPH = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":890,"y":420,"wires":[["d4439b27.1a8528","7430d36d.7e030c"]]},{"id":"e0ab8a1e.ce2178","type":"link in","z":"4e5adae1.869064","name":"","links":["172281b7.00e07e"],"x":495,"y":420,"wires":[["a8446cef.3262f"]]},{"id":"a4253ef3.3371e","type":"link in","z":"4e5adae1.869064","name":"","links":["564d43a6.9d70ec"],"x":495,"y":480,"wires":[["ce9b7ed6.aefd7"]]},{"id":"ce9b7ed6.aefd7","type":"function","z":"4e5adae1.869064","name":"","func":"\nmsg.topic = \"SEC_AMPS\";\nmsg.SEC_AMPS = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":890,"y":480,"wires":[["24daf0bc.f8064","7430d36d.7e030c"]]},{"id":"24daf0bc.f8064","type":"debug","z":"4e5adae1.869064","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1090,"y":520,"wires":[]},{"id":"d4439b27.1a8528","type":"debug","z":"4e5adae1.869064","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":950,"y":340,"wires":[]},{"id":"7430d36d.7e030c","type":"function","z":"4e5adae1.869064","name":"","func":"var A = Number(msg.SEC_AMPS);\nvar B = Number(msg.SEC_TPH);\n\n//var A = 4;\n//var B = 2;\n\nvar C = (A / B);\nmsg.value = C;\n\nreturn msg;","outputs":1,"noerr":0,"x":1170,"y":460,"wires":[["2989b794.ddd1e8"]]},{"id":"2989b794.ddd1e8","type":"debug","z":"4e5adae1.869064","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1350,"y":460,"wires":[]},{"id":"a8446cef.3262f","type":"function","z":"4e5adae1.869064","name":"SImple Float to IntValue","func":"var intvalue = Math.floor( msg.payload);\nmsg.numb = intvalue;\nmsg.payload = intvalue;\nreturn msg;\n\n","outputs":1,"noerr":0,"x":690,"y":420,"wires":[["d2198a8d.1bb648","d4439b27.1a8528"]]}]

image

This is the flow for one piece:

[{"id":"2687e253.99ebce","type":"function","z":"55ae322c.6c4b0c","name":"","func":"msg.payload = msg.payload[0];\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":580,"wires":[["65b1b7bc.3d7958"]]},{"id":"1c954bd1.8ba794","type":"delay","z":"55ae322c.6c4b0c","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":1077.9999771118164,"y":618.0000591278076,"wires":[["48d7ae7c.b39fd","99fdb69.b451048","564d43a6.9d70ec"]]},{"id":"9772995b.28e908","type":"smooth","z":"55ae322c.6c4b0c","name":"","property":"payload","action":"mean","count":"3","round":"0","mult":"single","x":860.1666450500488,"y":601.0000152587891,"wires":[["ec42e0cf.5eac4","1c954bd1.8ba794"]]},{"id":"ec42e0cf.5eac4","type":"delay","z":"55ae322c.6c4b0c","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":1076.1666641235352,"y":740.3334140777588,"wires":[["791f4fd8.b9a62"]]},{"id":"996b4fc5.1cac9","type":"modbus-response","z":"55ae322c.6c4b0c","name":"Modbus Errors","registerShowMax":20,"x":380,"y":660,"wires":[]},{"id":"48d7ae7c.b39fd","type":"ui_gauge","z":"55ae322c.6c4b0c","name":"SECONDARY","group":"1702c144.fc178f","order":5,"width":"0","height":"0","gtype":"gage","title":"<font size=\"+1\">SECONDARY PLANT","label":"AMPS","format":"{{value}}","min":0,"max":"800","colors":["#00b500","#e6e600","#ca3838"],"seg1":"700","seg2":"775","x":1320,"y":620.0000772476196,"wires":[]},{"id":"f0b7d042.25fa9","type":"modbus-read","z":"55ae322c.6c4b0c","name":"SECONDARY AMPS","topic":"","showStatusActivities":true,"showErrors":true,"unitid":"1","dataType":"InputRegister","adr":"2","quantity":"1","rate":"2000","rateUnit":"ms","delayOnStart":false,"startDelayTime":"","server":"579d818b.985d6","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":163,"y":607,"wires":[["2687e253.99ebce"],["996b4fc5.1cac9"]]},{"id":"65b1b7bc.3d7958","type":"function","z":"55ae322c.6c4b0c","name":"","func":"if (msg.payload <= 1000)\n{\n    return msg;\n}\nelse \n{\n msg.payload = 0\n return msg;\n}\n","outputs":1,"noerr":0,"x":561,"y":603,"wires":[["9772995b.28e908"]]},{"id":"791f4fd8.b9a62","type":"ui_chart","z":"55ae322c.6c4b0c","name":"SECONDARY TREND","group":"1702c144.fc178f","order":6,"width":"6","height":"3","label":"<font size=\"+1\">SECONDARY TREND","chartType":"line","legend":"false","xformat":"HH:mm","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":"12","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":1291,"y":743,"wires":[[],[]]},{"id":"564d43a6.9d70ec","type":"link out","z":"55ae322c.6c4b0c","name":"SECONDARY AMPS","links":["a4253ef3.3371e"],"x":1435,"y":660,"wires":[]},{"id":"99fdb69.b451048","type":"debug","z":"55ae322c.6c4b0c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1320,"y":700,"wires":[]},{"id":"1702c144.fc178f","type":"ui_group","z":"","name":"Crusher Amps","tab":"c1801d09.e9298","order":4,"disp":true,"width":"6","collapse":false},{"id":"579d818b.985d6","type":"modbus-client","z":"","name":"Secondary Q PLC","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"10.35.210.101","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"},{"id":"c1801d09.e9298","type":"ui_tab","z":"","name":"CRUSHER AMPS","icon":"dashboard","order":2}]

This is the other piece:

[{"id":"e3aec82e.b3aad8","type":"ui_text","z":"a8ea106b.78be5","group":"9e071898.00b0c8","order":3,"width":0,"height":0,"name":"PIT CONV (Tons / hr )","label":"PIT CONV (Tons / hr )","format":"{{msg.payload}}","layout":"row-spread","x":1640,"y":320,"wires":[]},{"id":"2d38055b.ba9eba","type":"delay","z":"a8ea106b.78be5","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"10","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":1320,"y":360,"wires":[["1de2fd63.8c0a53"]]},{"id":"2854a1f.291de5e","type":"modbus-read","z":"a8ea106b.78be5","name":"Plant35 Secondary BeltScale Rate","topic":"","showStatusActivities":false,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"254","quantity":"2","rate":"2000","rateUnit":"ms","delayOnStart":false,"startDelayTime":"","server":"3d59fedd.73baa2","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":200,"y":300,"wires":[["6573091d.90fdb8"],["cf9c7015.d19b2","6d704015.9ef1a"]]},{"id":"6573091d.90fdb8","type":"modbus-response","z":"a8ea106b.78be5","name":"","registerShowMax":20,"x":550,"y":260,"wires":[]},{"id":"6d704015.9ef1a","type":"function","z":"a8ea106b.78be5","name":"(Modified by Colin) Convert byte array to SIGNED 32-bit integer","func":"// Create new Buffer based on array bytes\nconst buf = Buffer.from(msg.payload.buffer);\n// swap the words\nconst temp0 = buf[0];\nconst temp1 = buf[1];\nbuf[0] = buf[2];\nbuf[1] = buf[3];\nbuf[2] = temp0;\nbuf[3] = temp1;\n\n// Represent these bytes as 32-bit unsigned int\nconst value = buf.readInt32BE();\n\n// save the value\nmsg.payload = value;\n\nreturn msg;","outputs":1,"noerr":0,"x":670,"y":320,"wires":[["3c8b029f.e0709e"]]},{"id":"a83cc940.d6cbd8","type":"debug","z":"a8ea106b.78be5","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":1310,"y":280,"wires":[]},{"id":"cf9c7015.d19b2","type":"debug","z":"a8ea106b.78be5","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":510,"y":360,"wires":[]},{"id":"3c8b029f.e0709e","type":"function","z":"a8ea106b.78be5","name":"2 Decimal - IEEE-754 to float","func":"/* Converts from an number, string, buffer or array representing an IEEE-754 value\n* to a javascript float.\n* The following may be given in msg.payload:\n*      A string representing a number, which may be hex or binary\n*        examples, \"1735\"  \"0x02045789\"  0b01000000010010010000111111011011\n*      An integer value\n*      A four element array or buffer of 8 bit values, most significant byte first.\n*/\n// first make a number from the given payload if necessary\nlet intValue;\nif (typeof msg.payload === \"number\") {\n  intValue = msg.payload;\n} else if (typeof msg.payload === \"string\") {\n  intValue = Number(msg.payload);\n} else if (msg.payload.length == 2) {\n  // four byte array or buffer\n  intValue = (msg.payload[0] << 8) + msg.payload[1]; //) << 8) + msg.payload[2]) << 8) + msg.payload[3];\n} else {\n  node.warn(\"Unrecognised payload type or length\");\n}\n\nmsg.payload = Int2Float32(intValue);\nreturn msg;\n\nfunction Int2Float32(bytes) {\n  var sign = (bytes & 0x80000000) ? -1 : 1;\n  var exponent = ((bytes >> 23) & 0xFF) - 127;\n  var significand = (bytes & ~(-1 << 23));\n\n  if (exponent == 128)\n      return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);\n\n  if (exponent == -127) {\n      if (significand === 0) return sign * 0.0;\n      exponent = -126;\n      significand /= (1 << 22);\n  } else significand = (significand | (1 << 23)) / (1 << 23);\n\n  var finalValue = sign * significand * Math.pow(2, exponent);\n  return Math.round((finalValue + Number.EPSILON) * 100) / 100\n  //return sign * significand * Math.pow(2, exponent);\n}","outputs":1,"noerr":0,"x":1060,"y":320,"wires":[["a83cc940.d6cbd8","e3aec82e.b3aad8","2d38055b.ba9eba","172281b7.00e07e"]]},{"id":"172281b7.00e07e","type":"link out","z":"a8ea106b.78be5","name":"PRIMARY TPH","links":["df90984b.dd0c48","e0ab8a1e.ce2178"],"x":1300,"y":420,"wires":[]},{"id":"1de2fd63.8c0a53","type":"ui_chart","z":"a8ea106b.78be5","name":"PIT CONV (Tons/hr)","group":"9e071898.00b0c8","order":4,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"HH:mm","interpolate":"linear","nodata":"No Data","dot":false,"ymin":"","ymax":"","removeOlder":"12","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":1640,"y":360,"wires":[[],[]]},{"id":"9e071898.00b0c8","type":"ui_group","z":"","name":"PLANT 35 PRIMARY PIT CONV","tab":"629c9ff9.243eb","order":1,"disp":true,"width":"6","collapse":false},{"id":"3d59fedd.73baa2","type":"modbus-client","z":"","name":"Primary PLC","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"10.35.210.100","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"},{"id":"629c9ff9.243eb","type":"ui_tab","z":"","name":"Plant 35","icon":"dashboard","order":1}]

I just noticed that even if I feed two float values into the function I am trying to do the division in, I still get a NULL result. So, I am not sure why either.

image

See here

Edit, I updated the title of this to better help future users find answers to similar issues

Thank you!! I should have realized that. Works perfectly now.

1 Like

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