Node red function to return days left and simple text message

824 views
Skip to first unread message

Dan Bicks

unread,
Nov 23, 2017, 1:00:11 PM11/23/17
to Node-RED
Hi Guys,

I need to create a node red function that will be triggered once a day by an inject node. This function should return a simple text message with the days left from now until a specified date in the future. Do you gurus have any ideas on how to achieve this with a node red function node?

The result from this function will be sent to an MQTT topic.

Big thanks in advance.

D

Dan Bicks

unread,
Nov 23, 2017, 3:42:03 PM11/23/17
to Node-RED
Really appreciate any help on this Guys

Cheers

D

Colin Law

unread,
Nov 23, 2017, 4:04:36 PM11/23/17
to node...@googlegroups.com
You cannot expect an answer in such a short time, it may happen but
you cannot expect it.
Remember that this is an international group. At any one time approx
one third of the participants will be asleep, another third will be
working for a living and most of the rest will have better things to
do. That leaves just you and me apparently, and I was having my
dinner.

Take a look at the javascript Date functions. You should find
everything you need there I think.
https://www.w3schools.com/js/js_date_methods.asp

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/b823e086-0b1e-424d-a485-3deeb5985300%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Dan Bicks

unread,
Nov 23, 2017, 4:21:36 PM11/23/17
to Node-RED
Colin,

Thanks for your reply, that is no problem, I am new to this forum and very eager to get the functionality implemented. The link is
great I have stumbled on this but not an expert with node red function nodes and seem to get errors when I implement code in them.

Still trying to get my head around some of this amazing framework. Would be great if anyone has some example stuff for me to tinker with in terms of the functionality I am trying to achieve.

Thanks again for your time I do appreciate it.

Regards

D

Colin Law

unread,
Nov 23, 2017, 4:29:10 PM11/23/17
to node...@googlegroups.com
If you are serious about using node red then a bit of basic knowledge
of javascript is essential. I suggest finding a tutorial on basic js,
and the w3chools site has a wealth of examples. In addition the flows
library [1] has thousands of examples of flows, have a look at some of
those to see how they work. Also look at the pages on writing
function nodes. [2]

[1] https://flows.nodered.org/
[2] https://nodered.org/docs/writing-functions
> https://groups.google.com/d/msgid/node-red/e8c36b54-c90d-4072-b794-7ec6a2ac598a%40googlegroups.com.

Michael Hogan

unread,
Nov 23, 2017, 5:58:25 PM11/23/17
to Node-RED
Here is a quick little example of using the Date() "class".

now = new Date(); // Thu Nov 23 2017 17:49:15 GMT-0500 (Eastern Standard Time)
then=new Date(now.valueOf() + 1e9); // Tue Dec 05 2017 07:35:55 GMT-0500 (Eastern Standard Time)
days = (then-now)/86400000; // take difference, which returns milliseconds.  Then divide by 86400000 milliseconds/day to get 11.574074074074074

You can round it etc, if you want an integer.

At any rate, this example shows the basics of constructing a current time, a future time, and calculating time differences.  But, as others have pointed out, you are apt to spend a bit of time on W3 Schools, Stack Overflow etc. etc. as you progress.

Julian Knight

unread,
Nov 23, 2017, 6:04:28 PM11/23/17
to Node-RED
For fun and to get you going, I think the following JavaScript will calculate the number of days between two dates:

Math.round((second-first)/(1000*60*60*24))

Note, however, that the input variables (second and first) need to be JavaScript Dates.

The divisor is milliseconds in a second x seconds in a minute x minutes in an hour x hours in a day. That is because JAvaScript Dates are held as the number of milliseconds since 1/1/1970 (If I've remembered correctly).

AIOT MAKER

unread,
Nov 23, 2017, 10:49:35 PM11/23/17
to Node-RED
Building on top of what has been answered above...and using the following link as reference:  https://www.w3schools.com/js/js_dates.asp

The way I did it is necessary to hardcode the future date and the MQTT topic name inside the function node.
I did not test the boundary conditions so the calculation might (eventually) result in one day difference.
Also, I could not test the MQTT node as the public server that I normally use is down at the time of this writing.



[{"id":"8412dbe9.287618","type":"function","z":"46e18adc.8d3cc4","name":"Initialize","func":"// Hardcode the future date - Example: 08-May-2018\n// Date(year, month, day, hour, minute, second,  millisecond)\n// Jan =0, Feb = 1, Mar=2, etc..\nvar d = new Date(2018, 4, 8, 0, 0, 0, 0);\n// Calculate days left from today until future date\nvar daysLeft;\ndaysLeft = Math.floor((d-msg.payload)/(1000*60*60*24));\n// send formated msg to node MQTT\nmsg={topic:\"reminder/date\", payload:daysLeft};\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":140,"wires":[["bcec1998.9976c8","d4275afd.734df8"]]}]


Dan Bicks

unread,
Nov 24, 2017, 11:42:05 AM11/24/17
to Node-RED
You guys are amazing, super cool flow thanks AIOT and for all of you with your help.

I will now be able to get my head around this and produce a message that is sent to my IOT display device.

I did have also a text box and a button on a UI dashboard and tried to send the message in the text box when the button was clicked, I am not sure if any of you have done a similar thing any pointers would be great.

I intend to really put my head in to this over the weekend and learn the newbie stuff. I have coded in assembler language for many years and have done a lot in C for micro-controllers. Great to learn new things, a huge thanks again for the excellent support in this forum.

Kind regards D 
 

On Thursday, November 23, 2017 at 6:00:11 PM UTC, Dan Bicks wrote:

Colin Law

unread,
Nov 24, 2017, 4:17:44 PM11/24/17
to node...@googlegroups.com
If you have done C then js shouldn't cause many problems. Just
remember that objects are handled and passed by reference, so when you
say
msg1 = msg
Then you have two references to the same message, not two messages.

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/f0891782-9e94-46ed-ab5c-731fbb2139e0%40googlegroups.com.

steve rickus

unread,
Nov 25, 2017, 9:55:36 AM11/25/17
to Node-RED
Dan, you may want to check out the node-red-contrib-moment nodes. The package includes a "humanizer" node that can convert a positive or negative number of seconds into human readable form, more suitable for display.

For instance, to get the number of seconds until Christmas, I would use code something like this:

// target date/time
var xmas = Date.parse("2017-12-25 06:00:00");
// time difference in seconds
var diff = Math.round((msg.payload - xmas) / 1000);
// return seconds to humanizer
msg
.payload = diff;
return msg;

Have fun!
--
Steve

Dan Bicks

unread,
Nov 25, 2017, 2:19:23 PM11/25/17
to Node-RED
Steve thanks buddy,

I am having super fun with this already, I will look at the humanizer, so much one can do with Node Red it is amazing.

Currently I am now sending a countdown message via MQTT even included weeks and hours as well as days left to an event. Message is changed based on days left count.I will post my flow once I am all done with it.

Trying to now send data from text box's on UI form when a button is clicked, getting there.

Big thanks to you all again, great forum.

Dans

On Thursday, November 23, 2017 at 6:00:11 PM UTC, Dan Bicks wrote:

Julian Knight

unread,
Nov 25, 2017, 6:00:40 PM11/25/17
to Node-RED
And if you want any new or amended features in contrib-moment, just let me know, raise an issue or submit a pull request!

:-)
Reply all
Reply to author
Forward
0 new messages