Just started playing with node red and im loving it but due to my nascent skills developing, I am struggling to understand why msg.label only returns 0. For example the var equipStatus = msg.payload[0]['Equipment status']; is returning 7.
Not knowing what your incoming payload array contains, it's hard to say... Can you edit your post to show the input data?
FYI, when you post a chunk of code, it's clearest if you put a line with 3 backtics, like: ```
before and after your code... That makes it easier to read, and doesn't mess with any quotes or formatting, making the code easily imported by someone who wants to test it.
There is no place in this chunk of code that assigns the value 0 to msg.label. If msg.label is indeed being assigned to 0 then it is happening outside of this code. Perhaps you could share the whole flow?
The binary and shift operations seem to be all right. You said that msg.payload[0]['Equipment status'] value is 7, so this is what is assigned to the variable equipStatus. The second line will assign the variable chargStatus with value 1. Therefore the switch case 1 will be true and msg.label will be assigned the value "Float (13.8V)"
I would suggest that there is a better way to code this logic without using if statement or switch/case but it is better that you sort the code out first.
I don't think you mean to bit-shift 4 in your "if" test... perhaps you should instead check the value of the charge status to see if it's greater than or equal to 4? Either way, I prefer to use the status as an index into an array of status strings -- something like this (untested):
var equipStatus = msg.payload[0]['Equipment status'];
var chargStatus = 0b11 & (equipStatus >> 2);
var statusLabels = [
"Not Charging",
"Float (13.8V)",
"Boost (14.4V)",
"Equalization (14.6V)"
];
var eStatus = "FAULT";
if (chargStatus < statusLabels.length) {
eStatus = statusLabels[chargStatus];
}
msg.label = eStatus;
return msg;
Apologies still a bit inconsistent and i'm not sure as to why. As you can see the ['Equipment status'] payload is returning "11" but the function is still returning 0 when it should be 1.
If comment the first line and use "var e= 11" the function works.
var e = msg.payload[0]['Equipment status'];
//var e = 11
var c = 0b11 & (e >> 2);
switch (c) {
case 0:
var eStatus = "Not charging";
msg.label = eStatus;
msg.icon = "fa fa-bed fa-2x nr-dashboard-ok"
//node.warn("0");
return msg;
case 1:
eStatus = "Float (13.8V)";
msg.label = eStatus;
msg.icon = "fa fa-ship fa-2x nr-dashboard-ok"
//node.warn("1");
return msg;
case 2:
var eStatus = "Boost (14.4V)";
msg.label = eStatus;
//node.warn("2");
return msg;
case 3:
var eStatus = "Equalization (14.6V)";
msg.label = eStatus;
//node.warn("3");
return msg;
}
if (equipStatus >> 4) {
var eStatus = "FAULT";
msg.label = eStatus;
//node.warn("4");
return msg;
}
What do you get displayed in the debug panel if you change the first line of your code as below?
node.warn(msg.payload[0]['Equipment status']);
var e = 11;
var c = 0b11 & (e >> 2);
I wonder if msg.payload[0]['Equipment status'] returns a binary value, in which case it would be evaluated to 3 (and c evaluated to 0), explaining the end result.
So, it is like you said (and tested). When forcing the variable e to 11 the code in the function works and produces the correct label.
You have to look for the issue outside the code. Something between reading the database and delivering the data to the function node. For some reason the value arriving is "undefined" and this produces an undesirable (but predictable) label.