Function node output from mqtt message to switch gpio

Hello Guys i am new on node red and have a "Little" question about the function nodes.

I get mqtt Messages from an "Node MCU V3 Lolin", and put out a kind visualisation on the nodered/ui site. The whole System is a Raspberry Pi 3b+.
That works Pretty fine, the Messages come in an i can see the Values on Gauges an so on.
Now i would set an gpio Output on the raspi, if the Value (esp32/p10 and esp32/p25) under "50.00" the gpio Pin 11 must set true , if the Values over 50 Pin 11 must set false and Pin13 must set true, if the Value over "100.00" Pin 11,13 must be set false and Pin 15 to true.

I used a function node to test the Value readings and Change a text on the UI Site, this is sometimes working and sometimes not. In this case would say i must use a for while loop or someting else .

Anyone have an idea to get this thing working?

[{"id":"8d6fc00e.79c98","type":"tab","label":"Raumluftueberwachung Messungen","disabled":false,"info":""},{"id":"e5bbc414.047a58","type":"mqtt out","z":"8d6fc00e.79c98","name":"","topic":"esp32/output","qos":"","retain":"","broker":"ffe7b932.876ae8","x":270,"y":980,"wires":[]},{"id":"e25998fd.40c348","type":"mqtt in","z":"8d6fc00e.79c98","name":"","topic":"esp32/Temperatur","qos":"2","datatype":"auto","broker":"ffe7b932.876ae8","x":130,"y":401,"wires":[["94213d28.d3d4f","d9755720.400da8","9c962029.c6c61"]]},{"id":"f39fb428.731e38","type":"ui_switch","z":"8d6fc00e.79c98","name":"","label":"Output","tooltip":"","group":"47d4aadb.665534","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"","offvalue":"off","offvalueType":"str","officon":"","offcolor":"","x":129,"y":980,"wires":[["e5bbc414.047a58"]]},{"id":"94213d28.d3d4f","type":"debug","z":"8d6fc00e.79c98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":327,"y":368,"wires":[]},{"id":"49584a91.d303f4","type":"mqtt in","z":"8d6fc00e.79c98","name":"","topic":"esp32/Luftfeuchte","qos":"2","datatype":"auto","broker":"90edecf3.20888","x":130,"y":680,"wires":[["8c0db659.80f398","9e320ec4.ad1d6"]]},{"id":"8c0db659.80f398","type":"debug","z":"8d6fc00e.79c98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":317,"y":647,"wires":[]},{"id":"d9755720.400da8","type":"ui_chart","z":"8d6fc00e.79c98","name":"","group":"47d4aadb.665534","order":4,"width":0,"height":0,"label":"Temperatur","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"-10","ymax":"85","removeOlder":"12","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":327,"y":428,"wires":[[]]},{"id":"9e320ec4.ad1d6","type":"ui_gauge","z":"8d6fc00e.79c98","name":"","group":"47d4aadb.665534","order":6,"width":0,"height":0,"gtype":"gage","title":"Luftfeuchte","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b3d9","#0073e6","#001bd7"],"seg1":"33","seg2":"66","x":317,"y":707,"wires":[]},{"id":"1429e4bb.7620bb","type":"debug","z":"8d6fc00e.79c98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":330,"y":219,"wires":[]},{"id":"be7256b9.d02688","type":"ui_gauge","z":"8d6fc00e.79c98","name":"","group":"47d4aadb.665534","order":8,"width":0,"height":0,"gtype":"gage","title":"Partikel < 10 µg/m³","label":"µg/m³","format":"{{value}}","min":0,"max":"999","colors":["#00b3d9","#0073e6","#001bd7"],"seg1":"33","seg2":"66","x":330,"y":260,"wires":[]},{"id":"fc93c4ad.93b388","type":"mqtt in","z":"8d6fc00e.79c98","name":"","topic":"esp32/p10","qos":"0","datatype":"utf8","broker":"90edecf3.20888","x":120,"y":239,"wires":[["1429e4bb.7620bb","be7256b9.d02688","af4b8366.f8761","abb3ee6a.d6ce9"]]},{"id":"bb264d7c.3382f","type":"debug","z":"8d6fc00e.79c98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":330,"y":55,"wires":[]},{"id":"c828fe56.22d1","type":"ui_gauge","z":"8d6fc00e.79c98","name":"","group":"47d4aadb.665534","order":9,"width":0,"height":0,"gtype":"gage","title":"Partikel < 2.5 µg/m³","label":"µg/m³","format":"{{value}}","min":0,"max":"999","colors":["#00b3d9","#0073e6","#001bd7"],"seg1":"33","seg2":"66","x":350,"y":115,"wires":[]},{"id":"62551a23.54cc54","type":"mqtt in","z":"8d6fc00e.79c98","name":"","topic":"esp32/p25","qos":"2","datatype":"utf8","broker":"90edecf3.20888","x":120,"y":75,"wires":[["bb264d7c.3382f","c828fe56.22d1"]]},{"id":"70c662d3.84c96c","type":"mqtt in","z":"8d6fc00e.79c98","name":"","topic":"esp32/Luftdruck","qos":"2","datatype":"auto","broker":"90edecf3.20888","x":120,"y":560,"wires":[["188806c.3e99af9","d67d1b4e.910588"]]},{"id":"188806c.3e99af9","type":"debug","z":"8d6fc00e.79c98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":317,"y":527,"wires":[]},{"id":"d67d1b4e.910588","type":"ui_gauge","z":"8d6fc00e.79c98","name":"","group":"47d4aadb.665534","order":10,"width":0,"height":0,"gtype":"gage","title":"Luftdruck","label":"hPa","format":"{{value}}","min":"300","max":"1100","colors":["#00b3d9","#0073e6","#001bd7"],"seg1":"33","seg2":"66","x":307,"y":587,"wires":[]},{"id":"9f125332.13138","type":"mqtt in","z":"8d6fc00e.79c98","name":"","topic":"esp32/Hoehe","qos":"2","datatype":"auto","broker":"90edecf3.20888","x":114,"y":799,"wires":[["6d4b7a6b.c460c4","a977a0b6.b46a1"]]},{"id":"6d4b7a6b.c460c4","type":"debug","z":"8d6fc00e.79c98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":321,"y":766,"wires":[]},{"id":"a977a0b6.b46a1","type":"ui_gauge","z":"8d6fc00e.79c98","name":"","group":"47d4aadb.665534","order":11,"width":0,"height":0,"gtype":"gage","title":"Berechnete Höhe","label":"m","format":"{{value}}","min":0,"max":"999","colors":["#00b3d9","#0073e6","#001bd7"],"seg1":"33","seg2":"66","x":341,"y":826,"wires":[]},{"id":"9c962029.c6c61","type":"ui_gauge","z":"8d6fc00e.79c98","name":"","group":"47d4aadb.665534","order":2,"width":0,"height":0,"gtype":"gage","title":"Temperatur","label":"°C","format":"{{value}}","min":"-40","max":"85","colors":["#00b3d9","#0073e6","#001bd7"],"seg1":"33","seg2":"66","x":330,"y":480,"wires":[]},{"id":"af4b8366.f8761","type":"function","z":"8d6fc00e.79c98","name":"Feinstaubalarm","func":"var feinstaubalarm;\nfeinstaubalarm = \"gut\";\nmsg.color=\"limegreen\";\n\n\nif  (msg.payload  > \"50.01\"){\n      feinstaubalarm = \"maessig\";\n      msg.color=\"orange\";}\nelse if (msg.payload  > \"99.99\"){\n       feinstaubalarm = \"ungesund\";\n       msg.color=\"red\";}\n\nmsg.payload = feinstaubalarm;\nreturn msg;","outputs":1,"noerr":0,"x":520,"y":300,"wires":[["eae6aaa4.888078","ccc03a4f.f658f8"]]},{"id":"ccc03a4f.f658f8","type":"ui_text","z":"8d6fc00e.79c98","group":"47d4aadb.665534","order":14,"width":0,"height":0,"name":"","label":"Luftgüte ist","format":"{{msg.payload}}","layout":"row-center","x":740,"y":340,"wires":[]},{"id":"eae6aaa4.888078","type":"debug","z":"8d6fc00e.79c98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":720,"y":300,"wires":[]},{"id":"36456a18.535206","type":"function","z":"8d6fc00e.79c98","name":"case Feinstaub","func":"var feinstaubalarm;\n\nswitch(feinstaubalarm)\n{\ncase 1: \n    input(msg.payload >= \"51.00\")\n     feinstaubalarm = \"maessig\"\n     msg.color=\"orange\"\n     break;\n    \ncase 2: \n    input(msg.payload  >= \"101.00\")\n       feinstaubalarm = \"ungesund\"\n       msg.color=\"red\"\n       break;\ncase 3:\n    input(msg.payload  <= \"50.00\")\n     feinstaubalarm = \"gut\"\n     msg.color=\"green\"\n     break;\n}\nmsg.payload = feinstaubalarm;\nreturn msg;","outputs":1,"noerr":0,"x":700,"y":180,"wires":[[]]},{"id":"5385b197.db6ea","type":"rpi-gpio out","z":"8d6fc00e.79c98","name":"","pin":"11","set":"","level":"0","freq":"","out":"out","x":690,"y":460,"wires":[]},{"id":"63c214b9.26a92c","type":"rpi-gpio out","z":"8d6fc00e.79c98","name":"","pin":"13","set":"","level":"0","freq":"","out":"out","x":690,"y":520,"wires":[]},{"id":"a7def52f.8ab178","type":"rpi-gpio out","z":"8d6fc00e.79c98","name":"","pin":"15","set":"","level":"0","freq":"","out":"out","x":690,"y":580,"wires":[]},{"id":"8253cc8d.1c4aa","type":"ui_switch","z":"8d6fc00e.79c98","name":"","label":"Green","tooltip":"","group":"47d4aadb.665534","order":15,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":520,"y":460,"wires":[["5385b197.db6ea"]]},{"id":"13107f59.208a01","type":"ui_switch","z":"8d6fc00e.79c98","name":"","label":"Yellow","tooltip":"","group":"47d4aadb.665534","order":16,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":520,"y":540,"wires":[["63c214b9.26a92c"]]},{"id":"902fdd43.be21b","type":"ui_switch","z":"8d6fc00e.79c98","name":"","label":"Red","tooltip":"","group":"47d4aadb.665534","order":17,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":520,"y":600,"wires":[["a7def52f.8ab178"]]},{"id":"3cf546ef.c4692a","type":"function","z":"8d6fc00e.79c98","name":"if change","func":"var x = msg.payload[8];\nif (x<=\"50.01\") {\n    var msg_o = {payload :\"gut\"}\n} else{\n    var masg_o = {payload :\"maessig\"}\n}\nreturn msg_o;","outputs":1,"noerr":0,"x":550,"y":120,"wires":[[]]},{"id":"abb3ee6a.d6ce9","type":"switch","z":"8d6fc00e.79c98","name":"","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"50.00","vt":"num"},{"t":"gt","v":"50","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":520,"y":380,"wires":[["8253cc8d.1c4aa"],[]]},{"id":"ffe7b932.876ae8","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"47d4aadb.665534","type":"ui_group","z":"","name":"Übersicht Messwerte","tab":"9a849a30.7f97a8","disp":true,"width":6,"collapse":false},{"id":"90edecf3.20888","type":"mqtt-broker","z":"","name":"","broker":"192.168.2.110","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"9a849a30.7f97a8","type":"ui_tab","z":"","name":"Raumluftüberwachung FSAT17","icon":"dashboard","disabled":false,"hidden":false}]

You could do something like this...

[{"id":"e4227be7.8ccd38","type":"join","z":"e1dfad68.fee2a","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":"num","reduceFixup":"","x":590,"y":320,"wires":[["8814f78e.8ea438","5659c0b9.d1148"]]},{"id":"2553cd91.cfc512","type":"mqtt in","z":"e1dfad68.fee2a","name":"","topic":"esp32/p10","qos":"0","datatype":"utf8","broker":"4af1f016.58aaa","x":420,"y":380,"wires":[["e4227be7.8ccd38"]]},{"id":"cd5cc1c2.3b51","type":"mqtt in","z":"e1dfad68.fee2a","name":"","topic":"esp32/p25","qos":"2","datatype":"utf8","broker":"4af1f016.58aaa","x":420,"y":280,"wires":[["e4227be7.8ccd38"]]},{"id":"b8782877.701e08","type":"inject","z":"e1dfad68.fee2a","name":"","topic":"esp32/p25","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":550,"y":500,"wires":[["50b59230.ee1edc"]]},{"id":"3a78e088.0b496","type":"inject","z":"e1dfad68.fee2a","name":"","topic":"esp32/p25","payload":"109","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":560,"y":580,"wires":[["50b59230.ee1edc"]]},{"id":"6bc44faf.564c3","type":"inject","z":"e1dfad68.fee2a","name":"","topic":"esp32/p25","payload":"59","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":550,"y":540,"wires":[["50b59230.ee1edc"]]},{"id":"d7f2fa60.63ff38","type":"inject","z":"e1dfad68.fee2a","name":"","topic":"esp32/p10","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":550,"y":660,"wires":[["50b59230.ee1edc"]]},{"id":"c695104f.163f4","type":"inject","z":"e1dfad68.fee2a","name":"","topic":"esp32/p10","payload":"777","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":560,"y":740,"wires":[["50b59230.ee1edc"]]},{"id":"4f6e38bc.c445e8","type":"inject","z":"e1dfad68.fee2a","name":"","topic":"esp32/p10","payload":"74","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":550,"y":700,"wires":[["50b59230.ee1edc"]]},{"id":"50b59230.ee1edc","type":"mqtt out","z":"e1dfad68.fee2a","name":"","topic":"","qos":"","retain":"","broker":"4af1f016.58aaa","x":730,"y":620,"wires":[]},{"id":"8814f78e.8ea438","type":"debug","z":"e1dfad68.fee2a","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":750,"y":380,"wires":[]},{"id":"5ad671db.696e6","type":"debug","z":"e1dfad68.fee2a","name":"<= 50","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1050,"y":280,"wires":[]},{"id":"a7dce083.4112a","type":"debug","z":"e1dfad68.fee2a","name":"> 50 < 100","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1070,"y":320,"wires":[]},{"id":"5659c0b9.d1148","type":"function","z":"e1dfad68.fee2a","name":"pin 1<50, pin2 >50, pin3>100","func":"var p10 = Number(msg.payload[\"esp32/p10\"]);\nvar p25 = Number(msg.payload[\"esp32/p25\"]);\n\nmsg.p10 = p10;\nmsg.p25 = p25;\n\nmsg.payload = 1;\n\nif(p10 > 100 && p25 > 100){\n    //both p10 and p25 are > 100\n    msg.topic = \"both > 100\";\n    return [null, null, msg];//send msg on pin 3\n} else if(p10 > 50 && p25 > 50 && p10 < 100 && p25 < 100){\n    //both p10 and p25 are > 50 and < 100\n    msg.topic = \"both > 50 < 100\";\n    return [null, msg, null];//send msg on pin 2\n} else if(p10 <= 50 && p25 <= 50){\n    msg.topic = \"both <= 50\";\n    return [msg,null, null];//send msg on pin 1\n}\nreturn null;//doesnt match any criteria","outputs":3,"noerr":0,"x":800,"y":320,"wires":[["5ad671db.696e6"],["a7dce083.4112a"],["91fcde2a.e4f43"]]},{"id":"91fcde2a.e4f43","type":"debug","z":"e1dfad68.fee2a","name":"> 100","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1050,"y":360,"wires":[]},{"id":"4af1f016.58aaa","type":"mqtt-broker","z":"","name":"aedes local","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"40","cleansession":true,"birthTopic":"aedes/state","birthQos":"0","birthPayload":"ready","closeTopic":"aedes/state","closeQos":"0","closePayload":"alert","willTopic":"aedes/state","willQos":"0","willPayload":"alert"}]

Thanks a lot Steve_Mcl,

nice and quick qork, but it doesn´t work, if the Value goes down the Pins dont move back to false.

Many thanks, maybe I can expand or improve it to reach my goal

It was to be a starting point for you to build on

Just rearrange the logic in the function to send 1 or 0 in the payload etc.

I was trying to demonstrate how to join and how to process logic and how to transmit out of different pins on the function node.

Its up to you to make it do what you need.

:+1:

Good Job, i changed some things, it seems like the text working better, but i cant get the right way to switch the gpio Pins.

if the values leay <100 at one time the 3rd pin get no true but the 2nd pin get no reset if the value is under 50 of both.

[{"id":"35d48448.4de36c","type":"function","z":"8d6fc00e.79c98","name":"pin 1<50, pin2 >50, pin3>100","func":"var p10 = Number(msg.payload[\"esp32/p10\"]);\nvar p25 = Number(msg.payload[\"esp32/p25\"]);\n\nmsg.p10 = p10;\nmsg.p25 = p25;\n\nmsg.payload = false;\n\nif(p10 > 100 && p25 > 100){\n    //both p10 and p25 are > 100\n    msg.topic = \"both > 100\";\n   msg.payload = \"true\";\n    return [null, null, msg];//send msg on pin 3\n} else if(p10 > 50 && p25 > 50 && p10 < 100 && p25 < 100){\n    //both p10 and p25 are > 50 and < 100\n    msg.topic = \"both > 50 < 100\";\n    msg.payload = \"true\";\n    return [null, msg, null];//send msg on pin 2\n} else if(p10 <= 50 && p25 <= 50){\n    msg.topic = \"both <= 50\";\n    msg.payload = \"true\";\n    return [msg,null, null];//send msg on pin 1\n}\n\nreturn null;//doesnt match any criteria\n","outputs":3,"noerr":0,"x":520,"y":1100,"wires":[["db01207.c17e8e","5385b197.db6ea"],["2c72c718.49d2d8","63c214b9.26a92c"],["96fa579e.00d008","a7def52f.8ab178"]]}]

If you want to turn on some and off others, you can do this...

var p10 = Number(msg.payload["esp32/p10"]);
var p25 = Number(msg.payload["esp32/p25"]);

if(p10 > 100 && p25 > 100){
    //both p10 and p25 are > 100
    return [
        {payload:false}, 
        {payload:false}, 
        {payload:true} //output TRUE on pin 3
    ];
} else if(p10 > 50 && p25 > 50 && p10 < 100 && p25 < 100){
    //both p10 and p25 are > 50 and < 100
    return [
        {payload:false}, 
        {payload:true}, //output TRUE on pin 2
        {payload:false} 
    ];
} else if(p10 <= 50 && p25 <= 50){
    //both p10 and p25 are <= 50
    return [
        {payload:true}, //output TRUE on pin 1
        {payload:false}, 
        {payload:false} 
    ];
}
return null;//doesnt match any criteria

  • connect pin1 to turn ON one or more GPIO when BOTH are <= 50
    • Pins 2 and 3 will be triggered to turn something off
  • connect pin2 to turn ON one or more GPIO when BOTH are > 50 AND < 100
    • Pins 1 and 3 will be triggered to turn something off
  • connect pin3 to turn ON one or more GPIO when BOTH are > 100
    • Pins 2 and 3 will be triggered to turn something off

You can play with the logic to suit

Godlike….it works, thank you for all the hints!!!!! :wink:

[{"id":"9934e74e.25be58","type":"function","z":"8d6fc00e.79c98","name":"Anzeigewert Textausgabe gut;maessig;schlecht","func":"var p10 = Number(msg.payload[\"esp32/p10\"]);\nvar p25 = Number(msg.payload[\"esp32/p25\"]);\nvar qualitaet;\nmsg.p10 = p10;\nmsg.p25 = p25;\n\n\n\nif(p10 >= 100.01 && p25 >= 100.01){\n    //both p10 and p25 are > 100\n    qualitaet = \"schlecht\";\n    \n} else if(p10 >= 50.01 && p25 >= 50.01 && p10 <= 100.00 && p25 <= 100.00){\n    //both p10 and p25 are > 50 and < 100\n   qualitaet = \"maessig\";\n    \n} else if(p10 <= 50.00 && p25 <= 50.00){\n    qualitaet = \"gut\";\n    \n}\n\nmsg.payload = qualitaet;\nreturn msg;","outputs":1,"noerr":0,"x":560,"y":1000,"wires":[["eae6aaa4.888078","ccc03a4f.f658f8"]]}]

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