Help with getting time interval

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

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.

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

[{"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



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.

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

1 Like

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.

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

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

Yes, it will return the diff in milliseconds.

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:

[{"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.

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.

Ok trying to get my head around things:

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


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

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.
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".

1 Like

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

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

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.

A quick forum search would have given you a few links, one only 9 days ago

[{"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":[]}]
msg.payload = moment(msg.time,"HH:mm:ss").diff(moment(msg.sunset))
return msg;

Adding moments to setup tab of function node.

1 Like

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:

I'm stuck at this point.



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

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