Using 2 msg.payload with an "if" structure

#1

Hello, I am new here, I am trying to create a condition using mqtt nodes and openHAB nodes where if my openHAB item changes its state and I recieve the correct mqtt command it activates another mqtt command, but I don't know how to combine the information from these to places in one "if" node!

Here are my nodes:

Here the codes of the "if" nodes:

For the first one:

if (msg.payload == "ON"){
msg.payload = "BIENVENIDO";
}
else {
msg.payload = " ";
}
return msg.payload;

For the second one:

if (msg.payload == "BIENVENIDO" && msg.payload <= 23 ){
msg.payload = "ON";
}
else {
msg.payload = "OFF";
}
return msg;

I just think that I have to change the name of the msg.payload in order that it is associated with the first node of openHAB or with the out of the first if, but I don't know how to change this name!! I only know how to use msg.payload and it gets wrong.

#2

A node has no memory of messages, it receives a message, does something to it and passes it on.

So msg.payload cannot be == "BIENVENIDO" AND msg.payload <= 23

You will need to think about coding it a different way, or to store a value that your second flow and can look up. (see https://nodered.org/docs/writing-functions#storing-data )

If you look at the change node you can save a value to a global or flow context

In your other flow you can do the reverse.
i.e. set msg.save_value
to global.savedvalue

and then change you IF statement

#3

I would do it like this, no need for global variables.

[{"id":"3e4a4081.cbe08","type":"debug","z":"514a90a5.c7bae8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":578,"y":495,"wires":[]},{"id":"9c19a1a9.a2da9","type":"inject","z":"514a90a5.c7bae8","name":"","topic":"topic1","payload":"BIENVENIDO","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":132.5,"y":449,"wires":[["9ec7b65f.640f38"]]},{"id":"bec7f206.3dbca8","type":"inject","z":"514a90a5.c7bae8","name":"","topic":"temperature","payload":"24","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":124.5,"y":589,"wires":[["9ec7b65f.640f38"]]},{"id":"81be4b83.6170b","type":"inject","z":"514a90a5.c7bae8","name":"","topic":"temperature","payload":"22","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":121,"y":624,"wires":[["9ec7b65f.640f38"]]},{"id":"14efe62d.28395a","type":"inject","z":"514a90a5.c7bae8","name":"","topic":"topic1","payload":"Something Else","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":139,"y":482,"wires":[["9ec7b65f.640f38"]]},{"id":"9ec7b65f.640f38","type":"join","z":"514a90a5.c7bae8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":391.5,"y":575,"wires":[["6addbc3c.268eec"]]},{"id":"6addbc3c.268eec","type":"function","z":"514a90a5.c7bae8","name":"Test","func":"if (msg.payload.topic1 === \"BIENVENIDO\"  &&  msg.payload.temperature <= 23 ) {\n    msg.payload = \"ON\";\n} else {\n    msg.payload = \"OFF\";\n}\nreturn msg;","outputs":1,"noerr":0,"x":529.5,"y":574,"wires":[["3e4a4081.cbe08"]]}]
1 Like
#4

Just a quick hint: for such a functionality it is not required to use the "function" node. As an exercise try to always avoid the usage of a "function" node and use the "switch", "change", "join" and "split" nodes instead.
After you get used to it you will see that "function" is only necessary for extreme situations :slight_smile:

#5

For an assignment involving testing two elements I tend to use a Function node even though it can be done with a Change node. ? : operators can get a bit heavy with multiple tests and I think ease of understanding is often more important than absolute run-time efficiency.