# Storage Write file node insert timestamp

**URL:** <https://discourse.nodered.org/t/storage-write-file-node-insert-timestamp/57491>\
**Category:** General\
**Created:** [30 January 2022 10:03 UTC](https://discourse.nodered.org/t/storage-write-file-node-insert-timestamp/57491 "2022-01-30T10:03:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![x\_dadu](https://avatars.discourse-cdn.com/v4/letter/x/47e85d/32.png) [@x\_dadu](https://discourse.nodered.org/u/x_dadu)\
**Post date:** [30 January 2022 10:03 UTC](https://discourse.nodered.org/t/storage-write-file-node-insert-timestamp/57491/1 "2022-01-30T10:03:24Z")

</div>

I am a complete beginner in node-red and MQTT. I have a Pi zero 2 W as MQTT broker and node-red platform, and several temperature sensors that send data to the broker. For one of them I attached a write file node which when edited I added only the full path to the file. The file contains just temperature values, text as:  
56.3  
56.3  
56.2  
56.2  
56.1  
etc.  
How do I write the time to receive the message in front of each temperature value?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [30 January 2022 10:18 UTC](https://discourse.nodered.org/t/storage-write-file-node-insert-timestamp/57491/2 "2022-01-30T10:18:33Z")

</div>

There are may ways. Here is an example using the template node. It uses a timestamp, but you could convert that to a human readable if you wished.  
e.g.

```auto
[{"id":"487bb69.cdeb548","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"55.6","payloadType":"num","x":170,"y":220,"wires":[["5c0e3ccb.c0d2dc"]]},{"id":"5c0e3ccb.c0d2dc","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"timestamp","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":220,"wires":[["a76ea31e.4020b"]]},{"id":"a76ea31e.4020b","type":"template","z":"c791cbc0.84f648","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{timestamp}},{{payload}}","output":"str","x":560,"y":240,"wires":[["dab1571e.83d498","b8b5474a.f70428"]]},{"id":"dab1571e.83d498","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":220,"wires":[]},{"id":"b8b5474a.f70428","type":"file","z":"c791cbc0.84f648","name":"","filename":"path/to/file.ext","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":680,"y":280,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![x\_dadu](https://avatars.discourse-cdn.com/v4/letter/x/47e85d/32.png) [@x\_dadu](https://discourse.nodered.org/u/x_dadu)\
**Post date:** [30 January 2022 10:38 UTC](https://discourse.nodered.org/t/storage-write-file-node-insert-timestamp/57491/3 "2022-01-30T10:38:23Z")

</div>

thank you for response.  
Now I heave data:  
1643538949535,52.3  
1643538955715,52.3  
1643538965719,52.3  
1643538975720,52.3  
What format is that time ?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [30 January 2022 11:17 UTC](https://discourse.nodered.org/t/storage-write-file-node-insert-timestamp/57491/4 "2022-01-30T11:17:25Z")

</div>

it is a timestamp in milliseconds since 1/1/1970 at 00:00 hours.

---

<div class="post-metadata">

**Author:** ![x\_dadu](https://avatars.discourse-cdn.com/v4/letter/x/47e85d/32.png) [@x\_dadu](https://discourse.nodered.org/u/x_dadu)\
**Post date:** [30 January 2022 13:41 UTC](https://discourse.nodered.org/t/storage-write-file-node-insert-timestamp/57491/5 "2022-01-30T13:41:45Z")

</div>

I find on forum another way, I use a change node with $now() expression. I find also how to format the output data of time, so mai function is\>

```auto
$now('[H01]:[m01]:[s01]-[D01].[M01].[Y0001]')

```

a temperature value of 55.1 Celsius is printing:  
13:39:40-30.01.2022-55.1  
The only problem is the timezone is wrong, My place is GMT+2, so real time is 15:39.  
How to insert time zone parameter in $now expression ?

LE find it, excuse me for rushing

```auto
$now('[H01]:[m01]:[s01]-[D01].[M01].[Y0001]','+0200')

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [30 January 2022 13:52 UTC](https://discourse.nodered.org/t/storage-write-file-node-insert-timestamp/57491/6 "2022-01-30T13:52:06Z")

</div>

You should store all times as UTC which is what the timestamp and $now() output. I would not bother formatting $now, as both the ISO output of time and the timestamp in milliseconds are both good formats for future processing.

When you store as a UTC time, you can then when displaying format for your timezone.

So my advice is to use the milliseconds (1519211811670) or the IS0 ("2022-01-30T12:31:19.000Z"), as you will find it causes less issue further down the road.

---

<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:** [31 March 2022 13:52 UTC](https://discourse.nodered.org/t/storage-write-file-node-insert-timestamp/57491/7 "2022-03-31T13:52:46Z")

</div>

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