# Function help to deal with mqtt retained messages/influxdb "unable to parse"

**URL:** <https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218>\
**Category:** General\
**Created:** [7 March 2021 20:25 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218 "2021-03-07T20:25:42Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [7 March 2021 20:25 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/1 "2021-03-07T20:25:42Z")

</div>

When deploying, I'm getting influxdb "unable to parse" errors in debug. Only happens after deploy, and works fine at all other times.

I believe this is due to retained mqtt message not containing the full payload. In this case `linkQuality` Not sure why, as some devices do retain this.

I'm thinking to stop this, I could amend the function code, so if there is an `undefined` value, it is ignored and not passed onto influxdb. Or I am open to other suggestions.

Here is the flow:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/3/630ac5d00bb6e1d503c361546186a3c23506cd3f.png)

And here is the function:

```auto
//Convert the contact payload to 1 or 0 value
var open = msg.payload.contact === false ? 1 : 0

//Now configure the payload as normal for influxDB
msg.payload = [
    {
        contact_binary: open,
        contact: msg.payload.contact,
        batteryPercent: msg.payload.battery,
        linkQuality: msg.payload.linkquality,
        batteryVolts: msg.payload.voltage
    },
    {
        topic: "zigbee2mqtt/Xiaomi_4_letterbox",
        make: "aqara",
        location: "hallway",
        connection: "zigbee",
        power: "battery",
        friendlyname: "Letterbox"
    }
];

return msg;

```

Any advice appreciated.

---

<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:** [7 March 2021 22:05 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/2 "2021-03-07T22:05:44Z")

</div>

Are you sure it is that influx node generating the error? If so I don't see how it can be from the message you show in the debug as that doesn't even have some of the data in the error message. Unless it is in the second element of the array that is. Give the node a name to check. Or you can Search for the node id shown above the error.

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [8 March 2021 13:09 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/3 "2021-03-08T13:09:14Z")

</div>

Hi @Colin , yes it is definitely that influxdb node generating the error. I didn't expand the 2nd array element to fit it on the page better. It's just tag data which I add with the function node.

Here is an example flow showing the issue:

`[{"id":"fac377e1.b80978","type":"function","z":"be5275e8.e06c78","name":"Format front door letterbox Aqara","func":"//Convert the contact payload to 1 or 0 value\nvar open = msg.payload.contact === false ? 1 : 0\n\n\n//Now configure the payload as normal for influxDB\n\n\nmsg.payload = [\n {\n contact_binary: open,\n contact: msg.payload.contact,\n batteryPercent: msg.payload.battery,\n linkQuality: msg.payload.linkquality,\n batteryVolts: msg.payload.voltage\n },\n {\n topic: \"zigbee2mqtt/Xiaomi_4_letterbox\",\n make: \"aqara\",\n location: \"hallway\",\n connection: \"zigbee\",\n power: \"battery\",\n friendlyname: \"Letterbox\"\n }\n];\n\nreturn msg;\n\n//877 info:\n//TAG KEYS are topic/make/location/connection/power\n//TAG VALUES are \"aqara/lounge/zigbee/battery\"\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":850,"y":4210,"wires":[["8e8d224e.117fd"]]},{"id":"d736fb41.90d238","type":"debug","z":"be5275e8.e06c78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":4290,"wires":[]},{"id":"8e8d224e.117fd","type":"debug","z":"be5275e8.e06c78","name":"debug (goes to influxdb node)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1160,"y":4210,"wires":[]},{"id":"d175859e.f18018","type":"inject","z":"be5275e8.e06c78","name":"Complete payload","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zigbee2mqtt/Xiaomi_4_letterbox","payload":"{\"battery\":97,\"contact\":true,\"linkquality\":39,\"voltage\":2995}","payloadType":"json","x":460,"y":4180,"wires":[["fac377e1.b80978","d736fb41.90d238"]]},{"id":"1cdf3b83.74b9f4","type":"inject","z":"be5275e8.e06c78","name":"Partial payload","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"zigbee2mqtt/Xiaomi_4_letterbox","payload":"{\"battery\":97,\"contact\":true,\"voltage\":2995}","payloadType":"json","x":450,"y":4240,"wires":[["fac377e1.b80978","d736fb41.90d238"]]}]`

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/8/c8df77e075f31b16b411aff5c1e1a322c3e8b056.png)

It does not only happen with this flow, it occurs with any flow where there is an missing payload, which end up as `undefined`.

I'm sure there must be a way to deal with this in the function..

---

<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 March 2021 13:52 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/4 "2021-03-08T13:52:25Z")

</div>

You can easily test for undefined in a function  
`if (typeof somevar === "undefined") ...`

For future reference, in order to make flows more easily imported, see this post for details of how to share flows here - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [8 March 2021 14:56 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/5 "2021-03-08T14:56:06Z")

</div>

Trouble is, it’s not undefined when it enters the function, it’s missing from the payload. Not sure that is treated the same?

---

<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 March 2021 15:15 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/6 "2021-03-08T15:15:43Z")

</div>

Add before the return

```auto
if (typeof msg.payload[0].linkQuality === "undefined") delete msg.payload[0].linkQuality

```

or if you want to put in a default value then do it the same as you have for `open`, testing for === "undefined " and assigning the default value.

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [8 March 2021 16:12 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/7 "2021-03-08T16:12:01Z")

</div>

Ah, that works great thanks!

This can of course be used to filter all the payloads. What would be good a few lines to to check if ANY of the payloads `=== "undefined"` , and delete only that payload.

---

<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 March 2021 16:22 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/8 "2021-03-08T16:22:59Z")

</div>

There are a number of ways of doing it, this is one way

```auto
const fields = ["contact", "batteryPercent", "linkQuality"]

for (let i = 0; i<fields.length; i++) {
    if (typeof msg.payload[0][fields[i]] === "undefined") delete msg.payload[0][fields[i]]
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [8 March 2021 16:26 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/9 "2021-03-08T16:26:09Z")

</div>

That is awesome thanks for your help! 👍

---

<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 March 2021 16:38 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/10 "2021-03-08T16:38:21Z")

</div>

Actually a better way to check all of them is

```auto
let obj = msg.payload[0] // this saves typing
for (let field in obj) {
    if (obj.hasOwnProperty(field)) {
        if (typeof obj[field] === "undefined") delete obj[field]
    }
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [8 March 2021 16:39 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/11 "2021-03-08T16:39:25Z")

</div>

That is even better, no need to type in the fields!

Thanks a lot!

---

<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 March 2021 16:41 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/12 "2021-03-08T16:41:31Z")

</div>

You should report this issue against the influx node, it should not behave like that if it gets an undefined field. [Issues · mblackstock/node-red-contrib-influxdb · GitHub](https://github.com/mblackstock/node-red-contrib-influxdb/issues)

I presume you are using the latest version of the influx node. I should have asked that earlier.

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [8 March 2021 17:07 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/13 "2021-03-08T17:07:57Z")

</div>

I'm using node-red-contrib-influxdb node version 0.5.4, no update showing in pallette manager.

However, I am using influxdb 1.8.4 (Docker), as I had issues I can't remember right now with higher versions..

---

<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 March 2021 17:13 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/14 "2021-03-08T17:13:22Z")

</div>

0.5.4 is the latest influxdb node so that is ok, influxdb 1.8.4 is the latest 1.8 series so that should be fine too. So you should submit an issue so that it gets looked at.

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [8 March 2021 17:16 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/15 "2021-03-08T17:16:56Z")

</div>

No problems, I will raise an issue now 👍

---

<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 March 2021 17:17 UTC](https://discourse.nodered.org/t/function-help-to-deal-with-mqtt-retained-messages-influxdb-unable-to-parse/42218/16 "2021-03-22T17:17:39Z")

</div>

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