# Function: sendIt - avoid incorrect values

**URL:** <https://discourse.nodered.org/t/function-sendit-avoid-incorrect-values/87748>\
**Category:** General\
**Tags:** function-node\
**Created:** [6 May 2024 13:19 UTC](https://discourse.nodered.org/t/function-sendit-avoid-incorrect-values/87748 "2024-05-06T13:19:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [6 May 2024 13:19 UTC](https://discourse.nodered.org/t/function-sendit-avoid-incorrect-values/87748/1 "2024-05-06T13:19:53Z")

</div>

Hi, i´m using the following function to send some values to my influxdb:

```auto
function sendIt(topic, address, roundTo) {
    let m = {
        topic: topic,
        payload: parseFloat(msg.payload[address].toFixed(roundTo))
    } 
    node.send(m);
}
sendIt("VLT", 40008, 1);
sendIt("RLT", 40012, 1);
sendIt("WWo", 40013, 1);
sendIt("WW", 40014, 1);
sendIt("KTe", 40015, 1);
sendIt("KTa", 40016, 1);
sendIt("Hz", 43136, 0);
sendIt("GM", 43005, 0);
sendIt("WT%", 43437, 0);
sendIt("KT%", 43439, 0);
sendIt("HG", 40018, 1);
sendIt("Vol", 40072, 1);
sendIt("Pth", 'Pth', 0);

```

sometimes I get incorrect values from: VLT, RLT, WWo.  
for example: -78°C

I want to prevent that these speaks get into my database.  
Is there a way to integrate this into the function?

if "WWo" is \< 0°C =\> "WWo" = 0°C

greetings

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [6 May 2024 13:24 UTC](https://discourse.nodered.org/t/function-sendit-avoid-incorrect-values/87748/2 "2024-05-06T13:24:00Z")

</div>

Without seeing the input `msg.payload` object we probably can't help here.

And what have you tried?

---

<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:** [6 May 2024 13:28 UTC](https://discourse.nodered.org/t/function-sendit-avoid-incorrect-values/87748/3 "2024-05-06T13:28:30Z")

</div>

> [@Becker](#):
>
> if "WWo" is \< 0°C =\> "WWo" = 0°C

this is a bit confusing but i guess you mean if the value for address 40013 is \< `0`, set it to `0`. If yes, then something like:

```auto
function sendIt(topic, address, roundTo) {
    let value = msg.payload[address]
    if (topic === 'WWo' && value < 0) {
        value = 0
    }
    const m = {
        topic: topic,
        payload: parseFloat(value.toFixed(roundTo))
    }
    node.send(m);
}

```

---

<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:** [6 May 2024 13:32 UTC](https://discourse.nodered.org/t/function-sendit-avoid-incorrect-values/87748/4 "2024-05-06T13:32:33Z")

</div>

TBH, this is probably a bit better:

```auto
function sendIt(topic, address, roundTo, min, max) {
    let value = msg.payload[address]
    if (typeof min === "number") {
        if (value < min) {
            value = min
        }
    }
    if (typeof max === "number") {
        if (value > max) {
            value = max
        }
    }
    const m = {
        topic: topic,
        payload: parseFloat(value.toFixed(roundTo))
    }
    node.send(m);
}

sendIt("VLT", 40008, 1, 0, null); // min 0
sendIt("RLT", 40012, 1, 0.1, null); // min 0.1
sendIt("WWo", 40013, 1, -0.5, 1.9); // min -0.5, max 1.9
sendIt("WW", 40014, 1, null, 10.5); // max 10.5
sendIt("KTe", 40015, 1); // no limits
sendIt("KTa", 40016, 1); // no limits
sendIt("Hz", 43136, 0); // etc
sendIt("GM", 43005, 0);
sendIt("WT%", 43437, 0);
sendIt("KT%", 43439, 0);
sendIt("HG", 40018, 1);
sendIt("Vol", 40072, 1);
sendIt("Pth", 'Pth', 0);

```

---

<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:** [6 May 2024 13:35 UTC](https://discourse.nodered.org/t/function-sendit-avoid-incorrect-values/87748/5 "2024-05-06T13:35:09Z")

</div>

BTW, you should NEVER round values going into the database.

Otherwise if you ever do a summation across the month or year, you will lose precision.

Round them upon display only.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 May 2024 13:36 UTC](https://discourse.nodered.org/t/function-sendit-avoid-incorrect-values/87748/6 "2024-05-06T13:36:56Z")

</div>

I do things slightly differently because I'm more interested in ensuring that I don't end up with text values instead of numbers:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/daf9a29b8f01741033c6a46d70134f0e5a838804.png)

All of my sensor outputs to to the `one_week` db and the function node ensures that data is correctly formed.

```auto
/**
 * Validate input suitable for the influxdb-out node.
 * We will ALWAYS use the "array containing two objects" payload
 * OR the singe Object payload (if no tags being used).
 * See the Description tab for more details.
 */

// check measurement field is set - if not exit with error
if ( ! msg.measurement ) {
    node.error('msg.measurement is missing')
    node.send([null,msg])
    return
}

let fields,tags

// if payload is an object, assume that it contains fieldName:fieldValue pairs
if ( msg.payload!== null && msg.payload.constructor.name === 'Object' ) {
    fields = msg.payload
} else if ( msg.payload!== null && msg.payload.constructor.name === 'Array' ) {
    node.error('msg.payload cannot be an array. It must be an object containing fieldName:fieldValue pairs or a single value (which would be written to the `value` field name).')
    node.send([null,msg])
    return
} else {
    // Otherwise, we always use 'value' as the default field name
    fields = {'value': msg.payload}
}

const lstFields = Object.keys(fields)

// check to make sure that there is a value field name - if not, continue but with a warning
if ( ! lstFields.includes('value') ) {
    // Lets us turn off the warning if we know what we are doing :-)
    if ( msg.noValueField !== true )
        node.warn('Default field name "value" not present, was that deliberate? Set msg.noValueField=true or use the `value` field name to avoid this msg')
}

// check to make sure that all field values are numeric - if not, exit with a warning
let allNumeric = true
lstFields.forEach( key => {
    // I use On/Off for simple switch values in MQTT but these are not useful
    // in InfluxDB, so translate them to ON=1 and OFF=0 (ignoring case).
    try {
        if ( fields[key].toLowerCase() === 'on' ) fields[key] = 1
        if ( fields[key].toLowerCase() === 'off' ) fields[key] = 0
    } catch (e) {}

    // then check to make sure the field is actually a number
    if ( parseFloat(fields[key]) !== fields[key] ) {
        node.error(`Field msg.payload.${key} is not numeric. Only use numbers for field values, text should go in tags. Value=${fields[key]}`)
        allNumeric = false
        node.send([null,msg])
        return
    }
})
// @ts-ignore
if ( allNumeric === false ) {
    return
}

// check to make sure that if msg.tags is present, it is an object - if not, exit with a warning
if ( msg.tags ) {
    if ( !(msg.tags!== null && msg.tags.constructor.name === 'Object') ) {
        node.error('msg.tags is not an object - it must contain tagName:tagValue pairs')
        node.send([null,msg])
        return
    }
    tags = msg.tags
}

// Format the output to go to the InfluxDB out node
if ( msg.tags ) {
    msg.payload = [
        fields,
        tags,
    ]
} else {
    msg.payload = fields
}

return msg

```

This has saved me loads of grief over the years. 😀

---

<div class="post-metadata">

**Author:** ![Becker](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Becker](https://discourse.nodered.org/u/Becker)\
**Post date:** [6 May 2024 14:47 UTC](https://discourse.nodered.org/t/function-sendit-avoid-incorrect-values/87748/7 "2024-05-06T14:47:12Z")

</div>

thank you

@Steve-Mcl I´m rouding because of the node before: calculate ⌀ of 1min, so that I have the same decimal place again in my database every minute, that's accurate enough.

@TotallyInformation I have to take a look at this in peace 😉

---

<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:** [8 May 2024 13:25 UTC](https://discourse.nodered.org/t/function-sendit-avoid-incorrect-values/87748/8 "2024-05-08T13:25:00Z")

</div>

> [@Becker](#):
>
> I´m rouding because of the node before: calculate ⌀ of 1min, so that I have the same decimal place again in my database every minute, that's accurate enough.

What benefit do you see to throwing away information?

---

<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:** [22 May 2024 13:25 UTC](https://discourse.nodered.org/t/function-sendit-avoid-incorrect-values/87748/9 "2024-05-22T13:25:24Z")

</div>

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