# Help with functions

**URL:** <https://discourse.nodered.org/t/help-with-functions/48746>\
**Category:** General\
**Created:** [22 July 2021 01:29 UTC](https://discourse.nodered.org/t/help-with-functions/48746 "2021-07-22T01:29:59Z")\
**Posts on this page:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![ronbu](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@ronbu](https://discourse.nodered.org/u/ronbu)\
**Post date:** [23 July 2021 23:32 UTC](https://discourse.nodered.org/t/help-with-functions/48746/21 "2021-07-23T23:32:01Z")

</div>

It doesn’t look like your sensor is returning a temperature or at least a msg that identifies a reading as a temperature.

If you look at the debug #1 it shows the temp as 84.45502 and the humidity as 53.32265  
What is wrong with that?

you also ask " P.p.s. It looks like your flow is issuing the exact same http request for some solar data. Can you explain that?"

Sorry I don't have anything about solar in the flow that I know of.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 July 2021 23:45 UTC](https://discourse.nodered.org/t/help-with-functions/48746/22 "2021-07-23T23:45:50Z")

</div>

Oops my mistake, I imported your flow but looked at a flow I inserted while looking at another thread.

So the data is coming from an mqtt node. What is publishing the the data and how is it identifying that the msg is sending a temperature or a humidity?

Also, you are sending ` every` msg to both the temperature and humidity gauges.

If you control the content/topics if the device sending the data you should change it to either put both temperature and humidity in the payload or send one msg with the topic indicating is is for the temperature and one for humidity with the topic indicating it is for the humidity. The. In the flow eithor have one mqtt-in node subscribing to the high level of the topic and a switch node to determine if it is for temp or humi and sending it to the correct flow, to row mqtt-in nodes, one just getting the temperature msgs and one getting the humidity msgs.

---

<div class="post-metadata">

**Author:** ![ronbu](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@ronbu](https://discourse.nodered.org/u/ronbu)\
**Post date:** [24 July 2021 00:11 UTC](https://discourse.nodered.org/t/help-with-functions/48746/23 "2021-07-24T00:11:31Z")

</div>

I am looking at the data from the two change node so yes that is why I see 2 times. If i look at the data from the sht30temp node the mqtt reciver I can't get it to display anything below is a debug of the output of the mqtt node

7/23/2021, 8:03:23 PM[node: ea9eef94.21b9f](http://192.168.0.5:1880/#)sht30temp : msg : Object

object

topic: "sht30temp"

payload: object

device: "SHT30#1"

fTemp: 92.49645

humidity: 40.41352

qos: 1

retain: false

\_msgid: "e156e403.5326d8"

---

<div class="post-metadata">

**Author:** ![ronbu](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@ronbu](https://discourse.nodered.org/u/ronbu)\
**Post date:** [24 July 2021 00:13 UTC](https://discourse.nodered.org/t/help-with-functions/48746/24 "2021-07-24T00:13:47Z")

</div>

yes you are correct saying  
"If you control the content/topics "  
but I don't know how. Ronbu

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [24 July 2021 00:39 UTC](https://discourse.nodered.org/t/help-with-functions/48746/25 "2021-07-24T00:39:47Z")

</div>

Here is what is happening. A msg arrives from the mqtt-in node which is connected to two `change` nodes. This cause the msg to be cloned so now two msgs are passing thru the flows.

One change node causes msg.payload to be set to msg.payload.fTemp while the other causes msg.payload to be set to msg.payload.humidity. Each msg travels to the appropriate ui-guage but is also sent to a `function` node.

Here is where the issue happens. The two msgs arrive - at seperate times - at the `function` node. The function node process each msg separately.

there are two things you could do, (1) rejoin the two msgs before sending them to the `function` node or (2) also connect the `mqtt-in` node to the `function` node. You will have to change the first two lines of the code to reflect msg.payload.fTemp and msg.payload.humidity.

That should get you going.

---

<div class="post-metadata">

**Author:** ![ronbu](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@ronbu](https://discourse.nodered.org/u/ronbu)\
**Post date:** [24 July 2021 01:45 UTC](https://discourse.nodered.org/t/help-with-functions/48746/26 "2021-07-24T01:45:06Z")

</div>

WOW that sounds like it may work I will try both suggestions. I'll let you know. Thank you Ronbu.

---

<div class="post-metadata">

**Author:** ![ronbu](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@ronbu](https://discourse.nodered.org/u/ronbu)\
**Post date:** [24 July 2021 02:11 UTC](https://discourse.nodered.org/t/help-with-functions/48746/27 "2021-07-24T02:11:57Z")

</div>

I will have to study the join node as I have never used it before

---

<div class="post-metadata">

**Author:** ![ronbu](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@ronbu](https://discourse.nodered.org/u/ronbu)\
**Post date:** [24 July 2021 02:15 UTC](https://discourse.nodered.org/t/help-with-functions/48746/28 "2021-07-24T02:15:03Z")

</div>

I now get this error:

"Message missing msg.parts property - cannot join in 'auto' mode"

---

<div class="post-metadata">

**Author:** ![ronbu](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@ronbu](https://discourse.nodered.org/u/ronbu)\
**Post date:** [24 July 2021 03:54 UTC](https://discourse.nodered.org/t/help-with-functions/48746/29 "2021-07-24T03:54:12Z")

</div>

well I have tried both suggestion you have given me however I am having no luck as writing the function and the new (to me) join node , Would you please re-write my flow and the function so I would have a working flow so I can study it and understand how this works. I am at my wits end on this and want to give up but I want to learn, but getting no ware. Ronbu. I just want to learn......

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [24 July 2021 04:28 UTC](https://discourse.nodered.org/t/help-with-functions/48746/30 "2021-07-24T04:28:31Z")

</div>

not relevant

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [24 July 2021 04:31 UTC](https://discourse.nodered.org/t/help-with-functions/48746/31 "2021-07-24T04:31:57Z")

</div>

SOrry just saw this post.

You do not need the change nodes

As it is an object you can just reference each of the values with

msg.payload.ftemp  
msg.payload.humidity

Make sure they are numbers and put them into your original function

```auto
var T = msg.payload.ftemp;
var H = msg.payload.humidity;
node.warn(["T",T, typeof T]);
node.warn(["H",H, typeof H]);
var HI = -42.379 + 2.04901523*T + 10.14333127*H - .22475541*T*H - .00683783*T*T - .05481717*H*H + .00122874*T*T*H + .00085282*T*H*H - .00000199*T*T*H*H;
msg.payload = HI;
return msg;

```

Craig

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [24 July 2021 08:30 UTC](https://discourse.nodered.org/t/help-with-functions/48746/32 "2021-07-24T08:30:17Z")

</div>

While @craigcurtin has the function correct, you do still need the change nodes to move the T and H readings to msg.payload because the gauges require the values to be in msg.payload.

Here is the complete flow

```auto
[{"id":"ddb60bf6.ecbe98","type":"tab","label":"sht30","disabled":false,"info":""},{"id":"795c8109.f4c5c","type":"mqtt in","z":"ddb60bf6.ecbe98","name":"","topic":"sht30temp","qos":"2","datatype":"json","broker":"f59c9fa3.bd9ea","x":60,"y":140,"wires":[["501656f8.157128","f0c3b57f.b3e528","cff8bba.e10a848"]]},{"id":"10504951.4a3f87","type":"mqtt out","z":"ddb60bf6.ecbe98","name":"","topic":"sht30temp","qos":"2","retain":"","broker":"f59c9fa3.bd9ea","x":330,"y":20,"wires":[]},{"id":"dc9feee4.0f951","type":"debug","z":"ddb60bf6.ecbe98","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":590,"y":140,"wires":[]},{"id":"1b4b9cde.7d67a3","type":"ui_gauge","z":"ddb60bf6.ecbe98","name":"","group":"b716789b.7d27d8","order":1,"width":6,"height":3,"gtype":"gage","title":"TEMPTURE","label":"#","format":"{{value}}","min":"50","max":"110","colors":["#00b500","#e6e600","#ca3838"],"seg1":"40","seg2":"80","x":570,"y":100,"wires":[]},{"id":"501656f8.157128","type":"change","z":"ddb60bf6.ecbe98","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.fTemp","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":120,"wires":[["dc9feee4.0f951","1b4b9cde.7d67a3","9cb137e.89d2ac8","442805a3.6c525c","cff8bba.e10a848"]]},{"id":"9d1def75.64854","type":"ui_gauge","z":"ddb60bf6.ecbe98","name":"","group":"b716789b.7d27d8","order":2,"width":6,"height":3,"gtype":"gage","title":"HUMIDITY","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"25","seg2":"50","x":550,"y":240,"wires":[]},{"id":"f0c3b57f.b3e528","type":"change","z":"ddb60bf6.ecbe98","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.humidity","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":200,"wires":[["9d1def75.64854","f463c05a.b86ca","b0f855b5.ea91b8","442805a3.6c525c","cff8bba.e10a848"]]},{"id":"f463c05a.b86ca","type":"debug","z":"ddb60bf6.ecbe98","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":560,"y":200,"wires":[]},{"id":"b0f855b5.ea91b8","type":"ui_chart","z":"ddb60bf6.ecbe98","name":"","group":"b716789b.7d27d8","order":15,"width":20,"height":7,"label":"chart","chartType":"line","legend":"false","xformat":"dd HH:mm","interpolate":"linear","nodata":"","dot":false,"ymin":"30","ymax":"100","removeOlder":"4","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":560,"y":300,"wires":[[]]},{"id":"9cb137e.89d2ac8","type":"ui_chart","z":"ddb60bf6.ecbe98","name":"","group":"b716789b.7d27d8","order":7,"width":20,"height":7,"label":"chart","chartType":"line","legend":"false","xformat":"dd HH:mm","interpolate":"linear","nodata":"","dot":false,"ymin":"30","ymax":"110","removeOlder":"4","removeOlderPoints":"","removeOlderUnit":"86400","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":580,"y":60,"wires":[[]]},{"id":"57aa2c4a.898264","type":"ui_text","z":"ddb60bf6.ecbe98","group":"b716789b.7d27d8","order":8,"width":0,"height":0,"name":"heat index","label":"text","format":"{{msg.payload}}","layout":"row-spread","x":550,"y":380,"wires":[]},{"id":"f92436d7.42e8d8","type":"debug","z":"ddb60bf6.ecbe98","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":560,"y":420,"wires":[]},{"id":"decf5436.247168","type":"ui_gauge","z":"ddb60bf6.ecbe98","name":"","group":"b716789b.7d27d8","order":3,"width":7,"height":3,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":"130","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":570,"y":460,"wires":[]},{"id":"cff8bba.e10a848","type":"function","z":"ddb60bf6.ecbe98","name":"heat index fourmal","func":"var T = msg.payload.fTemp;\nvar H = msg.payload.humidity;\nnode.warn([\"T\",T, typeof T]);\nnode.warn([\"H\",H, typeof H]);\nvar HI = -42.379 + 2.04901523*T + 10.14333127*H - .22475541*T*H - .00683783*T*T - .05481717*H*H + .00122874*T*T*H + .00085282*T*H*H - .00000199*T*T*H*H;\nmsg.payload = HI;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":400,"wires":[["57aa2c4a.898264","decf5436.247168","f92436d7.42e8d8"]]},{"id":"442805a3.6c525c","type":"debug","z":"ddb60bf6.ecbe98","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":540,"y":340,"wires":[]},{"id":"f59c9fa3.bd9ea","type":"mqtt-broker","z":"","name":"HOME","broker":"192.168.0.05:1880","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"b716789b.7d27d8","type":"ui_group","z":"ddb60bf6.ecbe98","name":"SHT30","tab":"6ed960d6.dc76e","order":10,"disp":true,"width":20,"collapse":false},{"id":"6ed960d6.dc76e","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![ronbu](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@ronbu](https://discourse.nodered.org/u/ronbu)\
**Post date:** [24 July 2021 22:36 UTC](https://discourse.nodered.org/t/help-with-functions/48746/33 "2021-07-24T22:36:12Z")

</div>

Good morning;  
Are you sure you sent the new flow in your post? It is the same flow I had sent you.It does not have the join node and still does not show the heat index. please check your post maybe I am not importing it in-correct. Ronbu.

---

<div class="post-metadata">

**Author:** ![ronbu](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@ronbu](https://discourse.nodered.org/u/ronbu)\
**Post date:** [24 July 2021 22:42 UTC](https://discourse.nodered.org/t/help-with-functions/48746/34 "2021-07-24T22:42:41Z")

</div>

The above was the post i wanted to send last night but again the forum locked out as I am a new user.....well thats a mute point now as I was working on the code my PI 3 B+ SD card crashed....and no I do not have a backup so I out of this for now....All my flows, Arduino code is gone... I have spent the day tring to recover the SD card but not having any luck. I am very, very sad.. Ronbu.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [25 July 2021 10:12 UTC](https://discourse.nodered.org/t/help-with-functions/48746/35 "2021-07-25T10:12:35Z")

</div>

Need I say more Ron...

> [@node-RED Backup Flow](https://discourse.nodered.org/t/node-red-backup-flow/25033):
>
> I read a [post a few days ago](https://discourse.nodered.org/t/ive-lost-everything-connection-lost-to-machine-error-subflow-connections/24782) by @Trying_to_learn about losing his node-RED flow, which had taken him no doubt many hours to construct, and as his backup was on the same machine - he lost that too. So... I've spent a few days looking at off-site storage, and stumbled upon node-red-contrib-dsm which was written by @cflurin complete with an example flow to perform automated backups to dropbox. [dsm] I played about with the configuration, and made some changes to the way that the backups are stor…

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 September 2021 10:13 UTC](https://discourse.nodered.org/t/help-with-functions/48746/36 "2021-09-23T10:13:05Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/help-with-functions/48746.md?page=1)
