[help] How to extract 2 values from a 2 mqtt nodes

Hey, I want to extract temperature and sate from the two individual mqtt nodes and put it to a variable for an if condition.

[{"id":"e138bfbf.d608d","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"681e26cb.94af38","type":"mqtt in","z":"e138bfbf.d608d","name":"","topic":"ac_humidity","qos":"2","broker":"4c91647e.b969fc","x":210,"y":1020,"wires":[["feb1b16d.72fc1"]]},{"id":"4d44d983.912fd8","type":"mqtt in","z":"e138bfbf.d608d","name":"","topic":"inside_temp","qos":"2","broker":"4c91647e.b969fc","x":210,"y":1080,"wires":[["feb1b16d.72fc1"]]},{"id":"ba56540a.e134c8","type":"mqtt in","z":"e138bfbf.d608d","name":"","topic":"ac_state","qos":"2","broker":"4c91647e.b969fc","x":520,"y":960,"wires":[["feb1b16d.72fc1"]],"outputLabels":["ac_state"]},{"id":"feb1b16d.72fc1","type":"delay","z":"e138bfbf.d608d","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":760,"y":960,"wires":[["932a89c.a1acb78"]]},{"id":"932a89c.a1acb78","type":"function","z":"e138bfbf.d608d","name":"","func":"var msg3 =  msg.payload.temp_c ;\n\ncontext.data = context.data || {};\n\nvar msg1 = { payload:context.data.inside_temp };\nvar ac_state = { payload:context.data.ac_state };\n\n//var msg1 = msg.payload.inside_temp ;\n\n//var ac_state = msg.payload.ac_state ;\n\nvar lowthresholdtemp = msg3 - 4;\nvar highthresholdtemp = msg3 + 4;\n\nif (ac_state == \"ON\") {\n\n if (lowthresholdtemp <= msg1 || msg1 <= highthresholdtemp)  { // Use sensor value instead of 25\n        msg.payload =\"Not Cooling.\";\n        return msg;\n   }  else {\n        return null;\n    }\n}","outputs":1,"noerr":0,"x":910,"y":960,"wires":[["6996833c.c22b8c"]]},{"id":"6996833c.c22b8c","type":"debug","z":"e138bfbf.d608d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1110,"y":960,"wires":[]},{"id":"53a7ecc.378fc14","type":"http request","z":"e138bfbf.d608d","name":"","method":"GET","ret":"obj","paytoqs":false,"url":"http://api.weatherunlocked.com/api/current/6.8652,79.8599?app_id=970d4958&app_key=79c1ac7e6d18a960b017cd15eb35bfa0","tls":"","proxy":"","authType":"basic","x":770,"y":660,"wires":[[]]},{"id":"4165a85c.516248","type":"inject","z":"e138bfbf.d608d","name":"","topic":"","payload":"","payloadType":"date","repeat":"300","crontab":"","once":true,"onceDelay":"0.1","x":550,"y":660,"wires":[["53a7ecc.378fc14"]]},{"id":"afece970.5e8838","type":"function","z":"e138bfbf.d608d","name":"","func":"if (msg.payload==\"Error: getaddrinfo ENOTFOUND api.weatherunlocked.com api.weatherunlocked.com:80\"){\nreturn null;\n}\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":880,"wires":[["feb1b16d.72fc1"]]},{"id":"ec944499.e4fe58","type":"http request","z":"e138bfbf.d608d","name":"","method":"GET","ret":"obj","paytoqs":false,"url":"http://api.weatherunlocked.com/api/current/6.8652,79.8599?app_id=970d4958&app_key=79c1ac7e6d18a960b017cd15eb35bfa0","tls":"","proxy":"","authType":"basic","x":720,"y":740,"wires":[["afece970.5e8838"]]},{"id":"b372c91.940ac38","type":"inject","z":"e138bfbf.d608d","name":"","topic":"","payload":"","payloadType":"date","repeat":"300","crontab":"","once":true,"onceDelay":"0.1","x":500,"y":740,"wires":[["ec944499.e4fe58"]]},{"id":"4c91647e.b969fc","type":"mqtt-broker","z":"","name":"","broker":"85.119.83.194","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]

The output of inside_temp-

inside_temp : msg.payload : string[5]
" 30.5"

Take a look at the cookbook https://cookbook.nodered.org which appears to have a flow that might do what you want

1 Like

Which flow you are referring to? Would be great if you let me know on how to correct it @ukmoose

The one about creating a single message from two streams.

1 Like

@Colin @ukmoose Thank you, how can I correct this? I only want to retrieve the value 30.4 and value "ON"?

ac_humidity : msg.payload : Object
object
ac_humidity: "" 30.4""
ac_status: ""ON""
  // Read temperature and Humidity
    delay(dht.getMinimumSamplingPeriod());

    float humidity = dht.getHumidity();
    float temperature = dht.getTemperature();
    Serial.println("");
    Serial.print(dht.getStatusString());


  // Computes temperature values in Celsius
    float hic = dht.computeHeatIndex(temperature, humidity, false);
    char temperatureTemp[10];
    char humidityTemp[10];

    dtostrf(temperature, 5, 1, temperatureTemp);
    dtostrf(humidity, 5, 1, humidityTemp);

  // Publishes Temperature and Humidity values
  client.publish("inside_temp", temperatureTemp); /// send char
  client.publish("ac_humidity", humidityTemp);

Why not combine both into one mqtt msg and give the topic as the location of the A/C. that way if you ever have another A/C you would be able to identify which one it came from...

I don't understand what it is you have posted. Is the first bit the output from the Join node? If so then you want msg.payload.ac_humidity and msg.payload.ac_status. I don't know why you have posted the js.
By the way, the double double quotes in the debug output probably mean that the strings you are publishing have quotes actually in the strings, which you probably want to sort out.

@Colin @ukmoose I was able to retrieve the two sensor values through a join. But, now the api value ( msg.temp_c ) cannot be retrieved. When I add a debug node it shows the exact value but the same syntax is not working in the function written. Kindly advise me on this

[{"id":"3ee7cc19.4db744","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"9aa88822.a0e9f8","type":"inject","z":"3ee7cc19.4db744","name":"","topic":"Hello","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":300,"y":600,"wires":[["124dfd8f.a15a82"]]},{"id":"124dfd8f.a15a82","type":"change","z":"3ee7cc19.4db744","name":"","rules":[{"t":"move","p":"topic","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":600,"wires":[["6c3225ca.41f5fc"]]},{"id":"6c3225ca.41f5fc","type":"debug","z":"3ee7cc19.4db744","name":"","active":true,"console":"false","complete":"false","x":690,"y":600,"wires":[]},{"id":"ec415520.e23118","type":"debug","z":"3ee7cc19.4db744","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":770,"y":800,"wires":[]},{"id":"8067343.d9edec8","type":"inject","z":"3ee7cc19.4db744","name":"post id","topic":"","payload":"2","payloadType":"str","repeat":"","crontab":"","once":false,"x":150,"y":760,"wires":[["501c9c3.8839364"]]},{"id":"501c9c3.8839364","type":"change","z":"3ee7cc19.4db744","name":"","rules":[{"t":"set","p":"post","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":760,"wires":[["e8e192b8.b0424"]]},{"id":"5464588d.9a6158","type":"function","z":"3ee7cc19.4db744","name":"","func":"msg.payload = msg.payload.temp_c;\nreturn msg;","outputs":1,"noerr":0,"x":620,"y":880,"wires":[["ec415520.e23118"]]},{"id":"e8e192b8.b0424","type":"http request","z":"3ee7cc19.4db744","name":"","method":"GET","ret":"obj","paytoqs":false,"url":"http://api.weatherunlocked.com/api/current/6.8652,79.8599?app_id=970d4958&app_key=79c1ac7e6d18a960b017cd15eb35bfa0","tls":"","proxy":"","authType":"basic","x":430,"y":880,"wires":[["5464588d.9a6158"]]},{"id":"91c9075d.4fb408","type":"http request","z":"3ee7cc19.4db744","name":"","method":"GET","ret":"obj","url":"https://jsonplaceholder.typicode.com/posts/{{post}}","tls":"","x":490,"y":680,"wires":[[]]},{"id":"18dedf16.dc5ab1","type":"mqtt in","z":"3ee7cc19.4db744","name":"","topic":"ac_state","qos":"2","broker":"4c91647e.b969fc","x":80,"y":1260,"wires":[["e399ef4d.3a985"]],"outputLabels":["ac_state"]},{"id":"5dc80be2.a274c4","type":"mqtt in","z":"3ee7cc19.4db744","name":"","topic":"inside_temp","qos":"2","broker":"4c91647e.b969fc","x":90,"y":1320,"wires":[["e399ef4d.3a985"]]},{"id":"e399ef4d.3a985","type":"join","z":"3ee7cc19.4db744","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":430,"y":1260,"wires":[["d6306ed0.ed7a6"]]},{"id":"d6306ed0.ed7a6","type":"delay","z":"3ee7cc19.4db744","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":720,"y":1260,"wires":[["d1ee05c8.bd4f58"]]},{"id":"d1ee05c8.bd4f58","type":"function","z":"3ee7cc19.4db744","name":"","func":"\nvar indoor_temp = msg.payload.inside_temp;\nvar ac_state = msg.payload.ac_state;\n\nmsg.temp_c = 31;\n\nvar lowthresholdtemp = msg.temp_c - 4;\nvar highthresholdtemp = msg.temp_c + 4;\n\nif (ac_state==\"ON\") {\nif(lowthresholdtemp <= indoor_temp || indoor_temp <= highthresholdtemp)  { // Use sensor value instead of 25\n        msg.payload =\"Detected that your A/C is not Cooling. Request repair from the app\";\n        return msg;\n    }  else {\n        return null;\n    }\n} else {\nreturn null;\n}\n","outputs":1,"noerr":0,"x":890,"y":1260,"wires":[["85b2ab23.194188"]]},{"id":"b7435361.8333f","type":"function","z":"3ee7cc19.4db744","name":"","func":"if (msg.payload==\"Error: getaddrinfo ENOTFOUND api.weatherunlocked.com api.weatherunlocked.com:80\"){\nreturn null;\n}\nmsg.temp_c = msg.payload.temp_c;\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":1100,"wires":[["d1ee05c8.bd4f58"]]},{"id":"682e9e1d.9fc8f","type":"http request","z":"3ee7cc19.4db744","name":"","method":"GET","ret":"obj","paytoqs":false,"url":"http://api.weatherunlocked.com/api/current/6.8652,79.8599?app_id=970d4958&app_key=79c1ac7e6d18a960b017cd15eb35bfa0","tls":"","proxy":"","authType":"basic","x":730,"y":960,"wires":[["b7435361.8333f"]]},{"id":"7cd34548.00fbdc","type":"inject","z":"3ee7cc19.4db744","name":"","topic":"","payload":"","payloadType":"date","repeat":"300","crontab":"","once":true,"onceDelay":"0.1","x":550,"y":960,"wires":[["682e9e1d.9fc8f"]]},{"id":"85b2ab23.194188","type":"delay","z":"3ee7cc19.4db744","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"10","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":1060,"y":1260,"wires":[["7ee7670f.8f0488"]]},{"id":"7ee7670f.8f0488","type":"debug","z":"3ee7cc19.4db744","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1250,"y":1220,"wires":[]},{"id":"4c91647e.b969fc","type":"mqtt-broker","z":"","name":"","broker":"85.119.83.194","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]

First you need to home in on which node is causing the problem. Start by putting debug nodes at each point in the flow and find out which node is not doing what you expect. Then if you can't understand why, show us the debug output for what is going into that node and the debug output for what is coming out of that node and post just that node.