# This is not working and I don't understand why. (Complicated kinda)

**URL:** <https://discourse.nodered.org/t/this-is-not-working-and-i-dont-understand-why-complicated-kinda/91158>\
**Category:** General\
**Created:** [21 September 2024 08:07 UTC](https://discourse.nodered.org/t/this-is-not-working-and-i-dont-understand-why-complicated-kinda/91158 "2024-09-21T08:07:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 September 2024 08:07 UTC](https://discourse.nodered.org/t/this-is-not-working-and-i-dont-understand-why-complicated-kinda/91158/1 "2024-09-21T08:07:07Z")

</div>

Yeah, I write _dog's breakfast_ for code.

That's who I am.

I am TRYING to optimise this code from the already big dog's breakfast.

The existing one is convoluted and I've found problems with it.  
(calling the `setBit` and `clearBit` routines.)  
_Moving on_

I receive messages from my 3 devices. (Format below)  
and they tell me the state of the nation WRT my uplink.

It kind of did work a long time ago, but I fear that was more luck than coding.  
And when I noticed the first stage of bad coding, the whole thing just fell apart.

The biggest problem - IMO - is that I didn't do a good job of making routines.

This opened/s a whole new can of worms with indenting.  
But I am wanting to look past that as .... _decorative_ more than useful.

To declare:  
When I do `{ }` I do it _differently_ and so there is a bit of confusion

My way:

```auto
if (j == "something)
{
      // do stuff here
}
else
if (j == "something else
{
    // other stuff
}

```

The way it is done is like this:

```auto
if (j == "something){
      // do stuff here
}

```

Not a big deal. I can usually read around that.  
But the `else`.... _I digress_

(off topic) why are the lines above different colours?  
White, pink and green.

So I stepped way back and started anew.

And I made it handle only ONE sender's messages.

That looked to work so I _expanded_ it to handle all `3`.

At this stage I thought I could put the _block_ of code in the `switch()` block.

So I copied it three times within the constrains of the `case{ }` part.

_Should work_

It doesn't.

I can `set` all the bits for all `3` devices but can't `clear` them.

The message:  
(example)  
`{"Who":"TimePi","router":"online","pip":"online","modem":"online","internet":"offlline"}`

That one is supposed to clear the `internet` bit (`0`) of the node's context.  
It can set it quite easily - tested.

But it all falls apart when I try to clear the bit.

Sorry this is a big ask.  
But I fear I've missed the elephant in my travels.

Someone - please?

```auto
/////////////////////////////////
//
// 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 sender = msg.payload.Who

let router = msg.payload.router
let pip = msg.payload.pip
let modem = msg.payload.modem
let internet = msg.payload.internet

let bit
let rstate = context.get("rstate") || 0
let pstate = context.get("pstate") || 0
let mstate = context.get("mstate") || 0
let istate = context.get("istate") || 0

//node.warn("Sender " + sender)
//node.warn("bit " + bit) // This doesn't work now I've moved all the code into blocks.
//node.warn("Router " + router)
//node.warn("PIP " + pip)
//node.warn("modem " + modem)
//node.warn("Internet " + internet)

//node.warn("rstate " + rstate)
//node.warn("pip " + pstate)
//node.warn("mstate " + mstate)
//node.warn("istate " + istate)

switch (sender) {
    case "TimePi":
        bit = 0
        // Router
        if (router == "offline") {
            //
            // node.warn("router off line")
            rstate = clearBit(rstate, bit)
            context.set("rstate", rstate)
        } else
        // if (router == "online")
        {
            //
            // node.warn("router on line")
            rstate = setBit(rstate, bit)
            context.set("rstate", rstate)
        }
        // Publib IP
        if (pip == "offline") {
            //
            // node.warn("pip off line")
            pstate = clearBit(pstate, bit)
            context.set("pstate", rstate)
        } else
        // if (pip == "online") 
        {
            //
            // node.warn("pip on line")
            pstate = setBit(pstate, bit)
            context.set("pstate", rstate)
        }
        // modem
        if (modem == "offline") {
            //
            // node.warn("modem off line")
            mstate = clearBit(mstate, bit)
            context.set("mstate", rstate)
        } else
        // if (modem == "online") 
        {
            //
            // node.warn("modem on line")
            mstate = setBit(mstate, bit)
            context.set("mstate", rstate)
        }
        // Internet IP
        if (internet == "offline") {
            //
            // node.warn("internet off line")
            istate = clearBit(istate, bit)
            context.set("istate", rstate)
        } else
        // if (internet == "online") 
        {
            //
            // node.warn("internet on line")
            istate = setBit(istate, bit)
            context.set("istate", rstate)
        }
        break
    case "TelePi":
        bit = 1
        // Router
        if (router == "offline") {
            //
            // node.warn("router off line")
            rstate = clearBit(rstate, bit)
            context.set("rstate", rstate)
        } else
        // if (router == "online")
        {
            //
            // node.warn("router on line")
            rstate = setBit(rstate, bit)
            context.set("rstate", rstate)
        }
        // Publib IP
        if (pip == "offline") {
            //
            // node.warn("pip off line")
            pstate = clearBit(pstate, bit)
            context.set("pstate", rstate)
        } else
        // if (pip == "online") 
        {
            //
            // node.warn("pip on line")
            pstate = setBit(pstate, bit)
            context.set("pstate", rstate)
        }
        // modem
        if (modem == "offline") {
            //
            // node.warn("modem off line")
            mstate = clearBit(mstate, bit)
            context.set("mstate", rstate)
        } else
        // if (modem == "online") 
        {
            //
            // node.warn("modem on line")
            mstate = setBit(mstate, bit)
            context.set("mstate", rstate)
        }
        // Internet IP
        if (internet == "offline") {
            //
            // node.warn("internet off line")
            istate = clearBit(istate, bit)
            context.set("istate", rstate)
        } else
        // if (internet == "online") 
        {
            //
            // node.warn("internet on line")
            istate = setBit(istate, bit)
            context.set("istate", rstate)
        }
        break
    case "BedPi":
        bit = 2
        // Router
        if (router == "offline") {
            //
            // node.warn("router off line")
            rstate = clearBit(rstate, bit)
            context.set("rstate", rstate)
        } else
        // if (router == "online")
        {
            //
            // node.warn("router on line")
            rstate = setBit(rstate, bit)
            context.set("rstate", rstate)
        }
        // Publib IP
        if (pip == "offline") {
            //
            // node.warn("pip off line")
            pstate = clearBit(pstate, bit)
            context.set("pstate", rstate)
        } else
        // if (pip == "online") 
        {
            //
            // node.warn("pip on line")
            pstate = setBit(pstate, bit)
            context.set("pstate", rstate)
        }
        // modem
        if (modem == "offline") {
            //
            // node.warn("modem off line")
            mstate = clearBit(mstate, bit)
            context.set("mstate", rstate)
        } else
        // if (modem == "online") 
        {
            //
            // node.warn("modem on line")
            mstate = setBit(mstate, bit)
            context.set("mstate", rstate)
        }
        // Internet IP
        if (internet == "offline") {
            //
            // node.warn("internet off line")
            istate = clearBit(istate, bit)
            context.set("istate", rstate)
        } else
        // if (internet == "online") 
        {
            //
            // node.warn("internet on line")
            istate = setBit(istate, bit)
            context.set("istate", rstate)
        }
        break
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [21 September 2024 08:27 UTC](https://discourse.nodered.org/t/this-is-not-working-and-i-dont-understand-why-complicated-kinda/91158/2 "2024-09-21T08:27:48Z")

</div>

Andrew, here is a link to an explanation of the `JavaScript if, else, and else if` statements. Take a read and see if that clears it up for you.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 September 2024 08:29 UTC](https://discourse.nodered.org/t/this-is-not-working-and-i-dont-understand-why-complicated-kinda/91158/3 "2024-09-21T08:29:17Z")

</div>

Um, the link?

🤷

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [21 September 2024 08:30 UTC](https://discourse.nodered.org/t/this-is-not-working-and-i-dont-understand-why-complicated-kinda/91158/4 "2024-09-21T08:30:14Z")

</div>

oops, haven't had coffee yet

> **[W3Schools.com](https://www.w3schools.com/js/js_if_else.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 September 2024 08:30 UTC](https://discourse.nodered.org/t/this-is-not-working-and-i-dont-understand-why-complicated-kinda/91158/5 "2024-09-21T08:30:29Z")

</div>

No worries.

Thanks.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [21 September 2024 08:51 UTC](https://discourse.nodered.org/t/this-is-not-working-and-i-dont-understand-why-complicated-kinda/91158/6 "2024-09-21T08:51:50Z")

</div>

> [@Trying\_to\_learn](#):
>
> (off topic) why are the lines above different colours?  
> White, pink and green.

Because you are only putting on a quote on the beginning of 'something' & 'something else' and not also at the end of the string...

"something else  
"something else"

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 September 2024 09:01 UTC](https://discourse.nodered.org/t/this-is-not-working-and-i-dont-understand-why-complicated-kinda/91158/7 "2024-09-21T09:01:25Z")

</div>

Thanks.

I'm kinda overwhelmed with all the stuff I am trying.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 September 2024 09:26 UTC](https://discourse.nodered.org/t/this-is-not-working-and-i-dont-understand-why-complicated-kinda/91158/8 "2024-09-21T09:26:18Z")

</div>

# FOUND IT!

Didn't change all the names.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 October 2024 09:26 UTC](https://discourse.nodered.org/t/this-is-not-working-and-i-dont-understand-why-complicated-kinda/91158/9 "2024-10-05T09:26:58Z")

</div>

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