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":""}]