# Help with getting time interval

**URL:** <https://discourse.nodered.org/t/help-with-getting-time-interval/77170>\
**Category:** General\
**Created:** [2 April 2023 21:28 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170 "2023-04-02T21:28:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [2 April 2023 21:28 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/1 "2023-04-02T21:28:38Z")

</div>

For quite a long time now I have had code that _worked ok_ but it is starting to show its problems.

Overview:  
Sunset to a fixed time to _dim the lights_ in _n_ steps. (_n_ is fixed.)  
But the time between steps varies depending on the month (_season_).  
To get around the problem I have a file which has varying values which is changed at the start of each month.  
This works but recently the problem showed itself again that the times are just not really working.

I may be able to work it out, but knowing my working: If I don't ask, I won't be able to work it out. Where as if I do ask: I may be able to.

I am using `cron plus` to get sunset (start) time.  
So for the sake of just getting a base line working:  
`cron plus` saying `sunset` and _say_ `22:45`.

I am not sure how to do the maths to determine that time span.

_Side note_  
Then I need to divide that by `5`. But I'm guessing if I can get the first bit done, the `/5` should be easy.

Could someone show me a basic flow how to do this - please?

I'm guessing that the output of the `cron plus` node would be used to start the event/s.  
At _sunset_.

---

<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:** [2 April 2023 21:57 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/2 "2023-04-02T21:57:46Z")

</div>

I do not fully follow your question, but to get a time difference from sunset to 22:45 i would use moment(). and retun the difference in milliseconds  
e.g

```auto
[{"id":"d73b1c55db2ffdc6","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"time","v":"22:45","vt":"str"},{"p":"sunset","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":1300,"wires":[["f8f3f3cc78240699"]]},{"id":"f8f3f3cc78240699","type":"change","z":"65617ffeb779f51c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment($$.time,\"HH:mm\").diff($moment($$.sunset))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":1360,"wires":[["b4e9d79f39fa3018"]]},{"id":"b4e9d79f39fa3018","type":"debug","z":"65617ffeb779f51c","name":"debug 254","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":1400,"wires":[]}]

```

JSONata expression

```auto
$moment($$.time,"HH:mm").diff($moment($$.sunset))

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [2 April 2023 22:03 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/3 "2023-04-02T22:03:42Z")

</div>

Thanks.

With the `JSONata expression` thing....

Where does `$$.sunset` come from?

I'll load the code you posted and look at that but to reply for now.  
🙂

---

<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:** [2 April 2023 22:05 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/4 "2023-04-02T22:05:01Z")

</div>

The timestamp you set when cronplus tell you it is sunset.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [2 April 2023 22:07 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/5 "2023-04-02T22:07:42Z")

</div>

Ok. With you now.  
(_you didn't mention that in the reply - that's all_)

So, at `sunset` I send a `timestamp` (of _now_) and the `change` node tells me how long between _now_ and `22:45` and gives me a number.

That may be what I need.

Appreciated.  
I won't _solve_ it just yet. Just in case there is a gremlin I didn't see.

---

<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:** [2 April 2023 22:11 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/6 "2023-04-02T22:11:35Z")

</div>

> [@Trying\_to\_learn](#):
>
> _ou didn't mention that in the reply - that's all_)

True, i should of named the var `$$.sunset_timestamp` as is clearer. The example did show that it was a timestamp though.

> [@Trying\_to\_learn](#):
>
> o, at `sunset` I send a `timestamp` (of _now_) and the `change` node tells me how long between _now_ and `22:45` and gives me a number.

Yes, it will return the diff in milliseconds.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [2 April 2023 22:13 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/7 "2023-04-02T22:13:17Z")

</div>

Ok, _gremlin #1_

(And I may not be doing it right)

Just for the sake of making it more readable I stuck a `moment` node on the output so I see HH:mm:ss time to go.  
it is 08:12 (local) here.

I am told it is 34 minutes to sunset.

I don't think so.  
So what am I missing?  
Sorry.... I know it is going to be something stupid.

For the sake of it:  
the code:

```auto
[{"id":"f8f3f3cc78240699","type":"change","z":"aaa025a7d2bb5b14","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment($$.time,\"HH:mm\").diff($moment($$.sunset))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":3510,"y":440,"wires":[["b4e9d79f39fa3018","cce9d128e1c7102c"]]},{"id":"d73b1c55db2ffdc6","type":"inject","z":"aaa025a7d2bb5b14","name":"","props":[{"p":"time","v":"22:45:00","vt":"str"},{"p":"sunset","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":3400,"y":380,"wires":[["f8f3f3cc78240699"]]},{"id":"b4e9d79f39fa3018","type":"debug","z":"aaa025a7d2bb5b14","name":"debug 254","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":3700,"y":390,"wires":[]},{"id":"cce9d128e1c7102c","type":"moment","z":"aaa025a7d2bb5b14","name":"","topic":"","input":"","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm:ss","locale":"en-AU","output":"","outputType":"msg","outTz":"Australia/Sydney","x":3700,"y":480,"wires":[["42aeb02b24fee337"]]},{"id":"42aeb02b24fee337","type":"debug","z":"aaa025a7d2bb5b14","name":"debug 255","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":3880,"y":430,"wires":[]}]

```

The number (in milliseconds) also doesn't look right.

 ![Screenshot from 2023-04-03 08-16-56](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/90e303511b870413e6de82473e62ff4f5565de78.png)

~~I'm guessing I need to define the `$$sunset` part of the payload? (Shrug - sorry this is black magic to me. I've never used this before and so don't know what is _given_ and what isn't.)~~

No, _sunset_ is sent as `sunset` and the second time is sent as `time`.  
So yes it is still a bit vague for me.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [2 April 2023 22:39 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/8 "2023-04-02T22:39:44Z")

</div>

Ok trying to get my head around things:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/9/c9cc13e42215f9a863e94535759403341f932f9d.png)

This shows me the times used. They look good.

But that is quite a long time.  
12 hours.... there about.

12 hours in milliseconds.  
Just a bit more than the above shown `50909891` value.  
I'm making that:  
/3600 to convert it to hours. (\* 1000)  
= 14,141

_Ok._

Sorry.  
It is looking better/good.  
I guess I messed up using the `moment` node to make the value readable.

---

<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:** [2 April 2023 22:47 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/9 "2023-04-02T22:47:35Z")

</div>

Yes feeding moment a timestamp of 52000000 will return your local time of Fri Jan 02 1970 00:26:40 GMT+1000, not wht you expected. But you got there in the end with your calcs above.  
p.s.  
THe "HH:mm" in the expression is a tmplate so moments knows what string time value msg.time is. you have added second to the input. Here it makes no difference, but be careful. template for 22:45:00 would be "HH:mm:ss".

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 April 2023 04:34 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/10 "2023-04-03T04:34:25Z")

</div>

Yeah, I forgot about the timzone offset.  
My fault.

Also thanks for explaining the format only needing HH:mm and not HH:mm:ss

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 April 2023 04:41 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/11 "2023-04-03T04:41:11Z")

</div>

Sorry, but one last quick question:

Can I do all that in a `function` node?  
Just that I do a `node.status` of the calculated time so I can see/check it looks good.

---

<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:** [3 April 2023 06:12 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/12 "2023-04-03T06:12:03Z")

</div>

A quick forum search would have given you a few links, one only 9 days ago  
[https://discourse.nodered.org/search?q=using%20moment%20in%20function](https://discourse.nodered.org/search?q=using%20moment%20in%20function)

```auto
[{"id":"24a6de4d3ea58f69","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"time","v":"22:45:00","vt":"str"},{"p":"sunset","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":290,"y":1240,"wires":[["b14b663935c2c1aa"]]},{"id":"b14b663935c2c1aa","type":"function","z":"65617ffeb779f51c","name":"function 22","func":"msg.payload = moment(msg.time,\"HH:mm:ss\").diff(moment(msg.sunset))\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"moment","module":"moment"}],"x":330,"y":1360,"wires":[["6122c4f4fef5f2cb"]]},{"id":"6122c4f4fef5f2cb","type":"debug","z":"65617ffeb779f51c","name":"debug 254","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"$moment($$.payload).format(\"HH:mm:ss\")","targetType":"jsonata","statusVal":"$$.payload/3600000","statusType":"jsonata","x":590,"y":1250,"wires":[]}]

```

```auto
msg.payload = moment(msg.time,"HH:mm:ss").diff(moment(msg.sunset))
return msg;

```

Adding moments to setup tab of function node.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 April 2023 06:13 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/13 "2023-04-03T06:13:37Z")

</div>

The _problem_ is that I don't know the exact words to use to do the search, so I get wild things that don't help.

Sorry, I know... But that's how it is and trying to find the answer.

When I tried what you posted, I see this:

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

I'm stuck at this point.

Ah!

ok.

Imported the flow - rather than the example - and found the `on startup` tab trick.

---

<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:** [17 April 2023 06:14 UTC](https://discourse.nodered.org/t/help-with-getting-time-interval/77170/14 "2023-04-17T06:14:33Z")

</div>

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