# Wanna add timestamp to payload

**URL:** <https://discourse.nodered.org/t/wanna-add-timestamp-to-payload/66031>\
**Category:** General\
**Created:** [4 August 2022 18:32 UTC](https://discourse.nodered.org/t/wanna-add-timestamp-to-payload/66031 "2022-08-04T18:32:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![trombose009](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trombose009/32/30787_2.png) [@trombose009](https://discourse.nodered.org/u/trombose009)\
**Post date:** [4 August 2022 18:32 UTC](https://discourse.nodered.org/t/wanna-add-timestamp-to-payload/66031/1 "2022-08-04T18:32:52Z")

</div>

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 ☹

All I have is this flow without a function.

 ![Unbenannt](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/08dc8de4d370e11c83909869fdc0f0d95a4786eb.png)

```auto
[{"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":""}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [4 August 2022 18:55 UTC](https://discourse.nodered.org/t/wanna-add-timestamp-to-payload/66031/2 "2022-08-04T18:55:43Z")

</div>

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:

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![trombose009](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trombose009/32/30787_2.png) [@trombose009](https://discourse.nodered.org/u/trombose009)\
**Post date:** [5 August 2022 06:00 UTC](https://discourse.nodered.org/t/wanna-add-timestamp-to-payload/66031/3 "2022-08-05T06:00:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [5 August 2022 07:33 UTC](https://discourse.nodered.org/t/wanna-add-timestamp-to-payload/66031/4 "2022-08-05T07:33:51Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/d/8d3fa4eaf05c699808caafb1d6f571c6934daaec.png)

---

<div class="post-metadata">

**Author:** ![trombose009](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trombose009/32/30787_2.png) [@trombose009](https://discourse.nodered.org/u/trombose009)\
**Post date:** [5 August 2022 10:52 UTC](https://discourse.nodered.org/t/wanna-add-timestamp-to-payload/66031/5 "2022-08-05T10:52:28Z")

</div>

thanks a lot for that many replies.

that's the way I did it:

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

return msg;

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 August 2022 10:52 UTC](https://discourse.nodered.org/t/wanna-add-timestamp-to-payload/66031/6 "2022-08-19T10:52:32Z")

</div>

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