# What is wrong with my function. Please

**URL:** https://discourse.nodered.org/t/what-is-wrong-with-my-function-please/55211
**Category:** General
**Created:** [15 December 2021 03:30 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-please/55211 "2021-12-15T03:30:11Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![bogi](https://avatars.discourse-cdn.com/v4/letter/b/e47774/32.png) [@bogi](https://discourse.nodered.org/u/bogi)
#### Post date: [15 December 2021 03:30 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-please/55211/1 "2021-12-15T03:30:11Z")

</div>

Guys,

i'm fighting with my Flow. I'm receiving mqqt messages from ESP01 which reads BME280 and 32 ADC channels (NTC resistors for temprature readings). Additionally i hooked up PZEM-004 for power monitoring. All of that to tiny ESP01.  
ESP01 firmware i wrote created topic in JSON form manually (i'm not using ArduinoJson lib).  
My Flow receives it with no problem, parsing to object then i'm using Function node to make NTC calculations (to translate ADC reading of NTC to its resistances in Ohms and then, using Steinhart-Hart equation, to calculate temperature. In the same Function node im preparing output to InfluxDB.

## The problem is i'm getting "NaN" instead of temperature readings... Here is my function:

```auto
let device = msg.payload.dev;
msg.payload = {
       "cisnienie" : msg.payload.pre,
       "temperatura" : msg.payload.tem,
       "wilgotnosc" : msg.payload.hum,
       "napiecie_sieciowe" : msg.payload.vol,
       "pobor_pradu" : msg.payload.cur,
       "moc_pobierana" : msg.payload.pwr,
       "licznik_energii" : msg.payload.ene,
       "czestotliwosc" : msg.payload.frq,
       "PF" : msg.payload.p_f,
       "czujnik_gazu" : msg.payload.gaz,
       "ADC": msg.payload.ADC}
//now get 32 values from ADC
for (let i = 0; i < 32; i++)
{
    let U_ADC_int = Number(msg.payload.ADC[i]); //i have now 16-bit integer value from ADC in 
    U_ADC_int
    if ((U_ADC_int >= 1000)&(U_ADC_int < 40000)) //if integer value is within the range (1000 is ca 
     50mV, 40000 is ca 2 V) then convert integers to volts (65536 is 3.3V)
    {
    let U_zasil_int = Number(msg.payload.zas); //U_zasil is measured power supply value
    var R = 10000 * (U_ADC_int / (U_zasil_int - U_ADC_int)) //10000 is for 10kOhm resistor used in 
    divider, calculation is just Ohm Law and voltage divider.
    //and then Steinhart-Hart:
    var R0 = 10000; //10k type NTC
    var T0 = 273.15 + 25;
    var B = 3950; //NTC coefficient
    var T = 1 / ( (1/T0) + (1/B) * Math.log(R/R0) );
    T = T - 273.15;
    msg.payload[`kanal_${(i+1)}`] = Number(T.toFixed(1));
    } else{
   //the rest of the code to process ADC values outside of range: i.e. when i put jumper instead of NTC so i can use ADC channel for switches or connect photoresistor and so on... 
   } 
}
delete msg.payload.ADC;
msg.payload = [msg.payload, {"deviceID" : device, "location": "basement"}];
return msg;

```

* * *

This is the continuation of the threat that was automatically closed... Well, i haven't had time to play with NodeRED... Here is the thread:

> [@Extracting part of the data from JSON payload, perform calcs and combine them back](https://discourse.nodered.org/t/extracting-part-of-the-data-from-json-payload-perform-calcs-and-combine-them-back/46742/13):
>
> Try this function let device = msg.payload.device; msg.payload = { "cisnienie" : msg.payload.pressure, "temperatura" : msg.payload.temperature, "wilgotnosc" : msg.payload.humidity, "napieceie\_sieciowe" : msg.payload.voltage, "pobor\_pradu" : msg.payload.current, "moc\_pobierana" : msg.payload.power, "licznik\_energii" : msg.payload.energy, "czestotliwosc" : msg.payload.frequency, "PF" : msg.payload.power\_factor, "czujnik\_gazu" …

As you can see i've changed approach to reduce the amount of data sent via mqtt - shorter names and 16bit ADC values instead of floats. Now i need to calculate voltage value and then resistance value in NodeRed instead of in ESP8266. The reason for that is simplicity of changing things in NodeRED. Imagine i'm connecting photoresistor instead of NTC. In the old approach i would have to redo firmware and flash ESP. By sending raw ADC data to NodeRED i can modify Function node and go further.

The other thing is: sometimes some NTC will be disconnected or values are outside of the range - in this case i need to discard them and not to sent to InfluxDB so it will be not shown in Grafana trends.  
For this i'm just using this:  
msg.payload[`kanal_${(i+1)}`] = Number(T.toFixed(1));  
inside the loop. Is there some different approach to achieve it? And regarding toFixed() method - this one is just truncating the value. But 21.19 C become 21.1 C. What is the method for Math rounding? I found too many confusion regarding rounding and in many places it is recommended to use toFixed(). But what about rounding?  
I've checked JS Math methods. But Math.round(x) returns x rounded to its nearest integer but i need not integer but float with on decimal place accuracy. The only way i can think to sort it is multiplying my temmperature by 10, applying Math.round(x) and dividing by 10 to go back to float. Kinda stupid.

JavaScript is new to me. I'm C language amateur:)  
Thank you for advices!

---

<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: [15 December 2021 05:17 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-please/55211/2 "2021-12-15T05:17:20Z")

</div>

Only on a limb, but I think it is how the _values_ are derived.

`"cisnienie" : msg.payload.pre,`

As an example.

Try (and **NO** promises)  
`"cisnienie" : parseInt(msg.payload.pre),`

See if that makes a difference.

---

<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: [15 December 2021 08:28 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-please/55211/3 "2021-12-15T08:28:40Z")

</div>

Feed the message going into the function into a debug node and show us what it says.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [15 December 2021 09:20 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-please/55211/4 "2021-12-15T09:20:02Z")

</div>

Without seeing input payload i can see two things wrong  
1 Some of your comments are are on more than one line.  
2 msg.payload.zas is referenced in the loop does not exist.

To round to 1 deciaml place use

```auto
Math.round(T*10)/10

```

---

<div class="post-metadata">

### Author: ![bogi](https://avatars.discourse-cdn.com/v4/letter/b/e47774/32.png) [@bogi](https://discourse.nodered.org/u/bogi)
#### Post date: [16 December 2021 02:29 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-please/55211/5 "2021-12-16T02:29:37Z")

</div>

Thanks Guys,  
**Colin** - i'll put Function node input here when i get home.  
**Trying\_to\_learn** - actually `cisnienie" : msg.payload.pre,` and others there work with no problem.  
**E1cid** - ad. 1: it lloks like this only here on forum. Looks like "reply" window here on the forum is too narrow. In the Function node it is in the same line, properly commented out  
For rounding looks logical - i'll use it. Thank you.

So far i found a problem here:

> [@bogi](#):
>
> `let U_zasil_int = Number(msg.payload.zas);`

Whean i changed it to:

> [@bogi](#):
>
> `let U_zasil_int = 26500;`

where 26500 is the value i get via mqtt and i can see this value in the Function node input(!), then the calculations are fine and all the temperatures are correct and parsed correctly to influxDB.

I bet you Guys are using Grafana as well - do you know how to solve the following problem: in case i loose some data (i.e. disconnect temperature probe) the value will go out of range and i have to filter it to not to send it to InfluxDB. I already know how to do it using \< and \> comparators and if out of scope i will just not create:

> [@bogi](#):
>
> `msg.payload[`kanal\_${(i+1)}`] = Number(T.toFixed(1));`

for that particular channel. **However** , on the Grafana side i'm having problems to properly visualize gaps in the trends... Any clue?

Thank you,  
bogi

---

<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: [16 December 2021 04:41 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-please/55211/6 "2021-12-16T04:41:30Z")

</div>

> [@bogi](#):
>
> I bet you Guys are using Grafana as well

Not me sorry.

But all the best with getting it working.

It can be frustrating. Hang in there.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [16 December 2021 07:02 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-please/55211/7 "2021-12-16T07:02:46Z")

</div>

> [@bogi](#):
>
> So far i found a problem here:
> 
> > [@bogi](#):
> >
> > `let U_zasil_int = Number(msg.payload.zas);`
> 
> Whean i changed it to:
> 
> > [@bogi](#):
> >
> > `let U_zasil_int = 26500;`
> 
> where 26500 is the value i get via mqtt and i can see this value in the Function node input(!)

As stated msg.payload.zas does not exist. You overwrote msg.payload at the start of the code.

---

<div class="post-metadata">

### Author: ![bogi](https://avatars.discourse-cdn.com/v4/letter/b/e47774/32.png) [@bogi](https://discourse.nodered.org/u/bogi)
#### Post date: [21 December 2021 00:37 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-please/55211/8 "2021-12-21T00:37:45Z")

</div>

E1cid - you're right. i've moved it to the top and it's fine. Thanks!

---

<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: [19 February 2022 00:37 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-please/55211/9 "2022-02-19T00:37:52Z")

</div>

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