Tuesday, December 12, 2017

Using Node-RED to calculate the amount of days left from today to a future date and publish the result in an MQTT topic

This topic was asked in Google Node-RED discussion group. 


The requirement was quite simple and well explained:

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. The result from this function will be sent to an MQTT topic.

I found it an interesting exercise and proposed the following solution:



The function node named Initialize will do the calculation for the days left from today until a date in the future. This date in the future is hard-coded inside the javascript code. The key statement is assigning to the variable "d" the date in the proper format. For that purpose, it is used the javascript function new Date. Check https://www.w3schools.com/js/js_dates.asp to better understand the date format in Javascript.



This is the javascript code inside the function Initialize:



// Hardcode the future date - Example: 08-May-2018
// Date(year, month, day, hour, minute, second,  millisecond)
// Jan =0, Feb = 1, Mar=2, etc..

var d = new Date(2018, 4, 8, 0, 0, 0, 0);

// Calculate days left from today until future date
var daysLeft;
daysLeft = Math.floor((d-msg.payload)/(1000*60*60*24));

// send formated msg to node MQTT
msg={topic:"reminder/date", payload:daysLeft};
return msg;



The Initialize node assigns to the property Topic the value "reminder/date" which will be used by MQTT output node that accesses the public (and free) MQTT broker HiveMQ.


The debug node helps to check the working of the flow. It shows for instance that from today (Dec, 12th) it is missing 146 days until May 8th (2018), which is the date hard coded.







Here the full flow to be imported into Node-RED:


[{"id":"d4275afd.734df8","type":"mqtt out","z":"46e18adc.8d3cc4","name":"","topic":"","qos":"2","retain":"false","broker":"e7401ab8.4a07d8","x":470,"y":80,"wires":[]},{"id":"157a1549.c7ac0b","type":"inject","z":"46e18adc.8d3cc4","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":160,"y":140,"wires":[["8412dbe9.287618"]]},{"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"]]},{"id":"bcec1998.9976c8","type":"debug","z":"46e18adc.8d3cc4","name":"","active":true,"console":"false","complete":"true","x":470,"y":140,"wires":[]},{"id":"e7401ab8.4a07d8","type":"mqtt-broker","z":"","broker":"broker.hivemq.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]



No comments: