# Automate ceiling fans using temperature difference between sensors

**URL:** <https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813>\
**Category:** General\
**Created:** [7 December 2021 07:03 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813 "2021-12-07T07:03:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![connie](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@connie](https://discourse.nodered.org/u/connie)\
**Post date:** [7 December 2021 07:03 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/1 "2021-12-07T07:03:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [7 December 2021 10:19 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/2 "2021-12-07T10:19:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 December 2021 10:32 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/3 "2021-12-07T10:32:00Z")

</div>

> [@connie](#):
>
> I am an absolute newbie to NR

Welcome to the forum @connie

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). 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](https://cookbook.nodered.org/basic/join-streams) 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.

---

<div class="post-metadata">

**Author:** ![connie](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@connie](https://discourse.nodered.org/u/connie)\
**Post date:** [7 December 2021 16:16 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/4 "2021-12-07T16:16:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 December 2021 16:35 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/5 "2021-12-07T16:35:14Z")

</div>

> [@connie](#):
>
> So first I uses two node’s

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

---

<div class="post-metadata">

**Author:** ![connie](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@connie](https://discourse.nodered.org/u/connie)\
**Post date:** [9 December 2021 17:04 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/6 "2021-12-09T17:04:09Z")

</div>

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.  
Connie

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 December 2021 17:45 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/7 "2021-12-09T17:45:49Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![connie](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@connie](https://discourse.nodered.org/u/connie)\
**Post date:** [9 December 2021 18:26 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/8 "2021-12-09T18:26:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [9 December 2021 20:20 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/9 "2021-12-09T20:20:11Z")

</div>

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:

```auto
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.

Anyway:  
For the sake of keeping things all _here and now_ I'll do it this way:

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 December 2021 21:13 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/10 "2021-12-09T21:13:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![connie](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@connie](https://discourse.nodered.org/u/connie)\
**Post date:** [10 December 2021 05:29 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/11 "2021-12-10T05:29:21Z")

</div>

![Screen Shot 2021-12-09 at 9.27.50 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/609f8e4b6efe93a612b135c95d2c49d6bd00d03f.png)

---

<div class="post-metadata">

**Author:** ![connie](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@connie](https://discourse.nodered.org/u/connie)\
**Post date:** [10 December 2021 05:31 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/12 "2021-12-10T05:31:57Z")

</div>

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 ,  
Connie

---

<div class="post-metadata">

**Author:** ![connie](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@connie](https://discourse.nodered.org/u/connie)\
**Post date:** [10 December 2021 05:34 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/13 "2021-12-10T05:34:07Z")

</div>

![Screen Shot 2021-12-09 at 9.27.50 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/609f8e4b6efe93a612b135c95d2c49d6bd00d03f.png)  
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  
Connie

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [10 December 2021 05:38 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/14 "2021-12-10T05:38:45Z")

</div>

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:

 ![Screenshot from 2021-12-10 16-36-00](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/617d6d0c1969204b2fa741bf371549ea254a18a8.png)

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 December 2021 07:33 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/15 "2021-12-10T07:33:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![connie](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@connie](https://discourse.nodered.org/u/connie)\
**Post date:** [12 December 2021 19:50 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/16 "2021-12-12T19:50:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![connie](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@connie](https://discourse.nodered.org/u/connie)\
**Post date:** [12 December 2021 19:53 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/17 "2021-12-12T19:53:53Z")

</div>

12/12/2021, 11:39:15 AM[node: csv diagnostic](http://192.168.0.224:8123/api/hassio_ingress/YZJ5V_hv1xG8Lv8Qe6hYQhPOlTYJ-36t2GxIC9oiOlg/#)msg.payload : string[217]

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]  
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"

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 December 2021 19:54 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/18 "2021-12-12T19:54:33Z")

</div>

Why do you need the `csv` node?

---

<div class="post-metadata">

**Author:** ![connie](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@connie](https://discourse.nodered.org/u/connie)\
**Post date:** [12 December 2021 19:55 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/19 "2021-12-12T19:55:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![connie](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@connie](https://discourse.nodered.org/u/connie)\
**Post date:** [12 December 2021 19:59 UTC](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813/20 "2021-12-12T19:59:03Z")

</div>

" 12/12/2021, 11:57:04 AM[node: join debug](http://192.168.0.224:8123/api/hassio_ingress/YZJ5V_hv1xG8Lv8Qe6hYQhPOlTYJ-36t2GxIC9oiOlg/#)msg.payload : array[2]

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

[Next page](https://discourse.nodered.org/t/automate-ceiling-fans-using-temperature-difference-between-sensors/54813.md?page=2)
