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:
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:
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:
if (context.get("colour") != undefined)
{
colour = context.get("colour")
}
else
{
colour = "yellow"
}
(So if the context
is empty, it sets the colour
to yellow.
Ready?
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.