Two buttons activation

Hi,

I've a switch which generates a payload 'on' when ON and 'off' when OFF.
Then I've a button which generates a topic 'turnon' when clicked.
The two get together into a function shown below.
The idea is that the function should generate a 1 only when the button is clicked and the switch is ON.

I cannot get it right. I appreciate some help.
TIA


var act = context.get("activate");
if (msg.payload == 'on'){
    act = 1;
    context.set("activate", act)
}
else {
    act = 0;
    context.set("activate", act)
}
if(msg.topic == 'turnon' && act == 1){
    msg.payload = 1;
}
else{
     msg.payload = 0;
}

return msg;

Hi and welcome to the wonderful world of Node-Red.

Its good to see you are trying, and that you have the idea of context already.

But let's look at what is happening in the code.

=-=-=-=-

1 look at context for "active".
2 check the payload and see if it == "on"
3 set act to 1
4 save this to context "active"
5 endif

Ok. So that is ok. Could be written differently, but is neither here nor there.

6 else
7 set "act" to 0
8 save this to context "active"
9 close }

That is the end of the code to handle that message.

10 now look at "msg.topic" and see if it == "turnon" and if "act" is == 1
11 set payload to 1
12 endif
13 set payload to 0
14 close }
15 return msg

Part of the problem is you wait until the end to return the payload.

Another part is that this is processing two very different messages and there is only one "return (msg)" (msg part optional)

The first part of the code is looking at the message and setting "active" accordingly.
That should be it.

Also the way you parse the payload is complicating things.

I don't want to tell you the right and wrong way to code, but indulge me this for the first half (lines 1 - 9) though there are more lines:

var act = context.get("activate");
if (msg.payload == "on"{
   context.set("activate",1);
   return;
} else
if (msg.payload == "off");
   context.set("activate",0);
   return;
}

This is because unless you want one message to be able to affect the output.
But your implication is that there are two needed.

That looks at the incoming message and acts if the msg.payload == "on" or == "off" and then it exits the code.

It has also set the "activate" to the state of 1 or 0. Which persists to the next incoming message.

Now, if neither of those things happened you get to the rest of the code.....

I'll leave it there for now and see if you can get it working from what I have shown you here.

Hope it helps.

Thanks for getting back to me.

I plugged the changes but now, I don't get an output. I'm not much familiar with js (I know, I don't belong here) but, do I really need to use 'return'?

var act = context.get("activate");
if (msg.payload == 'on'){
    context.set("activate", 1);
    return;
}
else {
    context.set("activate", 0);
    return;
}
msg.payload = act;

/*if(msg.topic == 'turnon' && act == 1){
    msg.payload = 'here';
}
else{
     msg.payload = 'there';
}
*/
return msg;

Even with the following code I get no response :man_shrugging:

var act = context.get("activate");
if (msg.payload == "on"){
    context.set("activate", 1);
    return;
}
else {
    context.set("activate", 0);
    return;
}

if(msg.topic == "turnon" && act == 1){
    msg.payload = "here";
    return msg;
}
else{
     msg.payload = "there";
     return msg;
}

Ok, the following code worked.

var act = context.get("activate")||0;
if (msg.payload == "on"){
    context.set("activate", 1);
    return;
}
else if (msg.payload == "off"){
    context.set("activate", 0);
    return;
}

if(msg.topic == "turnon" && act == 1){
    msg.payload = 0;
    return msg;
}
else{
     msg.payload = 1;
     return msg;
}
1 Like

Good you worked it out yourself.

Can you now see why in the first half you need to return?

It is also nice to see how you shortened setting the context.

It all gets easier the more you try things, and there is no simple solution.
Some pick it up easy, some don't.

Good luck with things and I hope you enjoy using Node-Red.

This' the second weekend I'am dedicating to learn NR. So, I still have a long road ahead of me; but, definitely, need to learn JS. Thank you.

1 Like

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