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


Log in to reply
 

Suggested Topics

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts