# 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:** 20\
**Page:** 1

<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:** [22 July 2021 01:29 UTC](https://discourse.nodered.org/t/help-with-functions/48746/1 "2021-07-22T01:29:59Z")

</div>

i want to write a function to display heat index from temp and humidity from a sensor. I just don't know how to format the function to use the NWS equation any help? i can display the temp and humidity in the dashboard but i want to display the heat index as well. Ron

---

<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:** [22 July 2021 01:39 UTC](https://discourse.nodered.org/t/help-with-functions/48746/2 "2021-07-22T01:39:01Z")

</div>

Hi and welcome to the group/forum.

What is this `NWS` equation?

You have a `dashboard` working and that is good.

What kind of messages do you get from the sensor?

Could you post the message so it is known?

---

<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:** [22 July 2021 01:49 UTC](https://discourse.nodered.org/t/help-with-functions/48746/3 "2021-07-22T01:49:23Z")

</div>

hello the NWS equation is the formual to calcuate the temp and humidity to get a heat or feel like tempture

---

<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:** [22 July 2021 01:52 UTC](https://discourse.nodered.org/t/help-with-functions/48746/4 "2021-07-22T01:52:04Z")

</div>

i get the temp and the humidity and is displayed as number it all works well and i see it on my gauges just fine but i also want to display the cal. heat value

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [22 July 2021 01:53 UTC](https://discourse.nodered.org/t/help-with-functions/48746/5 "2021-07-22T01:53:43Z")

</div>

> [@ronbu](#):
>
> I just don't know how to format the function to use the NWS equation

Do you already have any JavaScript code to put in the function node?

If not, have you Googled for any?

I did. You will need to fiddle around with it to grab the sensor data and display as a msg instead of out to the console.

> **[Heat Index Function for Java Script](https://forum.freecodecamp.org/t/heat-index-function-for-java-script/139645/2)**
>
> Look at the top of the function void calcheatIndex he is using a formula to convert temperature and humidity into the heat index. Then the answer is in Fahrenheit and he sees if the user wants it in celsius or not. It’s the first formula here. ...

---

<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:** [22 July 2021 01:56 UTC](https://discourse.nodered.org/t/help-with-functions/48746/6 "2021-07-22T01:56:03Z")

</div>

this is the forumal frome NWS;  
**HI = -42.379 + 2.04901523_T + 10.14333127_RH - .22475541_T_RH - .00683783_T_T - .05481717_RH_RH + .00122874_T_T_RH + .00085282_T_RH_RH - .00000199_T_T_RH_RH**

---

<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:** [22 July 2021 01:57 UTC](https://discourse.nodered.org/t/help-with-functions/48746/7 "2021-07-22T01:57:36Z")

</div>

HI is heat index  
T is temp  
HR is humidity

---

<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:** [22 July 2021 01:58 UTC](https://discourse.nodered.org/t/help-with-functions/48746/8 "2021-07-22T01:58:25Z")

</div>

RH I mean

---

<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:** [22 July 2021 02:04 UTC](https://discourse.nodered.org/t/help-with-functions/48746/9 "2021-07-22T02:04:52Z")

</div>

i see now you sent a link I will look at that now . thank you.

---

<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:** [22 July 2021 02:30 UTC](https://discourse.nodered.org/t/help-with-functions/48746/10 "2021-07-22T02:30:07Z")

</div>

hi gunner I have looked at the code but still don't understand do I copy the code and put it into the function or what ?

---

<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:** [22 July 2021 02:35 UTC](https://discourse.nodered.org/t/help-with-functions/48746/11 "2021-07-22T02:35:11Z")

</div>

If you are using a `function` node, it is basically you write the formula as it is.

You still haven't shown the message with the values so this is an example:

The message looks like this - to say:

`msg = {"payload":"something","temperature":30,"humidity":30};`

And what ever else.

In the function node:

```auto
var temperature = msg.temperature;
var humidity = msg.humidity;
var feels_like = -42.379 + 2.04901523T + 10.14333127RH - .22475541TRH - .00683783TT - .05481717RHRH + .00122874TTRH + .00085282TRHRH - .00000199TTRHRH
msg.payload = feels_like;
return msg;

```

Where the _temperature_ and _humidity_ are from the initial lines where you define them from the original incoming message.

Then the _value_ you want is spat out as `msg.payload`.  
You then send that to a `chart` node and it is mapped.

---

<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:** [22 July 2021 03:00 UTC](https://discourse.nodered.org/t/help-with-functions/48746/12 "2021-07-22T03:00:57Z")

</div>

I will study this more but for tonight I must stop now thank you Ron.

---

<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:** [22 July 2021 18:01 UTC](https://discourse.nodered.org/t/help-with-functions/48746/13 "2021-07-22T18:01:32Z")

</div>

this my function so far and I get the NaN error.

```auto
var T = msg.ftemp;
var H = msg.humidity;
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;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 July 2021 18:54 UTC](https://discourse.nodered.org/t/help-with-functions/48746/14 "2021-07-22T18:54:38Z")

</div>

the equation works...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e4bafff35199b119c14e611b5e964eeeb63f8c5.png)

It is most likely `msg.ftemp` or `msg.humidity` are not numbers.

Try this...

```auto
var T = msg.ftemp;
var H = msg.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;

```

... then look in the debug window - whar to you see when the function runs?

---

<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:** [22 July 2021 19:12 UTC](https://discourse.nodered.org/t/help-with-functions/48746/15 "2021-07-22T19:12:23Z")

</div>

7/22/2021, 3:06:15 PM[node: cff8bba.e10a848](http://192.168.0.5:1880/#)function : (warn)

array[3]

0: "T"

1: null

2: "undefined"

7/22/2021, 3:06:15 PM[node: cff8bba.e10a848](http://192.168.0.5:1880/#)function : (warn)

array[3]

0: "H"

1: null

2: "undefined"

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 July 2021 19:16 UTC](https://discourse.nodered.org/t/help-with-functions/48746/16 "2021-07-22T19:16:40Z")

</div>

So that's why you get NaN, the values are not numbers.

Put a debug node before the function (set to show complete message) - do you see the temperature and humidity values?

---

<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 16:53 UTC](https://discourse.nodered.org/t/help-with-functions/48746/17 "2021-07-23T16:53:18Z")

</div>

because I am a" NEW USER" the forum kicked me off last night,  
said to wait 6 hours so Here is the error I get in the debug window.  
I have two debugs set up 1 is the data I am putting in to the fuction.  
2 is the function output. You can see I am gettig two temp reading  
and two humidity readings, so the fourmal is the wrong result.  
I know the function is not writen right or the data  
I am sending is in the wrong format

```auto
type 7/23/2021, 12:37:32 PMnode: debug 1
sht30temp : msg : Object
{ topic: "sht30temp", payload: 84.45502, qos: 1, retain: false, _msgid: "96609882.5777a8" }
7/23/2021, 12:37:32 PMnode: cff8bba.e10a848
function : (warn)
["T", 84.45502, "number"]
7/23/2021, 12:37:33 PMnode: cff8bba.e10a848
function : (warn)
["H", 84.45502, "number"]
7/23/2021, 12:37:33 PMnode: debug 1
sht30temp : msg : Object
{ topic: "sht30temp", payload: 53.32265, qos: 1, retain: false, _msgid: "96609882.5777a8" }
7/23/2021, 12:37:33 PMnode: cff8bba.e10a848
function : (warn)
["T", 53.32265, "number"]
7/23/2021, 12:37:33 PMnode: cff8bba.e10a848
function : (warn)
["H", 53.32265, "number"]
7/23/2021, 12:37:33 PMnode: debug 2
sht30temp : msg : Object
{ topic: "sht30temp", payload: 97.1269255999811, qos: 1, retain: false, _msgid: "96609882.5777a8" }
7/23/2021, 12:37:33 PMnode: debug 2
sht30temp : msg : Object
{ topic: "sht30temp", payload: 92.8997858105952, qos: 1, retain: false, _msgid: "96609882.5777a8" }

```

I don't know how to send the flow for you to see it.

---

<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 17:04 UTC](https://discourse.nodered.org/t/help-with-functions/48746/18 "2021-07-23T17:04:20Z")

</div>

This is the function as of now.

```auto
var T = msg.payload;
var H = msg.payload;
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;

```

---

<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 17:18 UTC](https://discourse.nodered.org/t/help-with-functions/48746/19 "2021-07-23T17:18:48Z")

</div>

This is the flow I think?..

```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":120,"wires":[["501656f8.157128","f0c3b57f.b3e528"]]},{"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;\nvar H = msg.payload;\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,"x":360,"y":440,"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:** ![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 22:07 UTC](https://discourse.nodered.org/t/help-with-functions/48746/20 "2021-07-23T22:07:39Z")

</div>

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

How are you reading the sensor?

P.s. I had to edit your last post because you had your text as part of the code

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

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