Timestamp converting

Hello,

I'm new to this forum so i dont know where to put this Topic.

I have a problem. I'm trying to publish 2 variables from mqtt with corrensponding date and time into a CSV file.

But the date and time i'm given with the new.Date().toString()
is in the notation: Wed Mar 31 2021 15:36:06. And i want it in another notation.

Then if i'm using the Date/time Formatter node i transfer this time to the correct time notation : 31.03.2021 15:36.

But how do i get this back into the last string with the variables in it, without overwriting the complete string.

I'm not sure I understand this, can you explain it some more?
Are you saying you want both the Wed Mar 31 2021 15:36:06 and the 31.03.2021 15:36 format? If so, put them in two different msg variables like msg.longDate and msg.numericDate

No i'd like to have the 31.03.2021 15:36 format.
And this has to be in the msg. send to the CSV file

Why don't you do it in the function node using javascript? Do a google search on 'javascript date conversion'

Is this part of a class assignment?

Do you mean you want to add a formatted time string alongside the sensor values to be written in your CSV?

What is the msg.payload output from your join node? If it is a key/value object, then simply add a property to msg.payload then it will travel to the CSV node alongside your other values.

If it is something different, share your flow and show us what comes out of the join node and what comes out of the function node.

I want to have the 2 values (PH, temp) extracted from mqtt in a CSV file
These values will be extracted from mqtt at the same time cause of the arduino code linked to this.

When these 2 values get published to the CSV file i want the current time and date exported with them.

My code:

[{"id":"ca5b1816.517648","type":"mqtt in","z":"bde2953.9de2c68","name":"PH waarde","topic":"vijver/ph","qos":"2","datatype":"json","broker":"93299d75.6190a","x":100,"y":620,"wires":[["31c9f087.3438f"]]},{"id":"1200210c.c6833f","type":"mqtt in","z":"bde2953.9de2c68","name":"Temp waarde","topic":"vijver/temp","qos":"2","datatype":"json","broker":"93299d75.6190a","x":110,"y":680,"wires":[["7b8e0037.f117a"]]},{"id":"15ba4b3a.fa8815","type":"file","z":"bde2953.9de2c68","name":"","filename":"/share/test.csv","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":960,"y":640,"wires":[[]]},{"id":"ce878c18.15589","type":"join","z":"bde2953.9de2c68","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":450,"y":640,"wires":[["b52021a0.03338"]]},{"id":"31c9f087.3438f","type":"change","z":"bde2953.9de2c68","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":620,"wires":[["ce878c18.15589"]]},{"id":"7b8e0037.f117a","type":"change","z":"bde2953.9de2c68","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":680,"wires":[["ce878c18.15589"]]},{"id":"b52021a0.03338","type":"function","z":"bde2953.9de2c68","name":"Name of database + timestamp","func":"msg.filename = \"/share/test.txt\";\nvar time = new Date().toString().replace(\" GMT+0200 (Central European Summer Time)\", \"\");\nvar x = msg.payload[0];\nvar y = msg.payload[1];\nmsg.payload = time + ';' + x + ';PH' + ';' + y + \";Graden\" ;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":690,"y":640,"wires":[["15ba4b3a.fa8815","55ae35c3.d6d0ac"]]},{"id":"27d23756.1fe1c8","type":"function","z":"bde2953.9de2c68","name":"Name of database + timestamp","func":"var time = new Date().toString().replace(\"Wed\", \"\").replace(\" GMT+0200 (Central European Summer Time)\", \"\");\nmsg.payload = time;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":760,"wires":[["fc6a4f83.2ede8"]]},{"id":"fc6a4f83.2ede8","type":"date-converter","z":"bde2953.9de2c68","name":"","topic":"","input":"payload","inputType":"msg","inTz":"","adjAmount":0,"adjType":"days","adjDir":"add","format":"DD.MM.YYYY HH:mm","locale":"","output":"payload","outputType":"msg","outTz":"","x":760,"y":760,"wires":[["55ae35c3.d6d0ac"],[]]},{"id":"b74db90d.f223a8","type":"inject","z":"bde2953.9de2c68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":760,"wires":[["27d23756.1fe1c8"]]},{"id":"55ae35c3.d6d0ac","type":"debug","z":"bde2953.9de2c68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1000,"y":720,"wires":[]},{"id":"93299d75.6190a","type":"mqtt-broker","name":"","broker":"192.168.1.173","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Try formating the date with moments using JSONata expression in a change node. Also you are joining with an array, depending on which message is received first your PH and Temp may be reversed. Try joining as an object
e.g.

[{"id":"ce878c18.15589","type":"join","z":"5a245aa1.510164","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"parts","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":420,"y":3520,"wires":[["26df3b60.9edf54"]]},{"id":"31c9f087.3438f","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"ph","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":3500,"wires":[["ce878c18.15589"]]},{"id":"7b8e0037.f117a","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"temp","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":3560,"wires":[["ce878c18.15589"]]},{"id":"26df3b60.9edf54","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"payload.time","pt":"msg","to":"$moment().tz(\"Europe/london\").format(\"DD.MM.YYYY HH:mm\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":3580,"wires":[["b52021a0.03338"]]},{"id":"6904e44e.8fbc84","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":90,"y":3500,"wires":[["31c9f087.3438f"]]},{"id":"8dda8b50.f07a8","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"7","payloadType":"num","x":90,"y":3560,"wires":[["7b8e0037.f117a"]]},{"id":"b52021a0.03338","type":"function","z":"5a245aa1.510164","name":"Name of database + timestamp","func":"msg.filename = \"/share/test.txt\";\nvar time = msg.payload.time;\nvar y = msg.payload.ph\nvar x = msg.payload.temp;\nmsg.payload = time + ';' + x + ';PH' + ';' + y + \";Graden\" ;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":3540,"wires":[["55ae35c3.d6d0ac"]]},{"id":"55ae35c3.d6d0ac","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":970,"y":3600,"wires":[]}]
1 Like

Thank you, this solution works great!!

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