Javascript

Javascript code I have found to be useful.

Making a function node give two outputs
// How to have two outputs

var msg1 = { payload: "Message 1" }var msg2 = { payload: "Message 2" };return [msg1, msg2];
Finding the lowest temperature and when it happened.
// Alex. 19 January 2024
//
// Javascript routine to find the following:
//   the time of this test in PM/AM format, 
//   and return it as msg.payload, 
//   and store it in global.lowestTime
//   and if the corresponding temp is lower than
//   last time 
//   then we update global.lowestTemp)
//
// First we need to know the time when this
// happened

var myDate = new Date();
let amOrPm;

let twelveHours = function () {
if (myDate.getHours() > 12) {
    amOrPm = 'PM';
    let twentyFourHourTime = myDate.getHours();
    let conversion = twentyFourHourTime - 12;
    return `${conversion}`
} else {
    amOrPm = 'AM';
    return `${myDate.getHours()}`
}
};
let hours = twelveHours();
let minutes = myDate.getMinutes();
if (minutes < 10){
    minutes = "0" + minutes 
}
var currentTime = `${hours}:${minutes} ${amOrPm}`;

// now let's see if the temperature 
// was colder than last time we read it
// if so we must update global.get("lowestTemp")

let current_temp = msg.payload
var coolest = Number(global.get("lowestTemp"));

if (current_temp < coolest) {

    // So current temperature is less than the
    // coolest so far don't add *C here because
    // lowestTemp is a number we now send the
    // results to global store

    global.set("lowestTime", currentTime);
    global.set("lowestTemp", current_temp)

    // these globals are used by the “Update
    // Desktop” tab to update the Dashboard (or
    // website)
}

// in any case, regardless of whether 
//temperature reading was a minimum 'Time of last
// reading", send the time to the Dashboard.
 
msg.payload = currentTime;
return msg;