Labor shift calculation using array.includes() method

I've got a timestamp inject node going through a date-time formatter to only output the hour of day.

I'm trying to calculate the current shift, 1st, 2nd, or 3rd based on the out of day using the array.includes() method but don't have it quite right.

// Payload is the hour of day in order to perform a shift
// calculation. 

const first = [ 07, 08, 09, 10, 11, 12, 13, 14 ];
const second = [ 15, 16, 17, 18, 19, 20, 21, 22 ];
const third = [ 23, 24, 01, 02, 03, 04, 05, 06 ];

if (first.includes(msg.payload)) {
 shift = 1;
}
if (second.includes(msg.payload)) {
 shift = 2;
}
if (third.includes(msg.payload)) {
 shift = 3;
}
return shift;

What am I doing wrong?

Thanks,
Richard

I would say 01, 02, 03, 04, 05, 06, 07, 08, 09 are not valid numbers, so I would ask what is the msg.payload a number or a string.

Then you return shift which is a number, but you have to return a object. So it would be something like return {payload: shift};

[edit] If payload is a string i would do something like this

const payload = Number(msg.payload);
if (payload >= 7 && payload <= 14) {
    msg = {payload: 1};
}else if (payload >= 15 && payload <= 22) {
    msg = {payload: 2};
}else if (payload >= 23 || payload <= 6) {
    msg = {payload: 3};
}else{
    msg = null;
}
return msg;

Awesome. That was it! (in both cases!)

Thanks,
Richard

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