If and return messages from mqtt

How to return message from a payload with boolean true value, but only if it is equal to what I set?

Would you expand you issue a bit please
How does MQTT fit in?

You should be able to use a switch node to test the value and then a change to set the value you want.

1 Like

I managed to do it, but using several nodes, How would I do it using only the function node?

My program:

[{"id":"f9095345dc0c2946","type":"switch","z":"6b0282876aa2ecc3","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Liga","vt":"str"},{"t":"eq","v":"Desliga","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":350,"y":160,"wires":[["ecd87482388e507e"],["57195910e1cca66b"]]},{"id":"b323ec2241fbfad9","type":"mqtt in","z":"6b0282876aa2ecc3","name":"","topic":"PISTAO","qos":"2","datatype":"auto","broker":"c6a90f85a98db3a6","nl":false,"rap":true,"rh":0,"inputs":0,"x":230,"y":160,"wires":[["f9095345dc0c2946"]]},{"id":"57195910e1cca66b","type":"convert","z":"6b0282876aa2ecc3","name":"","convertTo":"boolean (0/1)","x":530,"y":180,"wires":[["baf7742df2534949"]]},{"id":"ecd87482388e507e","type":"change","z":"6b0282876aa2ecc3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":100,"wires":[["baf7742df2534949"]]},{"id":"baf7742df2534949","type":"s7 out","z":"6b0282876aa2ecc3","endpoint":"936469fb997a5f75","variable":"PISTAO","name":"","x":880,"y":160,"wires":[ ]},{"id":"c6a90f85a98db3a6","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"936469fb997a5f75","type":"s7 endpoint","transport":"iso-on-tcp","address":"192.168.0.101","port":"102","rack":"0","slot":"2","localtsaphi":"01","localtsaplo":"00","remotetsaphi":"01","remotetsaplo":"00","connmode":"rack-slot","adapter":"","busaddr":"2","cycletime":"1000","timeout":"2000","name":"","vartable":[{"addr":"M0.0","name":"Start"},{"addr":"M0.1","name":"RESET"},{"addr":"M0.2","name":"PISTAO"}]}]

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

I edited your post to fix it.

In a function node you could do something like this:

if (msg.payload == 'Liga') {
    msg.payload = true;
}
if (msg.payload == 'Desliga') {
    msg.payload = 1;
}

return msg;

I'm unsure if you want the scond one to be a 1 or 0

1 Like

"ReferenceError: msg is not defined"
It's giving this error, by the way where can I learn to program like this?

Put a debug node (set to display the complete msg object) on the output of the node feeding the function node and show the results.

Thereā€™s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

Take a JavaScript tutorial like this

1 Like

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