UTC Conversion using Function

node-red-utc-help-demo.json (3.8 KB)

Convert using function
utc hex to time in this format
2023-3-19 14:20:14

I am able to convert the code
image

after that in the defined format I am unable to do

[
    {
        "id": "f8a89ada4bd4d49b",
        "type": "inject",
        "z": "478d88efdb005c1e",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"msg\":\"dAck\",\"mac\":\"BC572900A05B\",\"rslt\":\"succ\",\"seq\":7716,\"cause\":0,\"rmn\":1,\"dType\":\"hex\",\"data\":\"0200F502000000F0632422491E8A31686324226D1F0D3139632422B020952D7C632422E020C22A6E63242452204B28F46324277020B7292363242EBE2045287563243D341FCC2907632442C61F4A28BD63244DE11ECC287A63245A1C1E4C2851632460591DCB2830632483931D4C26D663248A401CC92626632490351C4925A2632497291BCA25A96324A2981B4A25B96324B2311ACB259A6324D86C1A4B25BE6324E1D319CB25806324EFC8194C25826324F82219B327736324FD9C1A3627A0632501CE1ABA27D26325097A1B33277463250D611BB727CD632510561C412822632512D21CB42858632515C11D34288F63251A961CCB271A\",\"gmac\":\"94A408B0482C\"}",
        "payloadType": "json",
        "x": 170,
        "y": 100,
        "wires": [
            [
                "d6488bee8efe8fdc",
                "55a47454709a309f"
            ]
        ]
    },
    {
        "id": "d6488bee8efe8fdc",
        "type": "function",
        "z": "478d88efdb005c1e",
        "name": "function 24",
        "func": " \n    var newMsg = {};\n  \n \n \n\nvar data_time =  msg.payload.data.substr(16, 8)  ;\nvar Temp = Number(hexTo8p8(msg.payload.data.substr(16+8, 4)));\nvar Humidity = Number(hexTo8p8(msg.payload.data.substr(16 + 8+4, 4)));\nvar data =  msg.payload.data.substr(16, 8);\n\n  for(var i=1;i<=30;i++)\n  {\n      data_time = msg.payload.data.substr(16 * i, 8  );\n      Temp = Number(hexTo8p8(msg.payload.data.substr(24 * i, 4  )));\n      Humidity = Number(hexTo8p8(msg.payload.data.substr(28 * i, 4  )));\n      data = msg.payload.data.substr(16 * i, 8 );\n      \n      var checktime = 1546337502;\n\n      if(checktime>data_time)\n      {\n\n      }\n\n      \n      newMsg.payload = [{\n        \n          time: data_time,\n          utc: Buffer.from(data_time, 'hex').toString(),\n \n          temp: Temp,\n          humidity: Humidity,\n       \n      },\n      {\n\n          history: 1 \n   \n\n\n\n      }];\n      node.send(newMsg);\n\n \n  }  \n\n\n \n \n     \n        \n   \n \n\n\nreturn null;\n\nfunction calculateDistance(rssi) {\n    let P = -69; // @TODO This value should come from MQTT message\n    let n = 3;\n    let d = Math.pow(10, ((P - rssi) / (10 * n))); //(n ranges from 2 to 4)\n    return d * 2;\n}\n\n\nfunction hexTo8p8(/** @type {string} */ value) {\n    let intValue = parseInt(value, 16);\n    let signed = (intValue & 0x8000) > 0 ? -1/10 : 1;\n    return signed * intValue / Math.pow(2, 8);\n}\n\n\n\nfunction int2float( /** @type {string} */value_int) {\n    // Create new Buffer based on array bytes\n    const buf = Buffer.from(value_int);\n    // swap the words\n    const temp0 = buf[0];\n    const temp1 = buf[1];\n    buf[0] = buf[2];\n    buf[1] = buf[3];\n    buf[2] = temp0;\n    buf[3] = temp1;\n\n    // Represent these bytes as 32-bit unsigned int\n    const value = buf.readInt32BE();\n\n    // save the value\n    msg.payload = value;\n\n    return msg;\n}\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 410,
        "y": 160,
        "wires": [
            [
                "caf2f9fe83302084"
            ]
        ]
    },
    {
        "id": "caf2f9fe83302084",
        "type": "debug",
        "z": "478d88efdb005c1e",
        "name": "debug 336",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 610,
        "y": 180,
        "wires": []
    }
]

Only to put it out there:
There is a moment node that does this for you.

how can i use the moment node in my function ,

newMsg.payload = [{

      time: data_time,
      utc: Buffer.from(data_time, 'hex').toString() **<--- Moment code eg ??**

      temp: Temp,
      humidity: Humidity,
   
  },

Well, rather than IN your function node:
Just before it when the message is arriving and the time is.... in .... the message.
Stick the moment node in and get it to return human readable time.

Then send the message to your node and don't bother doing the time stuff.
The time would be already human readable.

Just a thought.

OK, sorry.

the time isn't in the message.

Stick a timestamp into the message (change node. Set a message part to timestamp)
Send it through the moment node then into your function node.
(I can't/don't always read things and sometimes the parsing isn't the best.)

image
I am getting in UTC hex

image

and I have to parse the data from hex then convert in 8.8 floating point format then insert into time series

better be able to do inside my function

Ok, sorry.

I kinda jumped in there/here feet first.

Seems a weird standard for time. But ....

I hope someone else can help you.

Do you know how to PASTE the exact/actual message here?
(formatting, etc)

https://discourse.nodered.org/uploads/short-url/eCqS5mp23z2vcyKaLtWTxnVWTdM.json

import this

Oh sorry. You posted it at the top as an attachment.

You can paste code here.

Look at/in the top of the EDIT window.

The </> button.

(Or press the back tick button 3 times) The one just to the left of the 1 key.
Paste the code.
Press enter.
Repeat the 3 back ticks
press enter.

It should look something like:

This is where you code is.

Oh when you EXPORT the code, click on the JSON button and down the bottom select COMPACT.

[{"id":"f8a89ada4bd4d49b","type":"inject","z":"478d88efdb005c1e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"msg\":\"dAck\",\"mac\":\"BC572900A05B\",\"rslt\":\"succ\",\"seq\":7716,\"cause\":0,\"rmn\":1,\"dType\":\"hex\",\"data\":\"0200F502000000F0632422491E8A31686324226D1F0D3139632422B020952D7C632422E020C22A6E63242452204B28F46324277020B7292363242EBE2045287563243D341FCC2907632442C61F4A28BD63244DE11ECC287A63245A1C1E4C2851632460591DCB2830632483931D4C26D663248A401CC92626632490351C4925A2632497291BCA25A96324A2981B4A25B96324B2311ACB259A6324D86C1A4B25BE6324E1D319CB25806324EFC8194C25826324F82219B327736324FD9C1A3627A0632501CE1ABA27D26325097A1B33277463250D611BB727CD632510561C412822632512D21CB42858632515C11D34288F63251A961CCB271A\",\"gmac\":\"94A408B0482C\"}","payloadType":"json","x":170,"y":100,"wires":[["d6488bee8efe8fdc","55a47454709a309f"]]},{"id":"d6488bee8efe8fdc","type":"function","z":"478d88efdb005c1e","name":"function 24","func":" \n    var newMsg = {};\n  \n \n \n\nvar data_time =  msg.payload.data.substr(16, 8)  ;\nvar Temp = Number(hexTo8p8(msg.payload.data.substr(16+8, 4)));\nvar Humidity = Number(hexTo8p8(msg.payload.data.substr(16 + 8+4, 4)));\nvar data =  msg.payload.data.substr(16, 8);\n\n  for(var i=1;i<=30;i++)\n  {\n      data_time = msg.payload.data.substr(16 * i, 8  );\n      Temp = Number(hexTo8p8(msg.payload.data.substr(24 * i, 4  )));\n      Humidity = Number(hexTo8p8(msg.payload.data.substr(28 * i, 4  )));\n      data = msg.payload.data.substr(16 * i, 8 );\n      \n      var checktime = 1546337502;\n      if(checktime>data_time)\n      {\n\n      }\n\n      \n      newMsg.payload = [{\n        \n          time: data_time,\n          utc:   \"\",\n \n          temp: Temp,\n          humidity: Humidity,\n       \n      },\n      {\n\n          history: 1 \n   \n\n\n\n      }];\n      node.send(newMsg);\n\n \n  }  \n\n\n \n \n     \n        \n   \n \n\n\nreturn null;\n\nfunction calculateDistance(rssi) {\n    let P = -69; // @TODO This value should come from MQTT message\n    let n = 3;\n    let d = Math.pow(10, ((P - rssi) / (10 * n))); //(n ranges from 2 to 4)\n    return d * 2;\n}\n\n\nfunction hexTo8p8(/** @type {string} */ value) {\n    let intValue = parseInt(value, 16);\n    let signed = (intValue & 0x8000) > 0 ? -1/10 : 1;\n    return signed * intValue / Math.pow(2, 8);\n}\n\n\n\nfunction int2float( /** @type {string} */value_int) {\n    // Create new Buffer based on array bytes\n    const buf = Buffer.from(value_int);\n    // swap the words\n    const temp0 = buf[0];\n    const temp1 = buf[1];\n    buf[0] = buf[2];\n    buf[1] = buf[3];\n    buf[2] = temp0;\n    buf[3] = temp1;\n\n    // Represent these bytes as 32-bit unsigned int\n    const value = buf.readInt32BE();\n\n    // save the value\n    msg.payload = value;\n\n    return msg;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":160,"wires":[["caf2f9fe83302084"]]},{"id":"caf2f9fe83302084","type":"debug","z":"478d88efdb005c1e","name":"debug 336","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":180,"wires":[]}]

Ok,
It does work.

Could you please install the moment node?
(Or: Do you have the moment mode installed already?
node-red-contrib-moment

And do you know how to import flows from other people?

yes installed

yes i can import

Sorry. I just didn't want to presume.

Try this.

[{"id":"f8a89ada4bd4d49b","type":"inject","z":"82a373101348c1cf","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"msg\":\"dAck\",\"mac\":\"BC572900A05B\",\"rslt\":\"succ\",\"seq\":7716,\"cause\":0,\"rmn\":1,\"dType\":\"hex\",\"data\":\"0200F502000000F0632422491E8A31686324226D1F0D3139632422B020952D7C632422E020C22A6E63242452204B28F46324277020B7292363242EBE2045287563243D341FCC2907632442C61F4A28BD63244DE11ECC287A63245A1C1E4C2851632460591DCB2830632483931D4C26D663248A401CC92626632490351C4925A2632497291BCA25A96324A2981B4A25B96324B2311ACB259A6324D86C1A4B25BE6324E1D319CB25806324EFC8194C25826324F82219B327736324FD9C1A3627A0632501CE1ABA27D26325097A1B33277463250D611BB727CD632510561C412822632512D21CB42858632515C11D34288F63251A961CCB271A\",\"gmac\":\"94A408B0482C\"}","payloadType":"json","x":370,"y":1000,"wires":[["d6488bee8efe8fdc","d1c33f17d410f135"]]},{"id":"d1c33f17d410f135","type":"moment","z":"82a373101348c1cf","name":"","topic":"","input":"data","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"","locale":"en-AU","output":"data","outputType":"msg","outTz":"Australia/Sydney","x":560,"y":1050,"wires":[["cb46a9925f7a74ac"]]},{"id":"cb46a9925f7a74ac","type":"debug","z":"82a373101348c1cf","name":"debug 337","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":1050,"wires":[]}]

This is JUST TO GET YOU STARTED.

You will have to put your function node after this and do what I said.
But I am not sure that is the correct date being shown.

the function contains instructions to chop data and convert from hex
in the next step time conversion needs to take place .

Yes, but now the time (is that the correct time?) is now in msg.data and so you can write the code in the function node a lot easier than trying to decode the original value.

I guess.

The parsing needs to be inside , you have attached a timestamp in the outer block on json.
this is not what i was asking

Try

let UTC_ISO = new Date(parseInt(msg.payload.data.slice(16, 24), 16) * 1000).toISOString();    
msg.payload = UTC_ISO.split(/\.|T/).slice(0,2).join(" ");
return msg;

output
2022-09-16 07:14:17

[edit] removed substr() as deprecated, used slice() instead.

Then it is beyond my skills.
Though I also fear you are not seeing the advantage of what I am saying.

I got the right time - I think. But I am NOT in your timezone. So I am guessing that it did convert the original HUGE number to human readable.

Good luck.

WAY above my skills.