Injecting a Timestamp in Node Red
-
I'm wondering if anyone has been able to use node red to inject a timestamp and join it to each incoming message from a serial gateway. My end goal is to output a .csv file with timestamped sensor values, so I want to attach a timestamp immediately upon receiving each message.
I am having trouble with the msg. type that is required for the join node, as well as being able to trigger the inject timestamp node upon receiving a message from the serial port.Thanks in advance!
Joel
-
Well, I'm not using a serial gateway but this flow adds a timestamp to a mqtt message and logs it.
You can tune this to your needs.[{"id":"844d0a60.59e128","type":"function","z":"850b8bf6.c34fe8","name":"Add timestamp","func":"msg.payload = new Date().toLocaleString() + '.' + new Date().getMilliseconds() + ', topic, ' + msg.topic + ', payload: ' + msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":580,"wires":[["bef9e569.e5f048"]]},{"id":"f2c362e0.a3bf4","type":"mqtt in","z":"850b8bf6.c34fe8","name":"","topic":"mygateway1-out/#","qos":"2","broker":"5f4cad33.82b5b4","x":150,"y":580,"wires":[["844d0a60.59e128"]]},{"id":"bef9e569.e5f048","type":"file","z":"850b8bf6.c34fe8","name":"","filename":"/home/pi/mysensors.log","appendNewline":true,"createDir":false,"overwriteFile":"false","x":550,"y":620,"wires":[[]]},{"id":"5f4cad33.82b5b4","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]
By the way, how are you using the serial gateway? I've tried it before but couldn't send and receive messages at the same time as the serial port was in use by either the sending or the receiving flow.
-
Thanks! I actually figured out the javascript and made a function exactly like this one. I am using the serial port just to receive data currently, so i only have one flow. I am using it with relatively default settings:
[{"id":"7fc64f75.67286","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"1f992795.acb908","type":"serial in","z":"7fc64f75.67286","name":"Seral Port","serial":"d11eb944.68b128","x":60,"y":620,"wires":[["19c6b2bb.f878dd"]]},{"id":"19c6b2bb.f878dd","type":"function","z":"7fc64f75.67286","name":"Get_Datetime","func":"var now = new Date()\nvar x = now.toLocaleString()\n\n\nmsg.datetime = x\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":260,"y":280,"wires":[["1197d270.3caa9e"]]},{"id":"1197d270.3caa9e","type":"function","z":"7fc64f75.67286","name":"Merge_datetime and payload","func":"msg.payload = msg.datetime+ ' ;' + msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":280,"wires":[["e44f08e9.aae3f8"]]},{"id":"e44f08e9.aae3f8","type":"file","z":"7fc64f75.67286","name":"","filename":"/home/pi/Desktop/dataNodered_nov5.csv","appendNewline":false,"createDir":false,"overwriteFile":"false","x":840,"y":280,"wires":[[]]},{"id":"d11eb944.68b128","type":"serial-port","z":"","serialport":"/dev/ttyACM0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":false,"responsetimeout":"10000"}]
I am about to begin to look into sending my nodes data. I want to find a way for them to sync up their data transmissions, and will will be looking into how to send them a variable with a customized sleep time in order to align their transmissions. How have you send data to the nodes via node red in the past? Will I run into trouble once i create a second data flow?
-
I'm using a mqtt gateway to send data to the nodes. It is possible you will run into the same issues I guess but that should be easy to test. Just send data on a regular basis to the out node and try to receive at the same time.
I use the VAR_1 type in the message of I'm sending data from node red to a node
Suggested Topics
-
Join the Project Contest 2014 - ENDED
Announcements • 8 Apr 2014, 08:03 • hek 13 May 2014, 21:48 -
Howto use myscontroller from node-red-contrib-mysensors?
Node-RED • 9 Mar 2019, 21:26 • pansen 17 Mar 2019, 22:37 -
multiple gateways on lan
Node-RED • 8 Jun 2019, 07:57 • sotiriszegiannis 10 Jun 2019, 20:20 -
node-red-contrib-mysensors release thread
Node-RED • 24 Apr 2018, 20:06 • tbowmo 4 May 2021, 10:25 -
Dollhouse project : need Node-Red Controller flow
Node-RED • 20 Oct 2020, 07:35 • SuperNinja 20 Oct 2020, 10:33 -
Thanks for node-red-contrib-mysensors and a question regarding collections or arrays
Node-RED • 25 Oct 2020, 07:44 • Joost 25 Oct 2020, 08:36