Wanna add timestamp to payload

Hey guys,

I wanna write some mqtt messages to a txt-file for troubleshooting purposes of an IOT device.
So, I achieved the goal to write these messages to a file.
But now I need some kind of function which is adding a timestamp to each mqtt payload at the moment I append it to the txt file.
Can anybody help me? I am not good in programming :frowning:

All I have is this flow without a function.

[{"id":"f4addc3f5fae43b9","type":"tab","label":"Imported mqtt logger","disabled":false,"info":"","env":[]},{"id":"dc010139.a13ea","type":"mqtt in","z":"f4addc3f5fae43b9","name":"","topic":"test/feeder","qos":"0","datatype":"auto","broker":"f839540e.7b7858","nl":false,"rap":false,"inputs":0,"x":240,"y":200,"wires":[["49d9bb43f0e8e692"]]},{"id":"d86a58b1c2b33f4d","type":"debug","z":"f4addc3f5fae43b9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","statusVal":"","statusType":"auto","x":730,"y":200,"wires":[]},{"id":"49d9bb43f0e8e692","type":"file","z":"f4addc3f5fae43b9","name":"","filename":"/data/logs/feederdebug.txt","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":490,"y":200,"wires":[["d86a58b1c2b33f4d"]]},{"id":"f839540e.7b7858","type":"mqtt-broker","name":"","broker":"192.168.178.44","port":"1883","clientid":"clientid","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

One way to do it is add a change node after the mqtt-in node. Here is a change node that will do it for you:

[{"id":"870ebd847bf6503b","type":"change","z":"f4addc3f5fae43b9","name":"","rules":[{"t":"set","p":"data_in","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"time","pt":"msg","to":"","tot":"date"},{"t":"set","p":"payload","pt":"msg","to":"time - data","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"time","fromt":"str","to":"time","tot":"msg"},{"t":"change","p":"payload","pt":"msg","from":"data","fromt":"str","to":"data_in","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":180,"wires":[["49d9bb43f0e8e692","6c0a941781332b5a"]]}]
1 Like

Thank you very much. That is working but the format of the timestamp is not my favourite.
Now i use a function. I will post it later

A no-code way of getting customisable timestamp using node-red-contrib-simpletime

3 Likes

thanks a lot for that many replies.

that's the way I did it:

msg.payload = msg.payload + " - " + new Date().toISOString();

return msg;
1 Like

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