Storage Write file node insert timestamp

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?

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.

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

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 ?

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

1 Like

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>

$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

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

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.

1 Like

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