Automate ceiling fans using temperature difference between sensors

I am an absolute newbie to NR, and have received help with flows from my son. My goal is to try and create a flow that will compare the temperature difference between two sensors and have my ceiling fans come on. I have a tp link HS200 wall switch, and two sensors that are already recognized by HA. Please forgive my ignorance in advance, but would really like some help.

Hi and welcome to the forum.

I'll go over it at a very high level to explain how I see how to do it.

Each sensor sends it's temperature at intervals.

The two values are received (we'll say by MQTT - IN nodes.

The two messages are joined together with a join node (set to manual) to make one message.

That message is sent into a function node and their difference determined.

Depending on the difference (maths) an output signal is generated and that will go to turn on the ceiling fan.

There are other nodes you could use but for now I'll try to use the standard nodes as much as possible.

Welcome to the forum @connie

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Once you have done that, then for your particular problem the first thing you have to do is to get both values into one message. See this article in the cookbook for an example of how to join messages into one object.

However, I note that you are using Home Assistant, few (if any) here use that, preferring to use pure node-red to solve problems, so you may have difficulty getting help if the questions are HA related.

Thank you so much for your very kind and patient response. I am using NR and it is recognizing the entities. I will definitely take advantage of the tutorial. So first I uses two node’s whose output is Matt to a function node, is that correct?

First you watch the tutorial and read and understand the cookbook recipe I linked to.

Hi Colin,
I am back after following your recommendation of watching the tutorial and cookbook. I have a better understanding of nodered. What i need to do is to use a join red that will combine the readings from my two sensors and then output the message to a function node that will perform a json computation. I am having trouble with the join node. Maybe you could guide me to the using the correct payloads from the sensors. Any help would be appreciated.

Have you imported the example from the cookbook and understood how it works?

Note that the two messages coming in must have different topics.

Yes I imported the file and do understand it mostly. The inject nodes are sending the payload to the join node using the imputed number values. Then the join node is combining them into one message, right?

Yes, the join node combines them into one message.

So you are wanting to join TWO messages. I'm guessing they arrive fairly close to one another.

Just change the 3 to 2.

Then you get a message like in the example.

In the function node you split the message back to the two values.

Using that example flow as shown on the page:

let value1 = msg.temperature;
let value2 = msg.humidity;

Ok, not much use to you in that form. But it is a start.
Given you have two sensors let's go that next step to what you could do:

I'm not sure if you have already given the two sensors unique names - though both having the same would not be a good idea.

For the sake of keeping things all here and now I'll do it this way:

[{"id":"9de56772adac32e8","type":"inject","z":"bda8009e199a9bce","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":190,"y":110,"wires":[["58e468b91dd55883"]]},{"id":"269f64ef8f1b9005","type":"function","z":"bda8009e199a9bce","name":"","func":"let tempA = msg.payload[0];\nlet tempB = msg.payload[1];\n\nlet difference = tempB - tempA;\n\nmsg.payload = difference;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":150,"wires":[["5eb52ada5ae033e1"]]},{"id":"58e468b91dd55883","type":"change","z":"bda8009e199a9bce","name":"","rules":[{"t":"set","p":"sensor1","pt":"msg","to":"A","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":110,"wires":[["6390e5b1dcdebee0"]]},{"id":"5610217690d06e60","type":"inject","z":"bda8009e199a9bce","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"22","payloadType":"num","x":190,"y":180,"wires":[["ef7331ef5fe27719"]]},{"id":"ef7331ef5fe27719","type":"change","z":"bda8009e199a9bce","name":"","rules":[{"t":"set","p":"sensor2","pt":"msg","to":"B","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":180,"wires":[["6390e5b1dcdebee0"]]},{"id":"6390e5b1dcdebee0","type":"join","z":"bda8009e199a9bce","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":540,"y":150,"wires":[["269f64ef8f1b9005"]]},{"id":"5eb52ada5ae033e1","type":"debug","z":"bda8009e199a9bce","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":220,"wires":[]}]

Hope that helps.

Yes, so what isn't working for you? Have you checked the Join node is set up correctly and that the two mesages going in have different topics?
If you can't get it working then feed the input messages into debug nodes (give the debug nodes names so you can tell which is which in the debug ouput) and show us what you see. Then show us how you have configured the Join node and tell us what is not working correctly.

I included a screen shot of my flow. As you can see I can combine the sensors to a debug, named test, but when I insert a join node I do not get any output to my test debug. Sure appreciate you looking at this.
Thanks ,

As you can see i have a flow with the two sensors to a debug nod labeled test. When i insert a join node i do not get any debug messages. ?? Sure appreciate the help

You are going to have to edit the join node.

By default it is set to automatic. You need to make it manual.

Like this:

Then connect a debug node to the output of the join node.
Make it a different one and make that debug node show the entire message.
I also suggest rather than one debug joined as you have it, put a debug node on each sensor output and name them as needed so you can see which is which.

Oh, something else I just saw/noticed:
Looking at what you showed on the screen shot the important part of the message is:
msg.state by the looks of it.

And another thing:
You need to expand those messages you have on the screen shot.

You will see the text: > {entity_id.....
The > (text reference) click there and the message will be expanded to show all parts.

As I said, the topic values for the two messages have to be different, what topic values do your two messages have?
You can set topic values using Change nodes in each leg.

Hello Colin,
I have made some headway. it appears that I have successfully added a join node, and csv node. So I am guessing the next node is a function node to complete the computation of the state values? I need some help with the code I guess.
Thanks continually for your help

12/12/2021, 11:39:15 AMnode: csv diagnosticmsg.payload : string[217]


sensor.red_temperature,69.8,2021-12-12T19:36:02.729351+00:00,2021-12-12T19:36:02.729351+00:00,192991,69.8 sensor.upstairs_temperature,77.4,2021-12-12T19:26:40.524458+00:00,2021-12-12T19:26:40.524458+00:00,755209,77.4

Did I get the debug message copied correctly?`12/12/2021, 11:39:15 AMnode: csv diagnostic
"msg.payload : string[217]

Why do you need the csv node?

I thought is would be necessary for the function node to be able to combine the state values. ?

" 12/12/2021, 11:57:04 AMnode: join debugmsg.payload : array[2]


0: object

entity_id: "sensor.red_temperature"

state: 70

attributes: object

last_changed: "2021-12-12T19:53:02.733411+00:00"

last_updated: "2021-12-12T19:53:02.733411+00:00"

context: object

timeSinceChangedMs: 241702

original_state: "70.0"

1: object

entity_id: "sensor.upstairs_temperature"

state: 77.9

attributes: object

last_changed: "2021-12-12T19:55:40.538025+00:00"

last_updated: "2021-12-12T19:55:40.538025+00:00"

context: object

timeSinceChangedMs: 83900

original_state: "77.9"

here's the message from the join debug