Stuck with a bit of code (function node) and messages

I am sorry folks, but this is/has been going on for a while and now and as I am looking at it, I am confused.

Few things happening:
I have 3 outputs and I can not for the life of me see how/where the third one is made.

I am wanting to add extra stuff to the messages so I can Maybe track down the bigger problem.
(And I've just worked that out.)

So on to the BIGGER question.

Quick backstory.

I have 3 machines.
Each test the main router and the public IP address every 20 seconds.
They construct a message and send that to one another and in the second bit the messages are combined.

The values are tallied and if I get a case where all are set to 0 then I am told the uplink is off line.

A few months back I went through and did a lot of tidying up OLD code and as good as it was: it was also bad. (Sod's law)

2 of the machine's nodes were not parsing the messages and so it was 1 in 3 of the machines that was actually giving me a true message.

I fixed the first one - and stupidly didn't see the second one.
The rogue reports of uplink down, (etc) seemed to stop.

They're ba-ack!

But about daily at the same-ish times.

What I'm wanting to do is set a new/extra part of the messages sent that tells me who sent the message.

Then I can (maybe) get closer to understanding / finding out what is going on.

The code - slightly different in each machine for weightings of messages.

//  2023 09 30  -- new code.
let msg1 = {};
let msg2 = {};
let machine1 = "TimePi";
let machine2 = "BedPi";
let machine3 = "TelePi";
let newval;

// set up to wipe all stuff.
if (msg.payload == "WIPE") {
    context.set('m1', 0);
    context.set('m2', 0);
    context.set('m3', 0);
    context.set('mstate', 0);
    context.set('ustate', 0);
    node.status({ fill: "green", text: "WIPED" });
    //    node.warn("WIPED");
    return;
}

if (msg.payload == "DEBUG") {
    context.set("debug", msg.onoff);
    if (msg.onoff == 0) {
        node.status({});
        //        return;
    } else {
        node.status({ fill: "blue", text: "DEBUG ON" });
        //        return;
    }
    return;
}

//Set Bit
function setBit(number, bitPosition) {
    return number | (1 << bitPosition);
}

//Clear Bit
function clearBit(number, bitPosition) {
    const mask = ~(1 << bitPosition);
    return number & mask;
}
/////////////////////////////////

//node.warn("Look for this " + msg.payload.Who);

let machine = msg.payload.Who;
//let link = msg.link;
let mstate = msg.payload.Modem;
let ustate = msg.payload.UpLink;
let modem;
let uplink;
let value;
let m1 = context.get("m1") || 0;
let m2 = context.get("m2") || 0;
let m3 = context.get("m3") || 0;
let debug = context.get("debug") || 0;

node.status({});        //  Wipe status

if (debug == 1) {
    node.warn("************************");
    node.warn("Machine is " + machine);
    //node.warn("link is " + link);
    node.warn("Modem state is " + mstate);
    node.warn("Uplink state is " + ustate);
}

//node.warn("mstate " + mstate);

if (machine == machine1) {
    //  Machine 1
    if (debug == 1) {
        //
        node.warn("Setting value for MACHINE 1");
    }
    //    node.warn("Machine 1");
    context.set("m1", 1);
    if (mstate == "Offline") {
        value = context.get("mstate") || 0;
        newval = clearBit(value, 0);
        context.set("mstate", newval);
    } else
        if (mstate == "Online") {
            value = context.get("mstate") || 0;
            newval = setBit(value, 0);
            context.set("mstate", newval);
        }
    if (ustate == "Offline") {
        value = context.get("ustate") || 0;
        newval = clearBit(value, 0);
        context.set("ustate", newval);
    } else
        if (ustate == "Online") {
            value = context.get("ustate") || 0;
            newval = setBit(value, 0);
            context.set("ustate", newval);
        }
}

if (machine == machine2) {
    //  Machine 2
    if (debug == 1) {
        //
        node.warn("Setting value for MACHINE 2");
    }
    //    node.warn("Machine 2");
    context.set("m2", 1);
    if (mstate == "Offline") {
        value = context.get("mstate") || 0;
        newval = clearBit(value, 1);
        context.set("mstate", newval);
    } else
        if (mstate == "Online") {
            value = context.get("mstate") || 0;
            newval = setBit(value, 1);
            context.set("mstate", newval);
        }
    if (ustate == "Offline") {
        value = context.get("ustate") || 0;
        newval = clearBit(value, 1);
        context.set("ustate", newval);
    } else
        if (ustate == "Online") {
            value = context.get("ustate") || 0;
            newval = setBit(value, 1);
            context.set("ustate", newval);
        }
}

if (machine == machine3) {
    //  Machine 3
    if (debug == 1) {
        //
        node.warn("Setting value for MACHINE 3");
    }
    //    node.warn("Machine 3");
    context.set("m3", 1);
    if (mstate == "Offline") {
        value = context.get("mstate") || 0;
        newval = clearBit(value, 2);
        context.set("mstate", newval);
    } else
        if (mstate == "Online") {
            value = context.get("mstate") || 0;
            newval = setBit(value, 2);
            context.set("mstate", newval);
        }
    if (ustate == "Offline") {
        value = context.get("ustate") || 0;
        newval = clearBit(value, 2);
        context.set("ustate", newval);
    } else
        if (ustate == "Online") {
            value = context.get("ustate") || 0;
            newval = setBit(value, 2);
            context.set("ustate", newval);
        }
}

m1 = context.get("m1") || 0;
m2 = context.get("m2") || 0;
m3 = context.get("m3") || 0;

modem = context.get("mstate") || 0;
uplink = context.get("ustate") || 0;
msg2.payload = "M " + modem + ", U " + uplink + ", m1 " + m1 + ", m2 " + m2 + ", m3 " + m3;

if (debug == 1) {
    node.warn("--------------");
    node.warn("Modem value " + modem);
    node.warn("Uplink value " + uplink);
    node.warn("Counters " + m1 + " " + m2 + " " + m3);
    node.warn("--------------");
}

node.status({ text: "Uplink " + uplink + " Modem " + modem + " Counters " + m3 + " " + m2 + " " + m1});

if (m1 == 1 && m2 == 1 && m3 == 1) {
    //  This is used to wipe values once 3 messages are received.
    //    node.warn("Wiping stored values.");
    context.set("m1", 0);
    context.set("m2", 0);
    context.set("m3", 0);
    context.set("mstate", 0);
    context.set("ustate", 0);
    //    modem  = context.get("modem") || 0;
    //    uplink = context.get("uplink") || 0;
    //  Now send the message

    //node.status({text:""});

//}

//if (m1 == 1 || m2 == 1 || m3 == 1) {
    //---------
    if (debug == 1) {
        //
        node.warn("Values being sent");
        node.warn("modem is " + modem);
    }
    //---------
    if (modem === 0) {
        uplink = "UNKNOWN";
        msg = { "payload": '<font color = "red"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "red" };
        //msg.colour = "red";
    } else

//        if (modem > 3) {
        if (modem == 7) {
            //node.status({fill: "green",text:machine3});
            msg = { "payload": '<font color = "lime"> <i class="fa fa-bullseye fa-2x"></i></font>', "colour": "lime" };
            //msg.msg.colour = "lime";
        } else

//            if (modem > 2) {
            if (modem == 3 || modem == 5 || modem == 6) {
                //node.status({fill: "yellow",text:machine2});
                msg = { "payload": '<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "green" };
                //msg.colour = "green";
            } else

//                if (modem == 1) {
                if (modem == 1 || modem == 2 || modem == 4) {
                    //node.status({fill: "red",text:machine1});
                    msg = { "payload": '<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "springgreen" };
                    //msg.colour = "springgreen";
                }
    if (uplink === 0) {
        msg1 = { "payload": '<font color = "red"> <i class="fa fa fa-bullseye fa-2x" ></i></font>', "colour": "red" };
        //msg1.colour = "red";
    } else

//        if (uplink > 3) {
        if (uplink == 7) {
            msg1 = { "payload": '<font color = "lime"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "lime" };
            //msg1.colour = "lime";
        } else

//            if (uplink > 2) {
            if (uplink == 3 || modem == 5 || modem == 6) {
                msg1 = { "payload": '<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "green" };
                //msg1.colour = "green";
            } else

//                if (uplink == 1)
                if (uplink == 1 || modem == 2 || modem == 4) {
                    msg1 = { "payload": '<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "springgreen" };
                    //msg1.colour = "springgreen";
                } else

                    if (uplink == "UNKNOWN") {
                        msg1 = { "payload": '<font color = "black"> <i class="fa fa-question-circle fa-2x" ></i></font>', "colour": "black" };
                        //msg.colour = "black";
                    }

    //    msg2.payload = {"Modem":modem,"Uplink":uplink};
    if (debug == 1) {
        //
        node.warn("Payload set to " + msg.payload);
    }
    return [msg, msg1, msg2];

}

if (debug == 1) {
    //
    node.warn("NO MESSAGE");
}

return [null, null, msg2];

The important part at the bottom.

return [msg, msg1, msg2]

msg and msg1 are nearly identical, but are for different devices. Modem (to call it) and the uplink.

I'm wanting to add - with as little extra typing as possible - a msg.sender to the messages.

But simply doing this:

msg.sender = "devicename"
msg1.sender = "devicename"

just before the return [msg,msg1,msg2] line.

Gives me errors.

I'll try again - again banking on Sod's law to make a lire of me - but I'm confused why it gives me an error.

Thanks in advance.

Slight update:

It only throws the error when I do the msg1.sender = "BedPi";
But seems to accept msg.sender = "BedPi";

Screen shots.
And yes, I've cheated and commented out the line for the sake of getting the screenshots.

All good.

Again: Sorry. I've got the flu and my brain is not working the best.

Andrew,

Almost certain that msg1 will be a string - based on where you have set it - but you are trying to change it from a string to being an object when you start adding the extra properties.

Sorry not a lot of time to go through all the code now - just stood out at the end

What is the error it throws ?

Craig

Thanks Craig.

As I posted it and went for a dump I realised the problem.

I am adding msg.sender

It needs to be msg.payload.sender = "BedPi"

This is because it is being sent over MQTT and so it all needs to be in the payload.

Ooops.

Changed the code to:

    //
    //  2024 09 10
    //
    //  Because of a lot of false alerts, adding this to try and find out what is going on.
    msg.payload.sender = "BedPi";
    msg1.payload.sender = "BedPi";

No errors.
With it changed for each machine - of course.

Ok, folks, the story deepens.

I did the modification and didn't get an error with the lines:

msg.payload.sender = "Computer name";

And so added it to the 3 nodes on the 3 machines.
(That's 6 lines.)

Anyway, today I got home and all hell had broken out.

{
"topic":"ERROR_REPORT/TelePi/Events",
"qos":0,
"retain":false,
"_msgid":"346c082fef4db59c",
"delay":60000,"
_event":"node:b7dfa060.6a4218",
"error":{"message":"TypeError: Cannot set property 'sender' of undefined","source":{"id":"6ea45b88f26f883d","type":"function","name":"New Router/Uplink status *","count":1}},"
_error":{},"settings":{"input":"2024-09-11T02:29:19.089Z","input_format":"","input_tz":"Australia/Sydney","output_format":"YYYY-MM-DD HH:mm:ss","output_locale":"en_AU","output_tz":"Australia/Sydney"},
"time":"2024-09-11 12:29:19",
"payload":
    {"message":"TypeError: Cannot set property 'sender' of undefined",
"source",{"id":"6ea45b88f26f883d",
"type":"function",
"name":"New Router/Uplink status *",
"count":1}}}

Sorry can't get it too nice.
But I tried formatting it to show the part of interest:
Cannot set property 'sender' of undefined

So seems that doesn't work.

DRATS!

This is the code (in total)
(Sorry)

//  2023 09 30  -- new code.
let msg1 = {};
let msg2 = {};
//machine1 = "TelePi";
//machine2 = "TimePi";
//machine3 = "BedPi";
let machine1 = "TimePi";
let machine2 = "BedPi";
let machine3 = "TelePi";
let newval;

// set up to wipe all stuff.
if (msg.payload == "WIPE") {
    context.set('m1', 0);
    context.set('m2', 0);
    context.set('m3', 0);
    context.set('mstate', 0);
    context.set('ustate', 0);
    node.status({ fill: "green", text: "WIPED" });
    //    node.warn("WIPED");
    return;
}

if (msg.payload == "DEBUG") {
    context.set("debug", msg.onoff);
    if (msg.onoff == 0) {
        node.status({});
        //        return;
    } else {
        node.status({ fill: "blue", text: "DEBUG ON" });
        //        return;
    }
    return;
}

//Set Bit
function setBit(number, bitPosition) {
    return number | (1 << bitPosition);
}

//Clear Bit
function clearBit(number, bitPosition) {
    const mask = ~(1 << bitPosition);
    return number & mask;
}
/////////////////////////////////

//node.warn("Look for this " + msg.payload.device);

let machine = msg.payload.Who;
//let link = msg.link;
let modem = msg.payload.Modem;
let uplink = msg.payload.UpLink
let mstate = context.get("mstate") || 0;
let ustate = context.get("ustate") || 0;
let value;
let m1 = context.get("m1") || 0;
let m2 = context.get("m2") || 0;
let m3 = context.get("m3") || 0;
let debug = context.get("debug") || 0;

node.status({});        //  Wipe status

if (debug == 1) {
    node.warn("************************");
    node.warn("Machine is " + machine);
    //node.warn("link is " + link);
    node.warn("Modem state is " + modem);
    node.warn("Uplink state is " + uplink);
    node.warn("mstate is " + mstate)
    node.warn("ustate is " + ustate)
}

//node.warn("mstate " + mstate);

if (machine == machine1) {
    //  Machine 1
    if (debug == 1) {
        //
        node.warn("Setting value for MACHINE 1");
    }
    //    node.warn("Machine 1");
    //context.set("m1", 1);
    //      ==--  modem stuff
    if (modem == "Offline") {
        if (debug == 1) {
            //
            node.warn(">> modem offline <<");
        }
        value = context.get("mstate") || 0;
        newval = clearBit(value, 0);
        context.set("mstate", newval);
        context.set("m1", 0);
    }
    else
    if (modem == "Online") {
        if (debug == 1) {
            //
            node.warn(">> modem online <<");
        }
        value = context.get("mstate") || 0;
        newval = setBit(value, 0);
        context.set("mstate", newval);
        context.set("m1", 1);
    }
    //      ==--  uplink stuff
    if (uplink == "Offline") {
        if (debug == 1) {
            //
            node.warn(">> uplink offline <<");
        }
        value = context.get("ustate") || 0;
        newval = clearBit(value, 0);
        context.set("ustate", newval);
    } 
    else
    if (uplink == "Online") {
        if (debug == 1) {
            //
            node.warn(">> uplink online <<");
        }
        value = context.get("ustate") || 0;
        newval = setBit(value, 0);
        context.set("ustate", newval);
    }
    if (debug == 1) {
        //
        node.warn("Finnished setting value for MACHINE 1");
        node.warn("mstate is " + mstate)
        node.warn("ustate is " + ustate)
    }
}

if (machine == machine2) {
    //  Machine 2
    if (debug == 1) {
        //
        node.warn("Setting value for MACHINE 2");
    }
    //    node.warn("Machine 2");
    //context.set("m2", 1);
    if (mstate == "Offline") {
        value = context.get("mstate") || 0;
        newval = clearBit(value, 1);
        context.set("mstate", newval);
        context.set("m2", 0);
    } else
        if (mstate == "Online") {
            value = context.get("mstate") || 0;
            newval = setBit(value, 1);
            context.set("mstate", newval);
            context.set("m2", 1);
        }
    if (ustate == "Offline") {
        value = context.get("ustate") || 0;
        newval = clearBit(value, 1);
        context.set("ustate", newval);
    } else
        if (ustate == "Online") {
            value = context.get("ustate") || 0;
            newval = setBit(value, 1);
            context.set("ustate", newval);
        }
}

if (machine == machine3) {
    //  Machine 3
    if (debug == 1) {
        //
        node.warn("Setting value for MACHINE 3");
    }
    //    node.warn("Machine 3");
    //context.set("m3", 1);
    if (mstate == "Offline") {
        value = context.get("mstate") || 0;
        newval = clearBit(value, 2);
        context.set("mstate", newval);
        context.set("m3", 0);
    } else
        if (mstate == "Online") {
            value = context.get("mstate") || 0;
            newval = setBit(value, 2);
            context.set("mstate", newval);
            context.set("m3", 1);
        }
    if (ustate == "Offline") {
        value = context.get("ustate") || 0;
        newval = clearBit(value, 2);
        context.set("ustate", newval);
    } else
        if (ustate == "Online") {
            value = context.get("ustate") || 0;
            newval = setBit(value, 2);
            context.set("ustate", newval);
        }
}

m1 = context.get("m1") || 0;
m2 = context.get("m2") || 0;
m3 = context.get("m3") || 0;

modem = context.get("modem") || 0;
uplink = context.get("uplink") || 0;
msg2.payload = "M " + modem + ", U " + uplink + ", m1 " + m1 + ", m2 " + m2 + ", m3 " + m3;

if (debug == 1) {
    node.warn("--------------");
    node.warn("Modem value " + modem);
    node.warn("Uplink value " + uplink);
    node.warn("Counters " + m1 + " " + m2 + " " + m3);
    node.warn("--------------");
}

node.status({ text: "Uplink " + uplink + " Modem " + modem + " Counters " + m3 + " " + m2 + " " + m1 });

if (m1 == 1 && m2 == 1 && m3 == 1) 
{
    //  This is used to wipe values once 3 messages are received.
    //    node.warn("Wiping stored values.");

    //
    //      BLOCK DISABLED 2024 09 11
    //
    context.set("m1", 0);
    context.set("m2", 0);
    context.set("m3", 0);
    context.set("mstate", 0);
    context.set("ustate", 0);
    //
    //      BLOCK DISABLED 2024 09 11
    //

    //    modem  = context.get("modem") || 0;
    //    uplink = context.get("uplink") || 0;
    //  Now send the message

    //}

    //if (m1 == 1 || m2 == 1 || m3 == 1) {
    //---------
    if (debug == 1) {
        //
        node.warn("Values being sent");
        node.warn("modem is " + modem);
        node.warn("Mode value is " + mstate);
        node.warn("Ustate value is " + ustate);
    }
    //---------
    if (mstate === 0) {
        uplink = "UNKNOWN";
        msg = { "payload": '<font color = "red"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "red" };
        //msg.colour = "red";
    } else

        //        if (modem > 3) {
        if (mstate == 7) {
            //node.status({fill: "green",text:machine3});
            msg = { "payload": '<font color = "lime"> <i class="fa fa-bullseye fa-2x"></i></font>', "colour": "lime" };
            //msg.msg.colour = "lime";
        } else

            //            if (modem > 2) {
            if (mstate == 3 || mstate == 5 || mstate == 6) {
                //node.status({fill: "yellow",text:machine2});
                msg = { "payload": '<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "green" };
                //msg.colour = "green";
            } else

                //                if (modem == 1) {
                if (mstate == 1 || mstate == 2 || mstate == 4) {
                    //node.status({fill: "red",text:machine1});
                    msg = { "payload": '<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "springgreen" };
                    //msg.colour = "springgreen";
                }
    if (ustate === 0) {
        msg1 = { "payload": '<font color = "red"> <i class="fa fa fa-bullseye fa-2x" ></i></font>', "colour": "red" };
        //msg1.colour = "red";
    } else

        //        if (uplink > 3) {
        if (ustate == 7) {
            msg1 = { "payload": '<font color = "lime"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "lime" };
            //msg1.colour = "lime";
        } else

            //            if (uplink > 2) {
            if (ustate == 3 || ustate == 5 || ustate == 6) {
                msg1 = { "payload": '<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "green" };
                //msg1.colour = "green";
            } else

                //                if (uplink == 1)
                if (ustate == 1 || ustate == 2 || ustate == 4) {
                    msg1 = { "payload": '<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "springgreen" };
                    //msg1.colour = "springgreen";
                } else

                    if (uplink == "UNKNOWN") {
                        msg1 = { "payload": '<font color = "black"> <i class="fa fa-question-circle fa-2x" ></i></font>', "colour": "black" };
                        //msg.colour = "black";
                    }

    //    msg2.payload = {"Modem":modem,"Uplink":uplink};
    if (debug == 1) {
        //
        node.warn("Payload set to " + msg.payload);
    }
    //
    //  2024 09 10
    //
    //  Because of a lot of false alerts, adding this to try and find out what is going on.
    //msg.payload.sender = "TimePi";
    //msg1.payload.sender = "TimePi";
    return [msg, msg1, msg2];

}

if (debug == 1) {
    //
    node.warn("NO MESSAGE");
}

return [null, null, msg2];

Look at the two lines near the bottom commented out.
(or search for sender)

How would I (easily) add a msg.payload.sender to both messages?

UPDATE!

I've found SOME of the many problems.

I've - therefore - reposted the code (above)

Yes, it is still a dog's breakfast bit I am slowly moving forward.
Alas SLOWLY!

This bit of code:

if (modem == "Offline") {
        if (debug == 1) {
            //
            node.warn(">> modem offline <<");
        }
        value = context.get("mstate") || 0;
        newval = clearBit(value, 0);
        context.set("mstate", newval);
        context.set("m1", 0);
    }
    else
    if (modem == "Online") {
        if (debug == 1) {
            //
            node.warn(">> modem online <<");
        }
        value = context.get("mstate") || 0;
        newval = setBit(value, 0);
        context.set("mstate", newval);
        context.set("m1", 1);
    }

The received message is:
{"topic":"STATUS/ADSL_Link_Status/","payload":{"Who":"TimePi","Modem":"Online","UpLink":"Online"},"_msgid":"70948e834d4248e6","_event":"node:910699e0.64db8"}

But the set bit doesn't seem to be working.

Around line 37

//Set Bit
function setBit(number, bitPosition) {
    return number | (1 << bitPosition);
}

//Clear Bit
function clearBit(number, bitPosition) {
    const mask = ~(1 << bitPosition);
    return number & mask;
}

So that/this would explain some of the problem/s.

I could post the text code if you want so we would be both on the same page.

Even more updated code.

Restructured the flow and got rid of a lot of older stuff that was in it.

//      Name machines
let machine1 = "TimePi";
let machine2 = "BedPi";
let machine3 = "TelePi";

//  2023 09 30  -- new code.
let msg1 = {};
let msg2 = {};
let newval;
let counter = context.get("counter") || 0;

// set up to wipe all stuff.
if (msg.payload == "WIPE") {
    context.set('m1', 0);
    context.set('m2', 0);
    context.set('m3', 0);
    context.set('mstate', 0);
    context.set('ustate', 0);
    context.set("counter",0);
    node.status({ fill: "green", text: "WIPED" });
    //    node.warn("WIPED");
    return;
}

if (msg.payload == "DEBUG") {
    context.set("debug", msg.onoff);
    if (msg.onoff == 0) {
        node.status({});
        //        return;
    } else {
        node.status({ fill: "blue", text: "DEBUG ON" });
        //        return;
    }
    return;
}

/////////////////////////////////
//
//  Routines.
//
//Set Bit
function setBit(number, bitPosition) {
    return number | (1 << bitPosition);
}
//
//Clear Bit
function clearBit(number, bitPosition) {
    const mask = ~(1 << bitPosition);
    return number & mask;
}
//
/////////////////////////////////

let machine = msg.payload.Who;
let modem = msg.payload.Modem;
let uplink = msg.payload.UpLink
let mstate = context.get("mstate") || 0;
let ustate = context.get("ustate") || 0;
let value;
let m1 = context.get("m1") || 0;
let m2 = context.get("m2") || 0;
let m3 = context.get("m3") || 0;
let debug = context.get("debug") || 0;
context.set("modem", modem);
context.set("uplink", uplink);

node.status({});        //  Wipe status

if (debug == 1) {
    node.warn("****  GLOBAL VALUES  *********");
    node.warn("Sending machine name is " + machine);
    node.warn("Modem state is " + modem);
    node.warn("Uplink state is " + uplink);
    node.warn("mstate is " + mstate)
    node.warn("ustate is " + ustate)
    node.warn("Counter value is " + counter)
}

if (machine == machine1) {
    //  Machine 1
    if (debug == 1) {
        //
        node.warn("Setting value for MACHINE 1");
        node.warn("modem state  is " + modem);
        node.warn("mstate value is " + mstate);
        node.warn("uplink state is " + uplink);
        node.warn("ustate value is " + ustate);
        node.warn("Counter value is " + counter);
    }
    //      ==--  modem stuff
    if (modem == "Offline") {
        if (debug == 1) {
            //
            node.warn(">> modem offline <<");
        }
        value = context.get("mstate") || 0;
        newval = clearBit(value, 0);
        context.set("mstate", newval);
        context.set("m1", 0);
        counter = (counter - 1) % 3
        context.set("counter", counter)
    }
    else
    if (modem == "Online") {
        if (debug == 1) {
            //
            node.warn(">> modem online <<");
        }
        value = context.get("mstate") || 0;
        newval = setBit(value, 0);
        context.set("mstate", newval);
        context.set("m1", 1);
        counter = (counter + 1) % 3
        context.set("counter", counter)
    }
    //      ==--  uplink stuff
    if (uplink == "Offline") {
        if (debug == 1) {
            //
            node.warn(">> uplink offline <<");
        }
        value = context.get("ustate") || 0;
        newval = clearBit(value, 0);
        context.set("ustate", newval);
    } 
    else
    if (uplink == "Online") {
        if (debug == 1) {
            //
            node.warn(">> uplink online <<");
        }
        value = context.get("ustate") || 0;
        newval = setBit(value, 0);
        context.set("ustate", newval);
    }
    if (debug == 1) {
        //
        node.warn("Finnished setting value for MACHINE 1");
        //node.warn("mstate is " + mstate)
        //node.warn("ustate is " + ustate)
        //node.warn("mstate is " + context.get("mstate"));
        //node.warn("ustate is " + context.get("ustate"));
        node.warn("modem state  is " + context.get("modem"));
        node.warn("mstate value is " + context.get("mstate"));
        node.warn("uplink value is " + context.get("uplink"));
        node.warn("ustate value is " + context.get("ustate"));
        node.warn("Counter value is " + counter);
    }
}

if (machine == machine2) {
    //  Machine 2
    if (debug == 1) {
        //
        node.warn("Setting value for MACHINE 2");
        node.warn("modem state  is " + modem);
        node.warn("mstate value is " + mstate);
        node.warn("uplink state is " + uplink);
        node.warn("ustate value is " + ustate);
        node.warn("Counter value is " + counter);
    }
    //    node.warn("Machine 2");
    //context.set("m2", 1);
    if (modem == "Offline") {
        if (debug == 1) {
            //
            node.warn(">> modem offline <<");
        }
        value = context.get("mstate") || 0;
        newval = clearBit(value, 1);
        context.set("mstate", newval);
        context.set("m2", 0);
        counter = (counter - 1) % 3
        context.set("counter", counter)
    } 
    else
    if (modem == "Online") {
        if (debug == 1) {
            //
            node.warn(">> modem online <<");
        }
        value = context.get("mstate") || 0;
        newval = setBit(value, 1);
        context.set("mstate", newval);
        context.set("m2", 1);
        counter = (counter + 1) % 3
        context.set("counter", counter)
    }
    if (uplink == "Offline") {
        if (debug == 1) {
            //
            node.warn(">> uplink offline <<");
        }
        value = context.get("ustate") || 0;
        newval = clearBit(value, 1);
        context.set("ustate", newval);
    } 
    else
    if (uplink == "Online") {
        if (debug == 1) {
            //
            node.warn(">> uplink online <<");
        }
        value = context.get("ustate") || 0;
        newval = setBit(value, 1);
        context.set("ustate", newval);
    }
    if (debug == 1) {
        //
        node.warn("Finnished setting value for MACHINE 2");
        //node.warn("mstate is " + mstate)
        //node.warn("ustate is " + ustate)
//        node.warn("mstate is " + context.get("mstate"));
//        node.warn("ustate is " + context.get("ustate"));
        node.warn("modem state  is " + context.get("modem"));
        node.warn("mstate value is " + mstate);
        node.warn("uplink state is " + context.get("uplink"));
        node.warn("ustate value is " + ustate);
        node.warn("Counter value is " + counter);
    }
}

if (machine == machine3) {
    //  Machine 3
    if (debug == 1) {
        //
        node.warn("Setting value for MACHINE 3");
        node.warn("modem state  is " + modem);
        node.warn("mstate value is " + mstate);
        node.warn("uplink value is " + uplink);
        node.warn("ustate value is " + ustate);
        node.warn("Counter value is " + counter);
    }
    //    node.warn("Machine 3");
    //context.set("m3", 1);
    if (modem == "Offline") {
        value = context.get("mstate") || 0;
        newval = clearBit(value, 2);
        context.set("mstate", newval);
        context.set("m3", 0);
        counter = (counter - 1) % 3
        context.set("counter", counter)
    } else
    if (modem == "Online") {
        value = context.get("mstate") || 0;
        newval = setBit(value, 2);
        context.set("mstate", newval);
        context.set("m3", 1);
        counter = (counter + 1) % 3
        context.set("counter", counter)
    }
    if (uplink == "Offline") {
        value = context.get("ustate") || 0;
        newval = clearBit(value, 2);
        context.set("ustate", newval);
    } else
    if (uplink == "Online") {
        value = context.get("ustate") || 0;
        newval = setBit(value, 2);
        context.set("ustate", newval);
    }
    if (debug == 1) {
        //
        node.warn("Finnished setting value for MACHINE 3");
        //node.warn("mstate is " + mstate)
        //node.warn("ustate is " + ustate)
//        node.warn("mstate is " + context.get("mstate"));
//        node.warn("ustate is " + context.get("ustate"));
        node.warn("modem state  is " + context.get("modem"));
        node.warn("mstate value is " + context.get("mstate"));
        node.warn("uplink state is " + context.get("uplink"));
        node.warn("ustate value is " + context.get("ustate"));
        node.warn("Counter value is " + counter);
    }
}

m1 = context.get("m1") || 0;
m2 = context.get("m2") || 0;
m3 = context.get("m3") || 0;

modem = context.get("modem") || 0;
uplink = context.get("uplink") || 0;
msg2.payload = "M " + modem + ", U " + uplink + ", m1 " + m1 + ", m2 " + m2 + ", m3 " + m3;

node.status({ text: "Uplink " + uplink + " Modem " + modem + " States " + m3 + " " + m2 + " " + m1 + "Ctr " + counter});

if (debug == 1) {
    node.warn("-----  New part of code  ---------");
    node.warn("modem state  " + modem);
    node.warn("Uplink value " + uplink);
    node.warn("Counters " + m1 + " " + m2 + " " + m3);
    node.warn("Values being sent");
    node.warn("modem is " + modem);
    node.warn("Modem value is " + mstate);
    node.warn("Uplink is " + uplink);
    node.warn("Ustate value is " + ustate);
    node.warn("Counter value is " + counter);
    node.warn("--------------");
}
//---------
//if (mstate === 0) {
if (counter === 0) {
    uplink = "UNKNOWN";
    msg = { "payload": '<font color = "red"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "red" };
    //msg.colour = "red";
} else

    //        if (modem > 3) {
//    if (mstate == 7) {
    if (counter == 7) {
        //node.status({fill: "green",text:machine3});
        msg = { "payload": '<font color = "lime"> <i class="fa fa-bullseye fa-2x"></i></font>', "colour": "lime" };
        //msg.msg.colour = "lime";
    } else

        //            if (modem > 2) {
//        if (mstate == 3 || mstate == 5 || mstate == 6) {
        if (counter == 2) {
            //node.status({fill: "yellow",text:machine2});
            msg = { "payload": '<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "green" };
            //msg.colour = "green";
        } else

            //                if (modem == 1) {
//            if (mstate == 1 || mstate == 2 || mstate == 4) {
            if (counter == 1) {
                //node.status({fill: "red",text:machine1});
                msg = { "payload": '<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "springgreen" };
                //msg.colour = "springgreen";
            }
if (ustate === 0) {
    msg1 = { "payload": '<font color = "red"> <i class="fa fa fa-bullseye fa-2x" ></i></font>', "colour": "red" };
    //msg1.colour = "red";
} else

    //        if (uplink > 3) {
    if (ustate == 7) {
        msg1 = { "payload": '<font color = "lime"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "lime" };
        //msg1.colour = "lime";
    } else

        //            if (uplink > 2) {
        if (ustate == 3 || ustate == 5 || ustate == 6) {
            msg1 = { "payload": '<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "green" };
            //msg1.colour = "green";
        } else

            //                if (uplink == 1)
            if (ustate == 1 || ustate == 2 || ustate == 4) {
                msg1 = { "payload": '<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "springgreen" };
                //msg1.colour = "springgreen";
            } else

                if (uplink == "UNKNOWN") {
                    msg1 = { "payload": '<font color = "black"> <i class="fa fa-question-circle fa-2x" ></i></font>', "colour": "black" };
                    //msg.colour = "black";
                }

//    msg2.payload = {"Modem":modem,"Uplink":uplink};
if (debug == 1) {
    //
    node.warn("Payload set to " + msg.payload);
}
//
//  2024 09 10
//
//  Because of a lot of false alerts, adding this to try and find out what is going on.
//msg.payload.sender = "TimePi";
//msg1.payload.sender = "TimePi";
return [msg, msg1, msg2];

//////////////==================

if (m1 == 1 && m2 == 1 && m3 == 1) 
//if (m1 == 1 || m2 == 1 || m3 == 1) 
{
    //  This is used to wipe values once 3 messages are received.
    //    node.warn("Wiping stored values.");

    //
    //      BLOCK DISABLED 2024 09 11
    //
    //context.set("m1", 0);
    //context.set("m2", 0);
    //context.set("m3", 0);
    //context.set("mstate", 0);
    //context.set("ustate", 0);
    //
    //      BLOCK DISABLED 2024 09 11
    //

    //    modem  = context.get("modem") || 0;
    //    uplink = context.get("uplink") || 0;
    //  Now send the message

    //}

    //if (m1 == 1 || m2 == 1 || m3 == 1) {
    //---------
    if (debug == 1) {
        //
    node.warn("Values being sent");
    node.warn("modem is " + modem);
    node.warn("Mode value is " + mstate);
    node.warn("Uplink is " + uplink);
    node.warn("Ustate value is " + ustate);
    }
    //---------
    if (mstate === 0) {
        uplink = "UNKNOWN";
        msg = { "payload": '<font color = "red"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "red" };
        //msg.colour = "red";
    } else

        //        if (modem > 3) {
        if (mstate == 7) {
            //node.status({fill: "green",text:machine3});
            msg = { "payload": '<font color = "lime"> <i class="fa fa-bullseye fa-2x"></i></font>', "colour": "lime" };
            //msg.msg.colour = "lime";
        } else

            //            if (modem > 2) {
            if (mstate == 3 || mstate == 5 || mstate == 6) {
                //node.status({fill: "yellow",text:machine2});
                msg = { "payload": '<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "green" };
                //msg.colour = "green";
            } else

                //                if (modem == 1) {
                if (mstate == 1 || mstate == 2 || mstate == 4) {
                    //node.status({fill: "red",text:machine1});
                    msg = { "payload": '<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "springgreen" };
                    //msg.colour = "springgreen";
                }
    if (ustate === 0) {
        msg1 = { "payload": '<font color = "red"> <i class="fa fa fa-bullseye fa-2x" ></i></font>', "colour": "red" };
        //msg1.colour = "red";
    } else

        //        if (uplink > 3) {
        if (ustate == 7) {
            msg1 = { "payload": '<font color = "lime"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "lime" };
            //msg1.colour = "lime";
        } else

            //            if (uplink > 2) {
            if (ustate == 3 || ustate == 5 || ustate == 6) {
                msg1 = { "payload": '<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "green" };
                //msg1.colour = "green";
            } else

                //                if (uplink == 1)
                if (ustate == 1 || ustate == 2 || ustate == 4) {
                    msg1 = { "payload": '<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "springgreen" };
                    //msg1.colour = "springgreen";
                } else

                    if (uplink == "UNKNOWN") {
                        msg1 = { "payload": '<font color = "black"> <i class="fa fa-question-circle fa-2x" ></i></font>', "colour": "black" };
                        //msg.colour = "black";
                    }

    //    msg2.payload = {"Modem":modem,"Uplink":uplink};
    if (debug == 1) {
        //
        node.warn("Payload set to " + msg.payload);
    }
    //
    //  2024 09 10
    //
    //  Because of a lot of false alerts, adding this to try and find out what is going on.
    //msg.payload.sender = "TimePi";
    //msg1.payload.sender = "TimePi";
    return [msg, msg1, msg2];

}

if (debug == 1) {
    //
    node.warn("NO MESSAGE");
}

return [null, null, msg2];

What have I done ITMT?

I've tried to change how the colours - dependent on how many online messages are - but it doesn't handle the value going down from 2 to 1.

I'm working on it.
But if you see the elephant.....

Thanks.