Almost! hex string to decimal value help needed

I'm so glad I 'found' node-red, have already some flows working!

But this one I cannot solve: after half a day of trying and searching I'm stuck at the last step: HEX to DECIMAL so I can feed the value to InfluxDB

What I now have is a 'STRING' HEX value but I need to convert it to decimal.

Here is my flow:

[{"id":"061a039b50b07042","type":"tab","label":"Youless direct","disabled":false,"info":"","env":[]},{"id":"9ca3b0bfbc02dbee","type":"buffer-parser","z":"061a039b50b07042","name":"buffer parser","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"hex","name":"TX_Relay_ID","offset":0,"length":2,"offsetbit":0,"scale":"1","mask":""},{"type":"hex","name":"Packet_ID","offset":2,"length":2,"offsetbit":0,"scale":"1","mask":""},{"type":"hex","name":"RX_Data[0]","offset":4,"length":4,"offsetbit":0,"scale":"1","mask":""},{"type":"hex","name":"RX_Data[1]","offset":8,"length":4,"offsetbit":0,"scale":"1","mask":""},{"type":"hex","name":"RX_Data[2]","offset":12,"length":4,"offsetbit":0,"scale":"1","mask":""},{"type":"hex","name":"RX_Data[3]","offset":16,"length":4,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap32","swap2":"","swap3":"swap16","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":true,"fanOutMultipleResult":true,"setTopic":true,"outputs":6,"x":490,"y":400,"wires":[["5c50d3bf5b7fdd4f"],["aff6cb1d41795ec9"],["6214fe344107f174"],["ee43a22bf059e4ec"],["4b8801982349a9f9"],["add7f23f54e45522"]]},{"id":"3ca63d7ef88a1e9d","type":"inject","z":"061a039b50b07042","name":"Test UDP packet","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"[130,0,163,0,214,0,0,0,214,1,0,0,76,0,0,0,176,3,0,0]","payloadType":"bin","x":230,"y":360,"wires":[["9ca3b0bfbc02dbee","b9d543696baed89e"]]},{"id":"5c50d3bf5b7fdd4f","type":"debug","z":"061a039b50b07042","name":"TX_Relay_ID","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":300,"wires":[]},{"id":"b9d543696baed89e","type":"debug","z":"061a039b50b07042","name":"raw UDP data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":220,"wires":[]},{"id":"0a5b884999e83813","type":"udp in","z":"061a039b50b07042","d":true,"name":"","iface":"","port":"9889","ipv":"udp4","multicast":"false","group":"","datatype":"buffer","x":260,"y":420,"wires":[["9ca3b0bfbc02dbee"]]},{"id":"aff6cb1d41795ec9","type":"debug","z":"061a039b50b07042","name":"Packet_ID","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":340,"wires":[]},{"id":"6214fe344107f174","type":"debug","z":"061a039b50b07042","name":"RX_Data[0]","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":380,"wires":[]},{"id":"ee43a22bf059e4ec","type":"debug","z":"061a039b50b07042","name":"RX_Data[1]","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":420,"wires":[]},{"id":"4b8801982349a9f9","type":"debug","z":"061a039b50b07042","name":"RX_Data[2]","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":460,"wires":[]},{"id":"add7f23f54e45522","type":"debug","z":"061a039b50b07042","name":"RX_Data[3]","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":500,"wires":[]}]

These convert a hex string to decimal, but there may be other ways that those who know more about these things

	function isHexString(hexString) {
		let l = hexString.length;

		return (parseInt(hexString, 16).toString(16).padStart(l, '0') === hexString.toLowerCase());

	}

	function hexToInteger(hexString, padCount = 2) {
		if (isHexString(hexString)) {
			return parseInt(hexString.slice(-padCount), 16);

		} else {
			return undefined;

		}

	} // End Function hexToInteger()

2 Likes

Thank you very much for your reply! Although it is too complicated for me and I don't understand that code... I had hoped there is a Node-Red' way to do that, without much code.

But your time is valuable so thanks again...!

I thought the buffer parser could do that for you. Have you tried different settings for the Type dropdown?

Oh yes, tried a lot of things... Maybe I first need to split up the UDP packet in the separate values first, don't know but I'm not giving up yet...

What do the uint16 settings give?

Will try, but I found the solution after many, many hours of trying and searching.

a reply from @Andrei in this post got me thinking. I needed to rightly divide and then swap the bytes to get the right values.

This is a working flow:

[{"id":"4fd79fd65858d998","type":"debug","z":"588a292e181fe8c9","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":680,"y":560,"wires":[]},{"id":"11489a0252196e01","type":"binary","z":"588a292e181fe8c9","name":"","property":"payload","pattern":"l16 => packet_id, l16 => tx_relay, l32 => packet_1, l32 => packet_2, l32 => packet_3, l32 => packet_4","x":470,"y":560,"wires":[["4fd79fd65858d998"]]},{"id":"afd3a9e3d033d5b5","type":"inject","z":"588a292e181fe8c9","name":"Test UDP packet","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"[130,0,163,0,214,0,0,0,214,1,0,0,76,0,0,0,176,3,0,0]","payloadType":"bin","x":250,"y":560,"wires":[["11489a0252196e01","03610996fc811d7a"]]},{"id":"03610996fc811d7a","type":"debug","z":"588a292e181fe8c9","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":300,"y":640,"wires":[]}]
1 Like

double post

Thank you for your time and directions, I really appreciate that very much.

to be complete, for others maybe who are looking for such a solution, here is the flow...

[{"id":"6ad195b70dd7b0de","type":"tab","label":"Flow 7","disabled":false,"info":"","env":[]},{"id":"03e628dbdbbddd43","type":"udp in","z":"6ad195b70dd7b0de","name":"WeatherDuino UDP","iface":"","port":"9889","ipv":"udp4","multicast":"false","group":"","datatype":"buffer","x":150,"y":160,"wires":[["f5f4b0ac9de7a12b"]]},{"id":"2b858bac73dca7f6","type":"comment","z":"6ad195b70dd7b0de","name":"Explanation of the UDP packet","info":"Decoding a WeatherDuino UDP packet\n\n======================================================================================\nSent out bij the 8266 controller is a 20 byte packet.\nfor example this packet with the Temperature and Humidity values:\n\noutput is only one line of course, values below are formatted and DEC\nvalues are added\n\n  Contents HEX:  82,0, A3,0,   D6,0,0,0,   D6,1,0,0,  4C,0,0,0,    B0,3,0,0\n  Contents DEC: 130,0,163,0,  214,0,0,0,  214,1,0,0,  76,0,0,0,   176,3,0,0\n\nShould result in (this example):\n\nPacket_ID   2 bytes/16bits  = 130 (id 130 = temp and humidity packet)\nTX_Relay    2 bytes/16bits  =  A3 (no hex to dec conversion needed)\nTemp in     4 bytes/32bits  = 214 (/10 = 21,4 C)\nHum in      4 bytes/32bits  = 470 (/10 = 47,0 %RH)\nTemp out    4 bytes/32bits  =  76 (/10 = 7,6 C)\nHum out     4 bytes/32bits  = 944 (/10 = 94,4 %RH)\n \n\n1st 4 bytes >> must be divided into TWO 2 byte values\n\n--- -------- -------- \nDEC      130,       0\nHEX       82,       0\nBIN 10000010,00000000 16bit SWAP is needed\n\n--- -------- --------\nDEC      163,       0\nHEX       A3,       0   \nBIN 10100011,00000000 16bit SWAP is needed\n\n\n2nd 4 bytes / 32bits \n--- -------- -------- -------- --------\nDEC      214,       0,       0,       0\nHEX       D6,       0,       0,       0   \nBIN 11010110,00000000,00000000,00000000 32 bit swap needed\n\n\n3rd 4 bytes \n--- -------- -------- -------- --------\nDEC      214,       1,       0,       0\nHEX       D6,       1,       0,       0   \nBIN 11010110,00000001,00000000,00000000 32 bit swap needed\n\n4th 4 bytes\n--- -------- -------- -------- --------\nDEC      214,       0,       0,       0\nHEX       D6,       0,       0,       0   \nBIN 11010110,00000000,00000000,00000000 32 bits swap needed\n\n5th 4 bytes\n--- -------- -------- -------- --------\nDEC      214,       0,       0,       0\nHEX       D6,       0,       0,       0   \nBIN 11010110,00000000,00000000,00000000 32 bit swap needed\n\n\n","x":180,"y":120,"wires":[]},{"id":"05651916ebb427fb","type":"switch","z":"6ad195b70dd7b0de","name":"","property":"payload.Packet_ID","propertyType":"msg","rules":[{"t":"eq","v":"128","vt":"num"},{"t":"eq","v":"130","vt":"num"},{"t":"eq","v":"132","vt":"num"},{"t":"eq","v":"133","vt":"num"}],"checkall":"true","repair":false,"outputs":4,"x":150,"y":380,"wires":[["2b7f1cf57a438dd3"],["4ffc1c3f537ce39f"],["0582d9c7be326a6a"],["f4963b9f79054891"]]},{"id":"ec74a332f695d16b","type":"debug","z":"6ad195b70dd7b0de","name":"Packet_ID == 128","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":320,"wires":[]},{"id":"197e2c4320ff4bba","type":"debug","z":"6ad195b70dd7b0de","name":"Packet_ID == 130","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":360,"wires":[]},{"id":"d87a5eb753d49f88","type":"debug","z":"6ad195b70dd7b0de","name":"Packet_ID == 132","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":400,"wires":[]},{"id":"f5f4b0ac9de7a12b","type":"buffer-parser","z":"6ad195b70dd7b0de","name":"buffer parser","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16le","name":"Packet_ID","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"Value_1","offset":4,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"Value_2","offset":8,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"Value_3","offset":12,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"Value_4","offset":16,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"swap16","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":true,"setTopic":true,"outputs":1,"x":170,"y":240,"wires":[["05651916ebb427fb"]]},{"id":"8459954a6025a342","type":"inject","z":"6ad195b70dd7b0de","name":"Test UDP packet","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"[132,0,163,0,214,0,0,0,214,1,0,0,76,0,0,0,176,3,0,0]","payloadType":"bin","x":410,"y":160,"wires":[["f5f4b0ac9de7a12b"]]},{"id":"0582d9c7be326a6a","type":"change","z":"6ad195b70dd7b0de","name":"Process 132 - Wind","rules":[{"t":"move","p":"payload.Value_1","pt":"msg","to":"payload.wind_gust","tot":"msg"},{"t":"set","p":"payload.wind_gust","pt":"msg","to":"payload.wind_gust / 1000","tot":"jsonata"},{"t":"move","p":"payload.Value_2","pt":"msg","to":"payload.wind_speed","tot":"msg"},{"t":"set","p":"payload.wind_speed","pt":"msg","to":"payload.wind_speed /1000","tot":"jsonata"},{"t":"move","p":"payload.Value_3","pt":"msg","to":"payload.wind_dir","tot":"msg"},{"t":"set","p":"payload.wind_dir","pt":"msg","to":"$abs(payload.wind_dir /10)","tot":"jsonata"},{"t":"delete","p":"payload.Value_4","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":400,"wires":[["d87a5eb753d49f88"]]},{"id":"ae89d5731ff34f6c","type":"debug","z":"6ad195b70dd7b0de","name":"Packet_ID == 133","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":440,"wires":[]},{"id":"2b7f1cf57a438dd3","type":"change","z":"6ad195b70dd7b0de","name":"Process 128 - Pressure","rules":[{"t":"move","p":"payload.Value_1","pt":"msg","to":"payload.barometer","tot":"msg"},{"t":"set","p":"payload.barometer","pt":"msg","to":"$abs(payload.barometer / 1000)","tot":"jsonata"},{"t":"delete","p":"payload.Value_2","pt":"msg"},{"t":"delete","p":"payload.Value_3","pt":"msg"},{"t":"delete","p":"payload.Value_4","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":320,"wires":[["ec74a332f695d16b"]]},{"id":"4ffc1c3f537ce39f","type":"change","z":"6ad195b70dd7b0de","name":"Process 130 - Temp / Hum","rules":[{"t":"move","p":"payload.Value_1","pt":"msg","to":"payload.temp_inside","tot":"msg"},{"t":"set","p":"payload.temp_inside","pt":"msg","to":"payload.temp_inside / 10","tot":"jsonata"},{"t":"move","p":"payload.Value_2","pt":"msg","to":"payload.humidity_inside","tot":"msg"},{"t":"set","p":"payload.humidity_inside","pt":"msg","to":"payload.humidity_inside / 10","tot":"jsonata"},{"t":"move","p":"payload.Value_3","pt":"msg","to":"payload.temp_outside","tot":"msg"},{"t":"set","p":"payload.temp_outside","pt":"msg","to":"payload.temp_outside /10","tot":"jsonata"},{"t":"move","p":"payload.Value_4","pt":"msg","to":"payload.humidity_outside","tot":"msg"},{"t":"set","p":"payload.humidity_outside","pt":"msg","to":"payload.humidity_outside /10","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":360,"wires":[["197e2c4320ff4bba"]]},{"id":"f4963b9f79054891","type":"change","z":"6ad195b70dd7b0de","name":"Process 133 - Rain","rules":[{"t":"move","p":"payload.Value_1","pt":"msg","to":"payload.rain_conversion_factor","tot":"msg"},{"t":"move","p":"payload.Value_2","pt":"msg","to":"payload.rain_rate","tot":"msg"},{"t":"set","p":"payload.rain_rate","pt":"msg","to":"(payload.rain_rate * payload.rain_conversion_factor) / 10","tot":"jsonata"},{"t":"move","p":"payload.Value_3","pt":"msg","to":"payload.rain_today","tot":"msg"},{"t":"set","p":"payload.rain_today","pt":"msg","to":"(payload.rain_today * payload.rain_conversion_factor) / 1000","tot":"jsonata"},{"t":"delete","p":"payload.Value_4","pt":"msg"},{"t":"delete","p":"payload.rain_conversion_factor","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":440,"wires":[["ae89d5731ff34f6c"]]}]