[newbie] changing a date time format

I made a flow which reads out the temperature from a sensor and writes the temperature together with a timestamp in a file. I'm struggling however with the time-date format:
Currently I get data in a tab separated format like this:

09/20/2020, 14:45:53	24:00	/esp001/DHT11/Temperature

I'd rather have something like this:

2020-09-20 14:45:53	24:00	/esp001/DHT11/Temperature

I could probably get this right with string splitting and some substitutions, but I think there must be a
more elegant solution.

Here is my flow with a a node which injects the same data as the sensor produces:

[{"id":"e5bf915a.655b4","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"9fe662a.766ad2","type":"mqtt in","z":"e5bf915a.655b4","name":"/esp001/DHT11/Temperature","topic":"/esp001/DHT11/Temperature","qos":"2","datatype":"auto","broker":"e734c2d5.0259a","x":300,"y":80,"wires":[["7732c15f.af476"]]},{"id":"7732c15f.af476","type":"function","z":"e5bf915a.655b4","name":"removenan","func":"function removenan(x) {\n  if (isNaN(x)) {\n    ;\n  }\n  return x;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":730,"y":80,"wires":[["620f78fd.663ca"]]},{"id":"88f65b87.e70f78","type":"file","z":"e5bf915a.655b4","name":"","filename":"/data/temp.dat","appendNewline":false,"createDir":true,"overwriteFile":"false","encoding":"none","x":900,"y":220,"wires":[[]]},{"id":"620f78fd.663ca","type":"function","z":"e5bf915a.655b4","name":"Modify payload","func":"var d = new Date();\nvar t = d.toLocaleTimeString([],\n{   day: \"2-digit\",\n    month: \"2-digit\",\n    year: \"numeric\",\n    hour: \"2-digit\",\n    minute: \"2-digit\",\n    second: \"2-digit\",\n    hour12: false\n});\npayload={\"time\":t,\"payload\":msg.payload,\"topic\":msg.topic};\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":220,"wires":[["9ba16b3c.e58b88"]]},{"id":"9ba16b3c.e58b88","type":"csv","z":"e5bf915a.655b4","name":"tsv","sep":"\\t","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":750,"y":220,"wires":[["88f65b87.e70f78"]]},{"id":"b665c9d3.fad22","type":"inject","z":"e5bf915a.655b4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"/esp001/DHT11/Temperature","payload":"24:00","payloadType":"str","x":260,"y":160,"wires":[["7732c15f.af476"]]},{"id":"e734c2d5.0259a","type":"mqtt-broker","z":"","name":"mosquitto","broker":"mosquitto","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

...maybe you look into node-contrib-monement: https://flows.nodered.org/node/node-red-contrib-moment
With it, you can convert timestamps into many formats.
It is based on moment.js https://momentjs.com/docs/ and from within a function node, you can do even more with it.

2 Likes

Hi Hugo,

You could before sending the mqtt data to your function, you can use @TotallyInformation moment node that can easily manipulate date and time and makes life much easier.

Example (change the timezone accordingly)

[{"id":"5612ec30.99fd5c","type":"inject","z":"e2d15202.6f33c","name":"mqtt","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"data from mqtt","payloadType":"str","x":270,"y":300,"wires":[["f70001ee.4d4d98"]]},{"id":"a7ad22ac.4e747","type":"debug","z":"e2d15202.6f33c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":300,"wires":[]},{"id":"f70001ee.4d4d98","type":"moment","z":"e2d15202.6f33c","name":"","topic":"","input":"","inputType":"date","inTz":"Europe/Athens","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm:ss","locale":"en_GB","output":"datetime","outputType":"msg","outTz":"Europe/Athens","x":580,"y":300,"wires":[["a7ad22ac.4e747"]]}]
1 Like

beat me to it .. great minds think alike :wink:

2 Likes

After installing node-red-contrib-moment node, you will get what you want:

[{"id":"80510a8c.875658","type":"mqtt in","z":"12fea260.00a55e","name":"/esp001/DHT11/Temperature","topic":"/esp001/DHT11/Temperature","qos":"2","datatype":"auto","broker":"19c0c7f7.7c0dd8","x":1360,"y":100,"wires":[["ccc02c12.2e53"]]},{"id":"ccc02c12.2e53","type":"function","z":"12fea260.00a55e","name":"removenan","func":"function removenan(x) {\n  if (isNaN(x)) {\n    ;\n  }\n  return x;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1790,"y":100,"wires":[["f382e2cb.8dcff"]]},{"id":"21eea6db.dd5dca","type":"file","z":"12fea260.00a55e","name":"","filename":"/data/temp.dat","appendNewline":false,"createDir":true,"overwriteFile":"false","encoding":"none","x":1960,"y":240,"wires":[[]]},{"id":"faa0922f.223c6","type":"function","z":"12fea260.00a55e","name":"Modify payload","func":"/*\nvar d = new Date();\nvar t = d.toLocaleTimeString([],\n{   day: \"2-digit\",\n    month: \"2-digit\",\n    year: \"numeric\",\n    hour: \"2-digit\",\n    minute: \"2-digit\",\n    second: \"2-digit\",\n    hour12: false\n});\n\npayload={\"time\":t,\"payload\":msg.payload,\"topic\":msg.topic};\n*/\n\npayload={\"time\":msg.now,\"payload\":msg.payload,\"topic\":msg.topic};\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1640,"y":240,"wires":[["e0bb1878.09c718","ae8710ee.4462c"]]},{"id":"e0bb1878.09c718","type":"csv","z":"12fea260.00a55e","name":"tsv","sep":"\\t","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":1810,"y":240,"wires":[["21eea6db.dd5dca"]]},{"id":"38c51046.84694","type":"inject","z":"12fea260.00a55e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"/esp001/DHT11/Temperature","payload":"24:00","payloadType":"str","x":1320,"y":180,"wires":[["ccc02c12.2e53"]]},{"id":"ae8710ee.4462c","type":"debug","z":"12fea260.00a55e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1830,"y":300,"wires":[]},{"id":"f382e2cb.8dcff","type":"moment","z":"12fea260.00a55e","name":"input: msg.payload","topic":"","input":"now","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm:ss","locale":"en_US","output":"now","outputType":"msg","outTz":"America/New_York","x":1750,"y":160,"wires":[["faa0922f.223c6"]]},{"id":"19c0c7f7.7c0dd8","type":"mqtt-broker","z":"","name":"mosquitto","broker":"mosquitto","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
1 Like

Are you writing the timestamp out for humans to read or for some other purpose? I note you haven't included the timezone, so unless it is UTC strange things may happen to it at the DST changeover.

I use the data for processing with Gnuplot. I entered the correct time zone in the moment-node.

kind regards,
Hugo

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