Problem with number converting from mqtt retained messages

Hello. Im having difficulties converting data i think. node named "juhtimine" does not output numbers as i think it should. I have no previous experiences with javascript.

Heres the flow, i hope real programmers understand what i want to do in this flow.

[{"id":"417c5761.10e258","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"93185b2c.d53f18","type":"mqtt in","z":"417c5761.10e258","name":"Avamise temp","topic":"automaatika/MisTempAvab_KeskmineTuba","qos":"2","datatype":"json","broker":"d4c6732f.76e66","x":110,"y":300,"wires":[["383a4d55.ad099a"]]},{"id":"83b8cce1.4807f","type":"mqtt in","z":"417c5761.10e258","name":"Sulgemise temp","topic":"automaatika/MisTempSulgeb_KeskmineTuba","qos":"2","datatype":"auto","broker":"d4c6732f.76e66","x":120,"y":220,"wires":[["341f6b74.208cf4"]]},{"id":"5bd5877e.9a4e88","type":"mqtt in","z":"417c5761.10e258","name":"Autom.liigutamineON/OFF","topic":"automaatika/onoff_keskminetuba","qos":"2","datatype":"utf8","broker":"d4c6732f.76e66","x":150,"y":260,"wires":[["721c684a.b1712"]]},{"id":"cbab4b9a.574e9","type":"debug","z":"417c5761.10e258","name":"automaatika","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":570,"y":280,"wires":[]},{"id":"341f6b74.208cf4","type":"function","z":"417c5761.10e258","name":"Sulgemistemp","func":"var x = (msg.payload);\nvar y = {sulgemisetemp: x};\nreturn {payload : y}","outputs":1,"noerr":0,"x":300,"y":220,"wires":[["cbab4b9a.574e9","b98337a5.8a2928"]]},{"id":"383a4d55.ad099a","type":"function","z":"417c5761.10e258","name":"Avamistemp","func":"var x = (msg.payload);\nvar y = {avamistemp: x};\nreturn {payload : y}","outputs":1,"noerr":0,"x":270,"y":300,"wires":[["cbab4b9a.574e9","b98337a5.8a2928"]]},{"id":"dd1b0553.141df","type":"mqtt in","z":"417c5761.10e258","name":"Temp","topic":"keskmine_tuba/temp","qos":"2","datatype":"json","broker":"d4c6732f.76e66","x":90,"y":60,"wires":[["3acff23d.60ffe6"]]},{"id":"3acff23d.60ffe6","type":"function","z":"417c5761.10e258","name":"Temp","func":"var x = (msg.payload);\nvar y = {temp: x};\nreturn {payload : y}","outputs":1,"noerr":0,"x":270,"y":60,"wires":[["cbab4b9a.574e9","b98337a5.8a2928"]]},{"id":"b98337a5.8a2928","type":"function","z":"417c5761.10e258","name":"juhtimine","func":"//if(msg.payload.lyliti == \"ON\"){\nvar temp = (msg.payload.temp);\nvar a = (msg.payload.lyliti);\nvar b = (msg.payload.sulgemistemp);\nvar c = (msg.payload.avamistemp);\nreturn {payload : b} //for debug purposes\n\n if(temp <= b && a == \"ON\"){\nreturn {payload : 0}\n} \n if(temp >= c && a == \"ON\"){\nreturn {payload : 1}\n} \n","outputs":1,"noerr":0,"x":560,"y":220,"wires":[["6803468.c8df5b8","6d7f7ab7.95854c"]]},{"id":"721c684a.b1712","type":"function","z":"417c5761.10e258","name":"lyliti","func":"var x = (msg.payload);\nvar y = {lyliti: x};\nreturn {payload : y}","outputs":1,"noerr":0,"x":330,"y":260,"wires":[["cbab4b9a.574e9","b98337a5.8a2928"]]},{"id":"6803468.c8df5b8","type":"debug","z":"417c5761.10e258","name":"juhtimine","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":760,"y":260,"wires":[]},{"id":"6d7f7ab7.95854c","type":"mqtt out","z":"417c5761.10e258","name":"akna juhtimine","topic":"suur_tuba/juhtimine","qos":"","retain":"","broker":"d4c6732f.76e66","x":780,"y":220,"wires":[],"info":"# ## 1"},{"id":"bdbec942.f3295","type":"comment","z":"417c5761.10e258","name":"these 3 mqtt subscriptions are retained messages from openhab, messages will receive here but \"juhtimine\" wil output error \"NaN\" after outputting variablesl ","info":"","x":560,"y":180,"wires":[]},{"id":"d4c6732f.76e66","type":"mqtt-broker","z":"","name":"localhost","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

You should go read https://cookbook.nodered.org/basic/join-streams and look at your flow again.

I did but got same output.

Show the flow where you are now joining the messages.

Hi and welcome.

1st it is necessary that you send screenshots from the output of your debug nodes because we cannot simulate your input (from mqtt)

What I can tell you now is all data you receive from mqtt is handled as strings. Mqtt data don`t carry any information of the type of data. Even if javascript tries the best to convert data types as nessesary it is better not to rely on its performance. When it fails, ie. using a malformed number in a string inside an equation it will result in NaN means "Not a Number".

You first have to analyse your input data and try to convert it i.e var temp = Number(msg.payload.temp); This is likely to fail so we need to see your input data (content of msg.payload.temp.

i have been playing around with "join", "change" node and other functions but havent found any solution because i dont really understand why last node dont see these values.
complete msg object prints out these values...

[ { "id": "b98337a5.8a2928", "type": "function", "z": "417c5761.10e258", "name": "juhtimine", "func": "//if(msg.payload.lyliti == \"ON\"){\nvar temp = (msg.payload.temp);\nvar a = (msg.payload.lyliti);\nvar b = (msg.payload.sulgemisetemp);\nvar c = (msg.payload.avamistemp);\nreturn {a, b, c, temp}; //for debug purposes\n\n/*\nif(temp <= b){\nvar z = \"0\";\nreturn {payload : z}\n} \nif(temp >= c){\nvar kk = \"1\";\nreturn {payload : kk}\n} \n*/", "outputs": 1, "noerr": 0, "x": 1080, "y": 80, "wires": [ [ "6803468.c8df5b8" ] ] } ]

Each msg enters the function node seperate from the other msg's. So when the msg out of the 'temp' function enters the 'justimine' function node, none of the data from the other three functions exist at that time.

And when the msg from the 'liliti' function node gets to the 'justimine' function node, the data from the 'temp' function is not there to be processed.

Nodes do not remember what has come before. You eithor have to join the output of the four function nodes together before processing them in the 'justimine' function node or each function node could put thit's data in a flow variable and then the 'justimine' function node could get the data from the flow variables BUT you need to be careful that all the data is correct and not being used multiple times.

"temp" will be automatically updated every 30 second in mqtt broker.
other mqtt values are retained and will change if manually changed (input from smartphone app).
so these other values are lost after each update? what could be solution?

i did try to join them with "join" node but i messed up. could you make example for me?

What you have set in MQTT has no bearing on what happens in the flows (other than it injects messages into the flow. What you should do it attach an inject node where each mqtt node is. This way you will duplicate what is happening but much slower. Even if you had one inject triggering all the paths, each msg will arrive separately .

So you need to join the msgs into one msg that has all the information needed by the last function node.

As for an example, here you go

[{"id":"8ccddb9a.a55f38","type":"inject","z":"ac14500e.2c57d","name":"temperature","topic":"temperature","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":1760,"wires":[["47b769c5.cb0e28"]]},{"id":"47b769c5.cb0e28","type":"join","z":"ac14500e.2c57d","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":310,"y":1800,"wires":[["f9afb265.b11b7"]]},{"id":"f9afb265.b11b7","type":"debug","z":"ac14500e.2c57d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":470,"y":1800,"wires":[]},{"id":"2d269127.4f04ce","type":"inject","z":"ac14500e.2c57d","name":"humidity","topic":"humidity","payload":"","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":1800,"wires":[["47b769c5.cb0e28"]]},{"id":"d6fbe805.0e4628","type":"inject","z":"ac14500e.2c57d","name":"pressure","topic":"pressure","payload":"999","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":1840,"wires":[["47b769c5.cb0e28"]]}]

You may also want to have 'and after every additional message' checked in the Join node, it depends on your requirement.

In the function in the third column there is something wrong. The objects you create in the return statement is not formatted well

return {“a”:a,”b”:b,”c”:c, “temp”:temp}

Is perhaps giving you the desired result here. A property inside an object is always defined by the property name as a string, a “:” and then the value. Values devided by “,” is more an array surrounded by “[“ but this would give you 4 messages on the output.

plus you are returning that as the msg - not the msg.payload so the debug is configured incorrectly if you want to see the complete message. You should really

return {payload: {"a":a, "b":b, "c":c, "temp":temp})

1 Like

return {a, b, c, temp}; was there for debugging purposes. please dont concentrate on that.
real code that i need to get working is that:

var temp = (msg.payload.temp);
var a = (msg.payload.lyliti);
var b = (msg.payload.sulgemisetemp);
var c = (msg.payload.avamistemp);

if(temp <= b){
var z = "0";
return {payload : z}
}
if(temp >= c){
var kk = "1";
return {payload : kk}
}

Your function will only work if each msg that goes into the function contains a payload that is an object like

payload {
    "temp": some_value,
    "lyliti": : some_value,
    "sulgemisetemp": some_value,
    "avamistemp": some_value
}
  1. Is that what is going into the 'function' node???
  2. have you implimented the join node yet?

arent these function nodes do the same like "join" node? i did not get join node to work.

Answer: No.

As I have said several times, a function node process EACH msg that arrives seperately.

what did not work? you haven't shown what you tried.

i really messed up everything while trying things out, could anyone do it for me? i could not find answers for my problem while googling.

  1. What settings did you use in the join node?
  2. put a debug node (set to display the complete msg object) on the output of each of the mqtt-in nodes
  3. show the results of the debug nodes
  4. export theflow and attach it to a reply.

There is a great video series I think you'll need to watch to get things going
https://www.youtube.com/results?search_query=Node-RED+Essentials

Join node usage is covered here

2 Likes

The reason you’re seeing these errors is because your properties contain slashes which are interpreted as divisor rather than part of the name. I saw this same issue explained to someone else a couple days ago, let me dig up the post for you. You can follow the instructions there to get rid of this error, then continue with the next part: following the instructions of those posting above me.