How can I include a check for NaN inside an object?

Hello, I am new here and I am not a programmer but have a question on a Node-RED flow that sometimes causes trouble, if one of my sensors is not available.

At first some background information:
I am using a TFA KlimaLoggPro device to collect temperature and humidity data from overall 9 sensors. 8 of them are connected wireless and sometimes the connectivity may be disturded.
All values come into my Node-RED flow via MQTT. Herefor I use the open source solution from https://weewx.com/.
All MQTT values are processed with an function-node to prepare them to be stored in an InfluxDB.

Here my function-node code:

[{"id":"35aa85af.afbc0a","type":"function","z":"14346dbc.bf7c62","name":"Fill Influx DB with KlimaLoggProData","func":"//Indoor=Flur\n    var Flur_Temperatur = Math.round(msg.payload.temp0_C * 1000) / 1000;\n    var Flur_Feuchtigkeit = Math.round(msg.payload.humidity0 * 1000) / 1000;\n// Taupunkt Flur\n    var es_Flur = { };\n    var ed_Flur = { };\n    var eln_Flur = { };\n    var td_Flur = { };\n    \n    if (Flur_Temperatur >= 0)\n    {\n    \t\t    es_Flur.payload = 6.1078*Math.pow(10,((7.5*Flur_Temperatur)/(237.3 + Flur_Temperatur)));\n    \t\t    ed_Flur.payload = Flur_Feuchtigkeit/100.0*es_Flur.payload;\n    \t\t    eln_Flur.payload = Math.log10(ed_Flur.payload/6.1078);\n    \t\t\ttd_Flur.payload = 237.3*eln_Flur.payload/(7.5 - eln_Flur.payload);\n    }\n    else\n    {\n    \t\t    es_Flur.payload = 6.1078*Math.pow(10,((7.6*Flur_Temperatur)/(240.7 + Flur_Temperatur)));\n    \t\t    ed_Flur.payload = Flur_Feuchtigkeit/100.0*es_Flur.payload;\n    \t\t    eln_Flur.payload = Math.log10(ed_Flur.payload/6.1078);\n    \t\t\ttd_Flur.payload = 240.7*eln_Flur.payload/(7.6 - eln_Flur.payload);\n    }\n// Taupunkt Flur\n\n\n\n//Sensor1=Aussen\nvar Aussen_Temperatur = Math.round(msg.payload.temp1_C * 1000) / 1000;\nvar Aussen_Feuchtigkeit = Math.round(msg.payload.humidity1 * 1000) / 1000;\n\n// Taupunkt Aussen\n    var es_Aussen = { };\n    var ed_Aussen = { };\n    var eln_Aussen = { };\n    var td_Aussen = { };\n    \n    if (Aussen_Temperatur >= 0)\n    {\n    \t\t    es_Aussen.payload = 6.1078*Math.pow(10,((7.5*Aussen_Temperatur)/(237.3 + Aussen_Temperatur)));\n    \t\t    ed_Aussen.payload = Aussen_Feuchtigkeit/100.0*es_Aussen.payload;\n    \t\t    eln_Aussen.payload = Math.log10(ed_Aussen.payload/6.1078);\n    \t\t\ttd_Aussen.payload = 237.3*eln_Aussen.payload/(7.5 - eln_Aussen.payload);\n    }\n    else\n    {\n    \t\t    es_Aussen.payload = 6.1078*Math.pow(10,((7.6*Aussen_Temperatur)/(240.7 + Aussen_Temperatur)));\n    \t\t    ed_Aussen.payload = Aussen_Feuchtigkeit/100.0*es_Aussen.payload;\n    \t\t    eln_Aussen.payload = Math.log10(ed_Aussen.payload/6.1078);\n    \t\t\ttd_Aussen.payload = 240.7*eln_Aussen.payload/(7.6 - eln_Aussen.payload);\n    }\n// Taupunkt Aussen\n\n\n\n//Sensor2=Wohnzimmer\nvar Wohnzimmer_Temperatur = Math.round(msg.payload.temp2_C * 1000) / 1000;\nvar Wohnzimmer_Feuchtigkeit = Math.round(msg.payload.humidity2 * 1000) / 1000;\n\n// Taupunkt Wohnzimmer\n    var es_Wohnzimmer = { };\n    var ed_Wohnzimmer = { };\n    var eln_Wohnzimmer = { };\n    var td_Wohnzimmer = { };\n    \n    if (Wohnzimmer_Temperatur >= 0)\n    {\n    \t\t    es_Wohnzimmer.payload = 6.1078*Math.pow(10,((7.5*Wohnzimmer_Temperatur)/(237.3 + Wohnzimmer_Temperatur)));\n    \t\t    ed_Wohnzimmer.payload = Wohnzimmer_Feuchtigkeit/100.0*es_Wohnzimmer.payload;\n    \t\t    eln_Wohnzimmer.payload = Math.log10(ed_Wohnzimmer.payload/6.1078);\n    \t\t\ttd_Wohnzimmer.payload = 237.3*eln_Wohnzimmer.payload/(7.5 - eln_Wohnzimmer.payload);\n    }\n    else\n    {\n    \t\t    es_Wohnzimmer.payload = 6.1078*Math.pow(10,((7.6*Wohnzimmer_Temperatur)/(240.7 + Wohnzimmer_Temperatur)));\n    \t\t    ed_Wohnzimmer.payload = Wohnzimmer_Feuchtigkeit/100.0*es_Wohnzimmer.payload;\n    \t\t    eln_Wohnzimmer.payload = Math.log10(ed_Wohnzimmer.payload/6.1078);\n    \t\t\ttd_Wohnzimmer.payload = 240.7*eln_Wohnzimmer.payload/(7.6 - eln_Wohnzimmer.payload);\n    }\n// Taupunkt Wohnzimmer\n\n\n\n//Sensor3=Esszimmer\nvar Esszimmer_Temperatur = Math.round(msg.payload.temp3_C * 1000) / 1000;\nvar Esszimmer_Feuchtigkeit = Math.round(msg.payload.humidity3 * 1000) / 1000;\n\n// Taupunkt Esszimmer\n    var es_Esszimmer = { };\n    var ed_Esszimmer = { };\n    var eln_Esszimmer = { };\n    var td_Esszimmer = { };\n    \n    if (Esszimmer_Temperatur >= 0)\n    {\n    \t\t    es_Esszimmer.payload = 6.1078*Math.pow(10,((7.5*Esszimmer_Temperatur)/(237.3 + Esszimmer_Temperatur)));\n    \t\t    ed_Esszimmer.payload = Esszimmer_Feuchtigkeit/100.0*es_Esszimmer.payload;\n    \t\t    eln_Esszimmer.payload = Math.log10(ed_Esszimmer.payload/6.1078);\n    \t\t\ttd_Esszimmer.payload = 237.3*eln_Esszimmer.payload/(7.5 - eln_Esszimmer.payload);\n    }\n    else\n    {\n    \t\t    es_Esszimmer.payload = 6.1078*Math.pow(10,((7.6*Esszimmer_Temperatur)/(240.7 + Esszimmer_Temperatur)));\n    \t\t    ed_Esszimmer.payload = Esszimmer_Feuchtigkeit/100.0*es_Esszimmer.payload;\n    \t\t    eln_Esszimmer.payload = Math.log10(ed_Esszimmer.payload/6.1078);\n    \t\t\ttd_Esszimmer.payload = 240.7*eln_Esszimmer.payload/(7.6 - eln_Esszimmer.payload);\n    }\n// Taupunkt Esszimmer\n\n\n\n//Sensor4=Arbeitszimmer\nvar Arbeitszimmer_Temperatur = Math.round(msg.payload.temp4_C * 1000) / 1000;\nvar Arbeitszimmer_Feuchtigkeit = Math.round(msg.payload.humidity4 * 1000) / 1000;\n\n// Taupunkt Arbeitszimmer\n    var es_Arbeitszimmer = { };\n    var ed_Arbeitszimmer = { };\n    var eln_Arbeitszimmer = { };\n    var td_Arbeitszimmer = { };\n    \n    if (Arbeitszimmer_Temperatur >= 0)\n    {\n    \t\t    es_Arbeitszimmer.payload = 6.1078*Math.pow(10,((7.5*Arbeitszimmer_Temperatur)/(237.3 + Arbeitszimmer_Temperatur)));\n    \t\t    ed_Arbeitszimmer.payload = Arbeitszimmer_Feuchtigkeit/100.0*es_Arbeitszimmer.payload;\n    \t\t    eln_Arbeitszimmer.payload = Math.log10(ed_Arbeitszimmer.payload/6.1078);\n    \t\t\ttd_Arbeitszimmer.payload = 237.3*eln_Arbeitszimmer.payload/(7.5 - eln_Arbeitszimmer.payload);\n    }\n    else\n    {\n    \t\t    es_Arbeitszimmer.payload = 6.1078*Math.pow(10,((7.6*Arbeitszimmer_Temperatur)/(240.7 + Arbeitszimmer_Temperatur)));\n    \t\t    ed_Arbeitszimmer.payload = Arbeitszimmer_Feuchtigkeit/100.0*es_Arbeitszimmer.payload;\n    \t\t    eln_Arbeitszimmer.payload = Math.log10(ed_Arbeitszimmer.payload/6.1078);\n    \t\t\ttd_Arbeitszimmer.payload = 240.7*eln_Arbeitszimmer.payload/(7.6 - eln_Arbeitszimmer.payload);\n    }\n// Taupunkt Arbeitszimmer\n\n\n\n//Sensor5=Schlafzimmer\nvar Schlafzimmer_Temperatur = Math.round(msg.payload.temp5_C * 1000) / 1000;\nvar Schlafzimmer_Feuchtigkeit = Math.round(msg.payload.humidity5 * 1000) / 1000;\n\n// Taupunkt Schlafzimmer\n    var es_Schlafzimmer = { };\n    var ed_Schlafzimmer = { };\n    var eln_Schlafzimmer = { };\n    var td_Schlafzimmer = { };\n    \n    if (Schlafzimmer_Temperatur >= 0)\n    {\n    \t\t    es_Schlafzimmer.payload = 6.1078*Math.pow(10,((7.5*Schlafzimmer_Temperatur)/(237.3 + Schlafzimmer_Temperatur)));\n    \t\t    ed_Schlafzimmer.payload = Schlafzimmer_Feuchtigkeit/100.0*es_Schlafzimmer.payload;\n    \t\t    eln_Schlafzimmer.payload = Math.log10(ed_Schlafzimmer.payload/6.1078);\n    \t\t\ttd_Schlafzimmer.payload = 237.3*eln_Schlafzimmer.payload/(7.5 - eln_Schlafzimmer.payload);\n    }\n    else\n    {\n    \t\t    es_Schlafzimmer.payload = 6.1078*Math.pow(10,((7.6*Schlafzimmer_Temperatur)/(240.7 + Schlafzimmer_Temperatur)));\n    \t\t    ed_Schlafzimmer.payload = Schlafzimmer_Feuchtigkeit/100.0*es_Schlafzimmer.payload;\n    \t\t    eln_Schlafzimmer.payload = Math.log10(ed_Schlafzimmer.payload/6.1078);\n    \t\t\ttd_Schlafzimmer.payload = 240.7*eln_Schlafzimmer.payload/(7.6 - eln_Schlafzimmer.payload);\n    }\n// Taupunkt Schlafzimmer\n\n\n\n//Sensor6=Kueche\nvar Kueche_Temperatur = Math.round(msg.payload.temp6_C * 1000) / 1000;\nvar Kueche_Feuchtigkeit = Math.round(msg.payload.humidity6 * 1000) / 1000;\n\n// Taupunkt Kueche\n    var es_Kueche = { };\n    var ed_Kueche = { };\n    var eln_Kueche = { };\n    var td_Kueche = { };\n    \n    if (Kueche_Temperatur >= 0)\n    {\n    \t\t    es_Kueche.payload = 6.1078*Math.pow(10,((7.5*Kueche_Temperatur)/(237.3 + Kueche_Temperatur)));\n    \t\t    ed_Kueche.payload = Kueche_Feuchtigkeit/100.0*es_Kueche.payload;\n    \t\t    eln_Kueche.payload = Math.log10(ed_Kueche.payload/6.1078);\n    \t\t\ttd_Kueche.payload = 237.3*eln_Kueche.payload/(7.5 - eln_Kueche.payload);\n    }\n    else\n    {\n    \t\t    es_Kueche.payload = 6.1078*Math.pow(10,((7.6*Kueche_Temperatur)/(240.7 + Kueche_Temperatur)));\n    \t\t    ed_Kueche.payload = Kueche_Feuchtigkeit/100.0*es_Kueche.payload;\n    \t\t    eln_Kueche.payload = Math.log10(ed_Kueche.payload/6.1078);\n    \t\t\ttd_Kueche.payload = 240.7*eln_Kueche.payload/(7.6 - eln_Kueche.payload);\n    }\n// Taupunkt Kueche\n\n\n\n//Sensor7=Bad\nvar Bad_Temperatur = Math.round(msg.payload.temp7_C * 1000) / 1000;\nvar Bad_Feuchtigkeit = Math.round(msg.payload.humidity7 * 1000) / 1000;\n\n// Taupunkt Bad\n    var es_Bad = { };\n    var ed_Bad = { };\n    var eln_Bad = { };\n    var td_Bad = { };\n    \n    if (Bad_Temperatur >= 0)\n    {\n    \t\t    es_Bad.payload = 6.1078*Math.pow(10,((7.5*Bad_Temperatur)/(237.3 + Bad_Temperatur)));\n    \t\t    ed_Bad.payload = Bad_Feuchtigkeit/100.0*es_Bad.payload;\n    \t\t    eln_Bad.payload = Math.log10(ed_Bad.payload/6.1078);\n    \t\t\ttd_Bad.payload = 237.3*eln_Bad.payload/(7.5 - eln_Bad.payload);\n    }\n    else\n    {\n    \t\t    es_Bad.payload = 6.1078*Math.pow(10,((7.6*Bad_Temperatur)/(240.7 + Bad_Temperatur)));\n    \t\t    ed_Bad.payload = Bad_Feuchtigkeit/100.0*es_Bad.payload;\n    \t\t    eln_Bad.payload = Math.log10(ed_Bad.payload/6.1078);\n    \t\t\ttd_Bad.payload = 240.7*eln_Bad.payload/(7.6 - eln_Bad.payload);\n    }\n// Taupunkt Bad\n\n\n\n//Sensor8=Keller\nvar Keller_Temperatur = Math.round(msg.payload.temp8_C * 1000) / 1000;\nvar Keller_Feuchtigkeit = Math.round(msg.payload.humidity8 * 1000) / 1000;\n\n// Taupunkt Keller\n    var es_Keller = { };\n    var ed_Keller = { };\n    var eln_Keller = { };\n    var td_Keller = { };\n    \n    if (Keller_Temperatur >= 0)\n    {\n    \t\t    es_Keller.payload = 6.1078*Math.pow(10,((7.5*Keller_Temperatur)/(237.3 + Keller_Temperatur)));\n    \t\t    ed_Keller.payload = Keller_Feuchtigkeit/100.0*es_Keller.payload;\n    \t\t    eln_Keller.payload = Math.log10(ed_Keller.payload/6.1078);\n    \t\t\ttd_Keller.payload = 237.3*eln_Keller.payload/(7.5 - eln_Keller.payload);\n    }\n    else\n    {\n    \t\t    es_Keller.payload = 6.1078*Math.pow(10,((7.6*Keller_Temperatur)/(240.7 + Keller_Temperatur)));\n    \t\t    ed_Keller.payload = Keller_Feuchtigkeit/100.0*es_Keller.payload;\n    \t\t    eln_Keller.payload = Math.log10(ed_Keller.payload/6.1078);\n    \t\t\ttd_Keller.payload = 240.7*eln_Keller.payload/(7.6 - eln_Keller.payload);\n    }\n// Taupunkt Keller\n\n\n\nvar data = {\n    \"timestamp\" : Number(msg.payload.dateTime)*1000,\n//    \"timestamp_parsed\" : new Date(Number(msg.payload.dateTime)*1000),\n    \"Flur_Temperatur\": Math.round(Flur_Temperatur* 10) / 10,\n    \"Flur_Feuchtigkeit\": Math.round(Flur_Feuchtigkeit* 10) / 10,\n    \"Flur_Taupunkt\": Math.round(td_Flur.payload* 10) / 10,\n    \"Aussen_Temperatur\": Math.round(Aussen_Temperatur* 10) / 10,\n    \"Aussen_Feuchtigkeit\": Math.round(Aussen_Feuchtigkeit* 10) / 10,\n    \"Aussen_Taupunkt\": Math.round(td_Aussen.payload* 10) / 10,\n    \"Wohnzimmer_Temperatur\": Math.round(Wohnzimmer_Temperatur* 10) / 10,\n    \"Wohnzimmer_Feuchtigkeit\": Math.round(Wohnzimmer_Feuchtigkeit* 10) / 10,\n    \"Wohnzimmer_Taupunkt\": Math.round(td_Wohnzimmer.payload* 10) / 10,\n    \"Esszimmer_Temperatur\": Math.round(Esszimmer_Temperatur* 10) / 10,\n    \"Esszimmer_Feuchtigkeit\": Math.round(Esszimmer_Feuchtigkeit* 10) / 10,\n    \"Esszimmer_Taupunkt\": Math.round(td_Esszimmer.payload* 10) / 10,\n    \"Arbeitszimmer_Temperatur\": Math.round(Arbeitszimmer_Temperatur* 10) / 10,\n    \"Arbeitszimmer_Feuchtigkeit\": Math.round(Arbeitszimmer_Feuchtigkeit* 10) / 10,\n    \"Arbeitszimmer_Taupunkt\": Math.round(td_Arbeitszimmer.payload* 10) / 10,\n    \"Schlafzimmer_Temperatur\": Math.round(Schlafzimmer_Temperatur* 10) / 10,\n    \"Schlafzimmer_Feuchtigkeit\": Math.round(Schlafzimmer_Feuchtigkeit* 10) / 10,\n    \"Schlafzimmer_Taupunkt\": Math.round(td_Schlafzimmer.payload* 10) / 10,\n    \"Kueche_Temperatur\": Math.round(Kueche_Temperatur* 10) / 10,\n    \"Kueche_Feuchtigkeit\": Math.round(Kueche_Feuchtigkeit* 10) / 10,\n    \"Kueche_Taupunkt\": Math.round(td_Kueche.payload* 10) / 10,\n    \"Bad_Temperatur\": Math.round(Bad_Temperatur* 10) / 10,\n    \"Bad_Feuchtigkeit\": Math.round(Bad_Feuchtigkeit* 10) / 10,\n    \"Bad_Taupunkt\": Math.round(td_Bad.payload* 10) / 10,\n    \"Keller_Temperatur\": Math.round(Keller_Temperatur* 10) / 10,\n    \"Keller_Feuchtigkeit\": Math.round(Keller_Feuchtigkeit* 10) / 10,\n    \"Keller_Taupunkt\": Math.round(td_Keller.payload* 10) / 10,\n    \"interval_minute\": Math.round(msg.payload.interval_minute* 10) / 10,\n    };\n\nvar topic = \"KlimaLoggProData\";\n\nmsg.topic = topic;\nmsg.payload = data;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":530,"y":580,"wires":[["915c7f79.b8183"]]}]

This flow works pretty good, as long as all sensors are available. The problem comes, when the wireless connectivity is disturbed. If this happens, the values for the affected sensor just show "NaN" and then the whole object cannot be inserted in the InfluxDB.

So now I am searching for an optimization of my function-node, in order to check whether any data is "Nan" and in this case, the data for the affected sensor should not be included in my data object anymore.

I tried things like
...

var data = {
    "timestamp" : Number(msg.payload.dateTime)*1000,
    if (isNaN(Flur_Temperatur) = false)
    {
        "Flur_Temperatur": Math.round(Flur_Temperatur* 10) / 10,
        "Flur_Feuchtigkeit": Math.round(Flur_Feuchtigkeit* 10) / 10,
        "Flur_Taupunkt": Math.round(td_Flur.payload* 10) / 10
    }
    else
    {
    },

...

but this does not work.

Can anybody tell me, what I am doing wrong? Or even better, how I can get the result I want?

Thanks,
MaDo

That would be
if (isNaN(Flur_Temperatur) === false
or
if (!isNaN(Flur_Temperatur))

Thank you E1cid for your quick response. I tried this already before but it did not fix my problem.

var data = {
    "timestamp" : Number(msg.payload.dateTime)*1000,
    if (isNaN(Flur_Temperatur) === false)
        {
            "Flur_Temperatur": Math.round(Flur_Temperatur* 10) / 10,
            "Flur_Feuchtigkeit": Math.round(Flur_Feuchtigkeit* 10) / 10,
            "Flur_Taupunkt": Math.round(td_Flur.payload* 10) / 10
        }
    else 
        {
        },
    if (isNaN(Aussen_Temperatur) === false)
        {
            "Aussen_Temperatur": Math.round(Aussen_Temperatur* 10) / 10,
            "Aussen_Feuchtigkeit": Math.round(Aussen_Feuchtigkeit* 10) / 10,
            "Aussen_Taupunkt": Math.round(td_Aussen.payload* 10) / 10,
        }
    else
        {
        },
     ...

In Node-RED in get some error information, see here with the "red x":
KlimaLoggfunctionextract

See here with the error message:
KlimaLoggfunctionextractwitherror

I do not understand how to fix this.

If a sensor is not available wireless, any value for this sensor is NaN, so I only want to check, if one is NaN.
If this is NOT the case, I want to have the three sensor values included in my object.
If it is NaN, then I want to ignore the three values for my object, so that it gets shorter.

Is it understandable what I would like to do?

I see you are doing if statements inside a object.
might be best to do.

var data = {
    "timestamp" : Number(msg.payload.dateTime)*1000
}
    if (isNaN(Flur_Temperatur) === false)
        {
            data.Flur_Temperatur = Math.round(Flur_Temperatur* 10) / 10
            data.Flur_Feuchtigkeit = Math.round(Flur_Feuchtigkeit* 10) / 10
            data.Flur_Taupunkt = Math.round(td_Flur.payload* 10) / 10
        }
    
    if (isNaN(Aussen_Temperatur) === false)
        {
            data.Aussen_Temperatur = Math.round(Aussen_Temperatur* 10) / 10
            data.Aussen_Feuchtigkeit = Math.round(Aussen_Feuchtigkeit* 10) / 10
            data.Aussen_Taupunkt = Math.round(td_Aussen.payload* 10) / 10
        }
    
return msg;

Thank you E1cid, that solved my problem :smile:

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