[Help] - How to save 2 timestamps in a single sequence

Hello kind people :slight_smile:

I am currently working with Node Red and Home Assistant,

My main goal is to have the time I leave home and the time I arrive at the office recorded, (I am trying to find out the average time for the commute)

When this particular sequence has finished, both the time-stamps I've set have returned the same time,


I guess the reason is that I don't know how to save the "Leave time". I believe it is important for both timestamps (leave and arrive) to be sent together to "sheets append", otherwise they will be added to different rows

I would love to hear ideas on how to make this work, (please let me know if more information is needed)


Its hard to say as you supply only an image. Save the differnt times in there own property then at end use the saved property values to calculate the result.
Here is an example, i have use change node as it has moments and i do not have moment node installed, I added a random delay to simulate travel.

[{"id":"b869689a.4c54f8","type":"inject","z":"c791cbc0.84f648","name":"start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"start","payload":"{}","payloadType":"json","x":90,"y":340,"wires":[["1c61daff.811165"]]},{"id":"1c61daff.811165","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload.start","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":340,"wires":[["55b9723d.25d9a4"]]},{"id":"55b9723d.25d9a4","type":"delay","z":"c791cbc0.84f648","name":"","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"5","randomLast":"10","randomUnits":"seconds","drop":false,"x":500,"y":340,"wires":[["96250956.788eb8"]]},{"id":"96250956.788eb8","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload.arrive","pt":"msg","to":"","tot":"date"},{"t":"set","p":"payload.duration","pt":"msg","to":"$moment($$.payload.arrive-payload.start).tz(\"UTC\").format(\"HH:mm:ss.SSS\")","tot":"jsonata"},{"t":"set","p":"payload.start","pt":"msg","to":"$split($moment($$.payload.start).tz(\"Europe/London\").format(\"YYYY-MM-DD HH:mm:ss\"),\" \")","tot":"jsonata"},{"t":"set","p":"payload.arrive","pt":"msg","to":"$split($moment($$.payload.arrive).tz(\"Europe/London\").format(\"YYYY-MM-DD HH:mm:ss\"), \" \")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":340,"wires":[["f3f50673.aac85"]]},{"id":"f3f50673.aac85","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"$flowContext(\"m_count.count\") & \" -- \" & $flowContext(\"m_count.time\")","statusType":"jsonata","x":690,"y":440,"wires":[]}]


1 Like

Thank you very much @E1cid!

This will solve it for me (I will probably copy paste because this is very advanced for me)

If you know of a YT video that explains what you just wrote down, the will be great :slight_smile:

Either way - you have been a major help, Thank you very much!

$moment(timestamp) Will return a moment --"Wed Nov 24 2021 20:58:13 GMT+0000"
tz(timezone) Will correct the moment to your local.
format(patten) then creates a string from the moment using the pattern
using the tokens here for a pattern Moment.js | Docs
This returns a string "2021-11-24 12:34:56.123" or any format you wish.
I then split the string on the space to create an array, $spilt(the_sting_formated_by_moment, "the_split_value_a_space")
The above was used to create the payload properties start and arrive.

To create the duration payload property I used the fact that a timestamp starts on 1970-01-01, so when i take the the arrive timestamp from the start timestamp, what is left is milliseconds since Jan 1st 1970 from midnight (00.00am).
So when i feed the remainder milliseconds into the moment function what is returned is a moment that I can format the time from, which will be the time of the duration of the delay
$moment(payload.arrive - payload.start).tz("UTC").format("HH:mm:ss.SSS")

The trick is understanding how to nest all the functions in side each other.
here is the Jsonata docs that show all the functions available, except the moment functions which are available at the previous url above.

1 Like

Thank you very much @E1cid

This is super helpful and helped me majorly!

I do understand this a lot better now - will practice and create similar things using the explanation you delivered above.

Much appreciated! :raised_hands:t3:

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