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

**URL:** <https://discourse.nodered.org/t/help-how-to-save-2-timestamps-in-a-single-sequence/54220>\
**Category:** General\
**Created:** [24 November 2021 17:24 UTC](https://discourse.nodered.org/t/help-how-to-save-2-timestamps-in-a-single-sequence/54220 "2021-11-24T17:24:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![snofkin](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@snofkin](https://discourse.nodered.org/u/snofkin)\
**Post date:** [24 November 2021 17:24 UTC](https://discourse.nodered.org/t/help-how-to-save-2-timestamps-in-a-single-sequence/54220/1 "2021-11-24T17:24:30Z")

</div>

Hello kind people 🙂

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)

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

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

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

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)

Thanks!

---

<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:** [24 November 2021 17:55 UTC](https://discourse.nodered.org/t/help-how-to-save-2-timestamps-in-a-single-sequence/54220/2 "2021-11-24T17:55:42Z")

</div>

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.

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

```

output

```auto
{
"start":["2021-11-24","17:53:14"],
"arrive":["2021-11-24","17:53:22"],
"duration":"00:00:07.551"
}

```

---

<div class="post-metadata">

**Author:** ![snofkin](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@snofkin](https://discourse.nodered.org/u/snofkin)\
**Post date:** [24 November 2021 20:44 UTC](https://discourse.nodered.org/t/help-how-to-save-2-timestamps-in-a-single-sequence/54220/3 "2021-11-24T20:44:14Z")

</div>

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 🙂

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

---

<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:** [24 November 2021 21:23 UTC](https://discourse.nodered.org/t/help-how-to-save-2-timestamps-in-a-single-sequence/54220/4 "2021-11-24T21:23:00Z")

</div>

`$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](https://momentjs.com/docs/#/displaying/format/)  
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.

> **[String functions · JSONata](https://docs.jsonata.org/string-functions)**
>
> \## \`$string()\`

---

<div class="post-metadata">

**Author:** ![snofkin](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@snofkin](https://discourse.nodered.org/u/snofkin)\
**Post date:** [24 November 2021 21:46 UTC](https://discourse.nodered.org/t/help-how-to-save-2-timestamps-in-a-single-sequence/54220/5 "2021-11-24T21:46:25Z")

</div>

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! 🙌🏼

---

<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:** [8 December 2021 21:46 UTC](https://discourse.nodered.org/t/help-how-to-save-2-timestamps-in-a-single-sequence/54220/6 "2021-12-08T21:46:38Z")

</div>

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