Help comparing two temp sensor output

Hello all,
I am a complete newbie to all things node red.... through a bunch of trial and error I have managed to set up a rasberry pi as a mosquitto server and node red server and have two working flows on my network but really do not totally understand node red.

I am trying to compare two dallas temp sensor outputs.... one for my hot tubs temp and one for my solar water heater panel. I want the solar pump to turn on when its temp is more than 10 degrees over the hot tubs temp.

I have searched and thought I had the fix with the attached flow using a function with an if than but find that I think I am having trouble with the dallas temp sensors out put which shows a a string using a debug node. Searching online I found a few examples of using a function node and parseInt of the message two make it an interger so i can numerically compare the two values.

Part of my problem is that I am really not a coder and have little experiance with all of this.
I also dont understand a lot of what this forum is discussing again total newbie.

I have searched for tutorials and this forum and find some similar posts about comparing inside and outside temperatures for a whole house fan which seems very similar to what I am trying to do.

but when I try to impliment the fix the poster used I cannot seem to get it to work and my uneducated guess is that while the parseInt() is supposed to out put the message to an integer it is either not doing that or the function as I have it set up is not able to read the interger to check it against the other temp reading which is also an interger. The if function is alway false yielding "off" and even when the solar temp sensor reading is more than 10 higher than the spatemp it remains false.

I am sure it is something stupid that I just dont understand yet so hopefully someone on the forum can have a look at the flow example and give me a little help and education.
Thanks for anyone who takes the time to have a look to help this newbie.
Troy

[{"id":"5e71878b.7bf938","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"aba4afb.1ea9c5","type":"mqtt in","z":"5e71878b.7bf938","name":"","topic":"esp8266spa/spatemp","qos":"2","datatype":"auto","broker":"562eb1ef.1c2f1","x":160,"y":340,"wires":[["c9f12235.3cecf","f3f65772.66c258"]]},{"id":"e5a61cf2.8c672","type":"mqtt in","z":"5e71878b.7bf938","name":"","topic":"esp8266spa/solartemp","qos":"2","datatype":"auto","broker":"562eb1ef.1c2f1","x":120,"y":480,"wires":[["e34726a0.201d88","486f095c.d5c1f8"]]},{"id":"e34726a0.201d88","type":"debug","z":"5e71878b.7bf938","name":"tutu","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":410,"y":620,"wires":},{"id":"c9f12235.3cecf","type":"debug","z":"5e71878b.7bf938","name":"tutu1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":570,"y":340,"wires":},{"id":"f3f65772.66c258","type":"function","z":"5e71878b.7bf938","name":"to int","func":"msg.payload=Number(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":380,"wires":[["51b6a281.39ac4c"]]},{"id":"d828674e.c50c38","type":"debug","z":"5e71878b.7bf938","name":"tutu2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":750,"y":440,"wires":},{"id":"486f095c.d5c1f8","type":"function","z":"5e71878b.7bf938","name":"to int","func":"msg.payload=Number(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":500,"wires":[["51b6a281.39ac4c"]]},{"id":"51b6a281.39ac4c","type":"function","z":"5e71878b.7bf938","name":"Comparatore","func":"// this is passed a payload which is an object with\n// properties outside_temp and inside_temp\nif (msg.payload.solartemp > msg.payload.spatemp+10) {\n msg.payload = "on";\n} else\n{\n msg.payload = "off";\n}\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":440,"wires":[["d828674e.c50c38"]]},{"id":"562eb1ef.1c2f1","type":"mqtt-broker","z":"","name":"","broker":"192.168.250.8","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Ok, when you want to paste the code, press the </> button just above where you are entering the text.

That will make it able to be used by others.

Hi Trying to learn,
Sorry my bad.... total newbie.
so like this

[{"id":"5e71878b.7bf938","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"aba4afb.1ea9c5","type":"mqtt in","z":"5e71878b.7bf938","name":"","topic":"esp8266spa/spatemp","qos":"2","datatype":"auto","broker":"562eb1ef.1c2f1","x":160,"y":340,"wires":[["c9f12235.3cecf","f3f65772.66c258"]]},{"id":"e5a61cf2.8c672","type":"mqtt in","z":"5e71878b.7bf938","name":"","topic":"esp8266spa/solartemp","qos":"2","datatype":"auto","broker":"562eb1ef.1c2f1","x":120,"y":480,"wires":[["e34726a0.201d88","486f095c.d5c1f8"]]},{"id":"e34726a0.201d88","type":"debug","z":"5e71878b.7bf938","name":"tutu","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":410,"y":620,"wires":[]},{"id":"c9f12235.3cecf","type":"debug","z":"5e71878b.7bf938","name":"tutu1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":570,"y":340,"wires":[]},{"id":"f3f65772.66c258","type":"function","z":"5e71878b.7bf938","name":"to int","func":"msg.payload=Number(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":380,"wires":[["51b6a281.39ac4c"]]},{"id":"d828674e.c50c38","type":"debug","z":"5e71878b.7bf938","name":"tutu2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":750,"y":440,"wires":[]},{"id":"486f095c.d5c1f8","type":"function","z":"5e71878b.7bf938","name":"to int","func":"msg.payload=Number(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":500,"wires":[["51b6a281.39ac4c"]]},{"id":"51b6a281.39ac4c","type":"function","z":"5e71878b.7bf938","name":"Comparatore","func":"// this is passed a payload which is an object with\n// properties outside_temp and inside_temp\nif (msg.payload.solartemp > msg.payload.spatemp+10) {\n    msg.payload = \"on\";\n} else\n{\n    msg.payload = \"off\";\n}\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":440,"wires":[["d828674e.c50c38"]]},{"id":"562eb1ef.1c2f1","type":"mqtt-broker","z":"","name":"","broker":"192.168.250.8","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

again sorry and thanks for letting me know.
Troy

That shouldn't be a problem.

Node-Red is more a "plug 'n' play" kind of thing where you connect blocks that do what you want.

Yes, there is a bit of stuff you need to do, but mostly with high level things like message names.

I guess Lego would be another good comparison.

Putting side all the parseInt() stuff, you need to have a good idea of what you want to do.
Rather than at a language level, but more (maybe) at a flow chart level.

Once you repost the flow so people can export it, you will get help with the code.
As it is, I'm not sure it is going to be able to be imported. Sometimes the file is corrupted because of the ASCII codes, etc. (Not your fault) You just need to paste it as code as I said in the first reply.

Thanks for the reply and I think I posted the code as you directed.... hopefully I got it correct.
Thanks
Troy

No problems.

Ok, sort of see what you want to do.

I changed the MQTT nodes for basic inject nodes. (That doesn't really matter)

Where things are falling over for you is that the message payloads don't live from message to message.

The thing is you need to store messages in context if you are going to compare them.

Only for clarity I had to change your flow a bit so you can send messages to see the different outputs. It had me a bit crazy because I didn't originally see the +10 in the comparison.

Anyway, this is the flow now that does what you want.

See how I saved the values in context (that is what you do so message values live longer than the message.)

It saves the values, then gets the values (default to 0 if the value isn't defined. *see later.) and compares them. I also put ( ) around the +10 part only to make sure the maths works in the correct order.

I also had to set the topic in the two function nodes because they would be set by the MQTT nodes, and as I didn't have them........

  • From above:
    The first two messages (one from each input) will be needed to set the values. It will only happen for the first two times and then it isn't really important.
    But it can throw you when testing and for the first two messages.

Code:

[{"id":"55c951a8.cf3d3","type":"debug","z":"a9fa01c4.c28d9","name":"tutu","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":340,"y":550,"wires":[]},{"id":"f5dcbd24.9b5508","type":"debug","z":"a9fa01c4.c28d9","name":"tutu1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":330,"y":330,"wires":[]},{"id":"ac547717.ff5a58","type":"function","z":"a9fa01c4.c28d9","name":"esp8266spa/spatemp","func":"msg.payload=Number(msg.payload);\nmsg.topic = \"esp8266spa/spatemp\";\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":380,"wires":[["8b4a2737.0b8638"]]},{"id":"87fff39d.347ec","type":"debug","z":"a9fa01c4.c28d9","name":"tutu2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":750,"y":440,"wires":[]},{"id":"60051663.0781b8","type":"function","z":"a9fa01c4.c28d9","name":"esp8266spa/solartemp","func":"msg.payload=Number(msg.payload);\nmsg.topic = \"esp8266spa/solartemp\";\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":500,"wires":[["8b4a2737.0b8638"]]},{"id":"8b4a2737.0b8638","type":"function","z":"a9fa01c4.c28d9","name":"Comparatore","func":"// this is passed a payload which is an object with\n// properties outside_temp and inside_temp\n\ncontext.set(msg.topic,msg.payload);\n\nvar solartemp = context.get(\"esp8266spa/solartemp\") || 0;\nvar spatemp = context.get(\"esp8266spa/spatemp\") || 0;\n\nnode.warn(\"Solar \" + solartemp);\nnode.warn(\"Spa   \" + spatemp);\n\n\n\nif (solartemp > (spatemp+10)) {\n    msg.payload = \"on\";\n} else\n{\n    msg.payload = \"off\";\n}\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":440,"wires":[["87fff39d.347ec"]]},{"id":"e857f51c.a51638","type":"inject","z":"a9fa01c4.c28d9","name":"","topic":"","payload":"20","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":380,"wires":[["f5dcbd24.9b5508","ac547717.ff5a58"]]},{"id":"deb1a23e.f2e5d","type":"inject","z":"a9fa01c4.c28d9","name":"","topic":"","payload":"22","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":500,"wires":[["60051663.0781b8","55c951a8.cf3d3"]]},{"id":"757d4547.b36fac","type":"inject","z":"a9fa01c4.c28d9","name":"","topic":"","payload":"50","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":330,"wires":[["ac547717.ff5a58"]]},{"id":"d47bcb38.ef8908","type":"inject","z":"a9fa01c4.c28d9","name":"","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":550,"wires":[["60051663.0781b8"]]},{"id":"96dd7bfb.b755d","type":"inject","z":"a9fa01c4.c28d9","name":"","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":430,"wires":[["ac547717.ff5a58"]]}]

If you use this, remove the lines in the first two function nodes that set the topic.
You will get frustrated if you use the nodes else where and wonder why the topic is getting set, or not the same as the one going in to the node.

1 Like

Thanks soo much..... I inserted into my larger flow and replaced the injects and works like a champ.
again thanks
I will study that part of the flow as I am sure there will cool stuff to learn in them.

Now onto my aquaponic pond management system .....baby steps.

Troy

Just to share the help:
The post where I put the flow (that worked):
There will be a little box at the bottom: SOLUTION.

Tick that box and then if people have a similar problem they can see which post solved the problem for you.

And thanks again for the solution thing.... lots to learn.
Troy

No problems. I hope I can help as many people as I can. Pay it forward kind of thing.
Good luck.

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