# Help with what is happening with code

**URL:** <https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645>\
**Category:** General\
**Created:** [21 July 2024 11:20 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645 "2024-07-21T11:20:37Z")\
**Posts on this page:** 17\
**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 July 2024 11:20 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/1 "2024-07-21T11:20:37Z")

</div>

# Folks, I am a gluten for punishment.

Chat GPT took me down some weird places. It kind of helped but then again: it didn't.

# Scenario:

I have an alternating output (on/off) which works fine as is, but - of course - I complicated things and how they don't.

## Basic overview:

The `subflow` toggles the message received by the `button` node.  
On/Off.  
It has text and colour to control/adjust how the button looks.  
If the button is pressed, it _toggles_ things and .... anyway.  
That works.  
P.S. The `payload` also toggles from `0` to `1` for OFF and ON.

## Complication:

There is _another factor_ that determines how the button looks.  
That is alternating weeks.

So now rather than just having fixed text (explaining what the button does) I have thought I would also _tweak_ the text.

## Truth table - of sorts:

OFF - `msg.colour` = `firebrick` `msg.txt` = "NAME"  
ON (week 0) - `msg.colour` = `springgreen` `msg.txt` = `this week`  
ON (week 1) - `msg.colour` = `green` `msg.txt` = `next week`

The _problem_ is this:  
It is `ON`, and I press the `OFF` button. It turns from what ever colour/text to `firebrick` and shows the NAME for `msg.txt`.  
Then when I turn it `ON` again it restores the previous text and colour.  
If it is `ON` and the week goes to the other, the text and colour adjust.

So there are two nodes sending messages:

# Code:

## node 1:

```auto
const timestamp = msg.payload;
const date = new Date(timestamp);
let msg2 = {}

// Calculate the week number
const weekno = Math.floor((timestamp + 345600000) / 604800000);

// Determine the parity of the week (odd or even)
const result_ = Number(weekno) % 2;

// Get the weekday (0 for Sunday, 1 for Monday, ..., 6 for Saturday)
const weekday = date.getDay();

if (result_ === 0) {
    if (weekday == 6) {
        //
        msg.showBan = false;
        msg.flag = "Wash";
    }
    else {
        //
        msg.showBan = true;
        msg.flag = " ";
    }
}
else {
    msg.showBan = true;
    msg.flag = " ";
}

//// Assign weekday to the message object
//msg.weekday = weekday;

// Create msg2 to adjust colour of button
if (msg.flag == "Wash")
{
    msg2.colour = "springgreen"
// msg2.txt = "Wash cloths"
    msg2.txt = "This week"
    msg2.week = 0
}
if (msg.flag == " ")
{
    msg2.colour = "green"
    msg2.txt = "Next week"
    msg2.week = 1
}

return [msg,msg2];

```

Using `msg2` for the input.

## node 2:

```auto
let pass = context.get("pass") || false
let colour = ''
if (context.get("colour") != undefined)
{
    colour = context.get("colour")
}
else
{
    colour = "yellow"
}

let text = msg.txt
const message = msg.payload
const week = msg.week

node.warn("Initial colour is " + colour)

if (week !== undefined) 
{
    node.warn("Week setting colour")
    context.set("colour", colour)
    context.set("text",text)
    node.warn("Setting colour to " + colour + " And text to " + text)
    //return msg
}

text = context.get("text") || "oops"

if (message == 0)
{
    // Inactive
    node.warn("InActive")
    //msg.colour = colour
    msg.text = text
}

if (message == 1)
{
    // Active
    node.warn("Active")
    msg.colour = colour
    msg.txt = text
}

node.status({text:colour})

return msg;

```

This receives the `msg2` from the first node OR it receives (alternately) these two messages:  
`{"_msgid":"5669a8dea20a1f33","payload":0,"topic":"","colour":"firebrick","txt":"Wash Cloths"}`  
or  
`{"_msgid":"230ad34770d6b9c5","payload":1,"topic":"","colour":"springgreen","txt":"Wash Cloths"}`

Despite my best efforts I am getting WEIRD messages.

Forgive the messy way I test if `context` exists. But I am not sure if having ` || "text"` works if the context is not set and that sets a _default_ value.

## So where's the confusion?

Indulge me:  
I have just _edited_ the second node's code. So all its context is empty.  
It receives a message from the other node.

And to put things in perspective I'll go back and emphasise this bit of code:

```auto
if (context.get("colour") != undefined)
{
    colour = context.get("colour")
}
else
{
    colour = "yellow"
}

```

(So if the `context` is empty, it sets the `colour` to yellow.

 ![Screenshot from 2024-07-21 21-13-54](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/05b1f4e9ea53b461fc43b1ecb869724eff102020.png)

Ready? 😉

 ![Screenshot from 2024-07-21 21-14-05](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b045b6b3439679d88d240df56c56e9f28095aa75.png)

HOW did it change from `yellow` to `green`?

## So anyway:

Have I explained it clearly enough?

I would like to make the _testing_ better, but I can't see any _shortcuts_ to doing it.

Thanks in advance.

---

<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 July 2024 11:45 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/2 "2024-07-21T11:45:10Z")

</div>

_Mr CHAT GPT_ threw this at me....  
Along with a bit of digging by me.

```auto
let pass = context.get("pass") || false; // Initialize pass variable with default value from context
let colour = context.get("colour") || "yellow"; // Initialize colour with default value "yellow" if not set in context

let text = msg.txt; // Initialize text variable from msg.txt
const message = msg.payload; // Initialize message variable from msg.payload
const week = msg.week; // Initialize week variable from msg.week

// Set colour and text in context if week is defined
if (week !== undefined) {
    context.set("colour", msg.colour);
    context.set("text", text);
}

// Retrieve text from context or set default "oops"
text = context.get("text") || "Week not set";

// Handle message values
if (message === 0) {
    // Set text for inactive state
    msg.text = text;
}

if (message === 1) {
    // Set colour and text for active state
    msg.colour = colour;
    msg.txt = text;
}

node.status({ text: msg.colour }); // Update node status with current colour

return msg; // Return updated msg object

```

The _Problem_ was my constant use of `colour` rather than `msg.colour` in later lines.

I changed it to `msg.colour` and it seems to be working better.  
(So maybe it is resolved. But I am still not confident enough to believe everything computers tell me.)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 July 2024 12:42 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/3 "2024-07-21T12:42:37Z")

</div>

> [@Trying\_to\_learn](#):
>
> But I am still not confident enough to believe everything computers tell me

And neither should you be! The so-called "AI" tools are really "Large Language Model" machine learning tools. They only "know" what someone has already thought and then written down somewhere it can be sucked into the model.

Though that is a slightly simplistic view of course with a couple of exceptions. They CAN "infer" relationships between people's content that isn't obvious to humans - that is one of the real benefits but also one of the biggest threats because inference based on language can never be 100% accurate.

The other exception is that, as people start to publish stuff that AI's have generated, those may also get fed back into models which can really mess up reality!

Anyway, bottom line is that you wouldn't 100% trust what someone in an online forum told you and you shouldn't 100% trust what an LLM tells you.

Like any other Internet "knowledge", you need to apply your own thinking and knowledge. The models can be great boons but you need to sanity check everything.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 July 2024 12:56 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/4 "2024-07-21T12:56:04Z")

</div>

By the way, the Inject Enhanced contributed node has even/odd week numbers available.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6d865910ea6b33cdc70b680c27765abafa546a8a.png)

Personally, I would have a separate flow that generates an odd/even flag, at maybe just after midnight on Monday morning. I would output that to MQTT and save to a Node-RED global variable for use anywhere.

Then you could use the MQTT change as an event but more importantly in this case, use the global to help populate your web page.

---

<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 July 2024 20:43 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/5 "2024-07-21T20:43:35Z")

</div>

Wow, that looks nice.

But this is an established machine and it is running the older NR.

3.0.2

So the code:

Was my adjustment the solution?  
`msg.colour` vs `colour`

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 July 2024 21:00 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/6 "2024-07-21T21:00:21Z")

</div>

> [@Trying\_to\_learn](#):
>
> Was my adjustment the solution?  
> `msg.colour` vs `colour`

If it works, probably 🙂

> [@Trying\_to\_learn](#):
>
> But this is an established machine and it is running the older NR.
> 
> 3.0.2

I don't think that node is dependent on Node-RED v4. I've been using it for ages on my live system. Certainly from early v3 maybe even v2, can't remember.

---

<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 July 2024 21:02 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/7 "2024-07-21T21:02:23Z")

</div>

I did a quick search but am not clear on the name.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 July 2024 21:15 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/8 "2024-07-21T21:15:20Z")

</div>

Ah, I forgot, it isn't obvious.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/90fcb0a967c391fbb45a39d876b9921b6390a146.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/5/f5973e2d65b2f968987a12ccbcb3ec9f2648af51.png)

I use a number of their nodes, really useful stuff.

---

<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 July 2024 21:22 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/9 "2024-07-21T21:22:14Z")

</div>

Um, sorry.

But even searching `inject enhanced` doesn't give me anything useful.

Inject gives me a lot of choices. Inject enhanced gives me none.

Found it.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 July 2024 21:33 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/10 "2024-07-21T21:33:44Z")

</div>

It is called `node-red-contrib-sun-position`.

---

<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:** [22 July 2024 05:46 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/11 "2024-07-22T05:46:09Z")

</div>

Yeah, ok, but the thing is:

On a _valid week_ I can se the colour to something.  
And on the other weeks, I can se the colour to something else.

So looking at it and seeing the COLOUR I can tell what's going on too.

There may be a way to do that other ways, but this way is the way I know.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [22 July 2024 12:01 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/12 "2024-07-22T12:01:13Z")

</div>

I added something to my own test dashboard:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/71deb8ba56e6dff194aa29ad6c007556d18b0103.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/9/c905286014c3c6f12563a40ad33d49a94a80a0e2.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/4/a49c9e690d0329e2fc563220bb3e391c7becb23d.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/4/646c0f01f58127460e78e4dfe0942e43d0ce518f.png)

So the even/odd week info is fully independent of everything else. There is a (UIBUILDER in my case) page with a response flow that currently only has responses from buttons that control my PC monitors brightness (not relevant here of course). But there is another flow (3rd image) that monitors the week via MQTT and updates the UI (noting that the output is cached).

All really simple. I could, of course, go further and make the UI into a button to be clicked if the sheets have been washed - but I'm mostly too lazy to do that. 😀 If it isn't a sheet washing week, it looks like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f275e47a16d271e818c31142fd818d3b6c4c8eb.png)

Yes, the colours need sorting out but this was only a test after all.

---

<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:** [22 July 2024 21:19 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/13 "2024-07-22T21:19:32Z")

</div>

Thanks.

Alas most of my stuff is not on the new dashboard.

Am still very much at the starting gate for learning that stuff.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 July 2024 00:18 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/14 "2024-07-23T00:18:43Z")

</div>

> [@Trying\_to\_learn](#):
>
> Alas most of my stuff is not on the new dashboard.

Well, as you know, I don't really use Dashboard 1 or 2 other than for testing or really simple stuff. Most things are now so easy to do with native HTML and JS that, with a little help from UIBUILDER, I never need the use of Vue or Angular even for complex requirements.

---

<div class="post-metadata">

**Author:** ![Sandrei](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Sandrei](https://discourse.nodered.org/u/Sandrei)\
**Post date:** [26 July 2024 10:11 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/15 "2024-07-26T10:11:14Z")

</div>

Hi, if you share your pc via anydesk, I can assist you.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [27 July 2024 02:00 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/16 "2024-07-27T02:00:46Z")

</div>

@Sandrei That's not really the way we do things on this forum, apart from the obvious security concerns, questions and answers posted here can be peer reviewed, and also benefit others with the same issues.

I'm sure @Trying_to_learn would be more than happy to receive some help via the forum 😉

---

<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:** [25 October 2024 02:01 UTC](https://discourse.nodered.org/t/help-with-what-is-happening-with-code/89645/17 "2024-10-25T02:01:16Z")

</div>

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