# Snmp data visualization

**URL:** <https://discourse.nodered.org/t/snmp-data-visualization/83147>\
**Category:** General\
**Tags:** function-node\
**Created:** [24 November 2023 13:07 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147 "2023-11-24T13:07:28Z")\
**Posts on this page:** 18\
**Page:** 2

<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:** [28 November 2023 21:09 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/21 "2023-11-28T21:09:56Z")

</div>

Can you try this please

```auto
node.warn(`payload: ${JSON.stringify(msg.payload)}`)
node.warn(`typeof data: ${typeof msg.payload[0].value.data}`)
if (typeof msg.payload[0].value.data === "object") {
    msg.payload[0].value = Buffer.from(msg.payload[0].value.data).toString()
}
return msg;

```

and if that doesn't do it

```auto
node.warn(`payload: ${JSON.stringify(msg.payload)}`)
node.warn(`typeof value: ${typeof msg.payload[0].value}`)
if (typeof msg.payload[0].value === "object") {
    msg.payload[0].value = Buffer.from(msg.payload[0].value.data).toString()
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![cef](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cef/32/85277_2.png) [@cef](https://discourse.nodered.org/u/cef)\
**Post date:** [29 November 2023 18:13 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/22 "2023-11-29T18:13:12Z")

</div>

Hi, i did the probe but the result is the same with the first function  
 ![WhatsApp Image 2023-11-29 at 14.44.47_832b9896](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe77b33ffe3633d01ab304294580d2cddf22595f.jpeg)  
the same happens in the text file  
and with second function there is an error

![WhatsApp Image 2023-11-29 at 14.52.07_8b5f4d93](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/c/9c59921ac6845cfab20bc7387545536a2e4f0a35.jpeg)

I want tell you that in one machine I could go to the old snmp node version 0.0.25 and inmediatly start work ok, really the problem is clear.  
I appreciate your time and effort and I'm really grateful, is not nescessary that you lose your time in this problem. If you want to do some more probes I can put your functions in the flow an probe it, if not, is all right.  
Unfortunately I can't do changes over your functions because I don't have the knowledge.  
Thank you

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 November 2023 20:40 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/23 "2023-11-29T20:40:34Z")

</div>

Late to the thread - but if you want to change the value to a string then you can try

```auto
if (typeof msg.payload[0].type === 4) {
    msg.payload[0].value = msg.payload[0].value.data.toString()
}
return msg;

```

---

<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:** [29 November 2023 21:03 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/24 "2023-11-29T21:03:09Z")

</div>

I would really like to get to the bottom of why my function doesn't work. It certainly can be made to word, it is just a matter of better understanding the data format, then you won't have to worry about being stuck with an old version which may fail to work at some point. Can you try this please.

```auto
node.warn(`payload in: ${JSON.stringify(msg.payload)}`)
node.warn(`typeof data: ${typeof msg.payload[0].value.data}`)
if (typeof msg.payload[0].value.data === "object") {
    node.warn("Modifying")
    msg.payload[0].value = Buffer.from(msg.payload[0].value.data).toString()
} 
node.warn(`payload out: ${JSON.stringify(msg.payload)}`)
return msg;

```

I would expect to see something like this

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/2/02ee7d3039c83b2ed4d10e014c3f350daea95d17.png)

---

<div class="post-metadata">

**Author:** ![cef](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cef/32/85277_2.png) [@cef](https://discourse.nodered.org/u/cef)\
**Post date:** [29 November 2023 21:14 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/25 "2023-11-29T21:14:16Z")

</div>

Thank you! Never is late, worst is never…

I have probed a lot of functions that simulated works ok, the problem is when I use SNMP NODE + FUNCTION (string conversion) + Write to file node

I’m not a programmer, I’m only an user, I understand somethings but I don’t have the knowledge for introduce changes like this. Really I don’t know what is happening.

But I found a solution using an old SNMP NODE version, from v2.0.0 to v0.0.25 and problem was solved.

Thank you for your response

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [29 November 2023 22:11 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/26 "2023-11-29T22:11:40Z")

</div>

You can see the code that was commented out when I "fixed" it for version 2

> <https://github.com/node-red/node-red-nodes/blob/97a370c4989badf79478b6a013bb82ff2b1e4f14/io/snmp/snmp.js#L256>

The reason for the change was that the underlying snmp returns a unique snmp object type - that other users were expecting - and by my changing just the octet string type it broke the object they were expecting, so it was decided to just return the native object as returned from the library so the user could process it later as required.

---

<div class="post-metadata">

**Author:** ![cef](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cef/32/85277_2.png) [@cef](https://discourse.nodered.org/u/cef)\
**Post date:** [30 November 2023 12:40 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/27 "2023-11-30T12:40:42Z")

</div>

> [@Colin](#):
>
> ```auto
> node.warn(`payload in: ${JSON.stringify(msg.payload)}`)
> node.warn(`typeof data: ${typeof msg.payload[0].value.data}`)
> if (typeof msg.payload[0].value.data === "object") {
> node.warn("Modifying")
> msg.payload[0].value = Buffer.from(msg.payload[0].value.data).toString()
> } 
> node.warn(`payload out: ${JSON.stringify(msg.payload)}`)
> return msg;
> 
> ```

HI, I understand the advantages of solve it using the newest version  
this are the results and the flow, function1 have your code, like you can see, in text file value appear in raw mode

 ![WhatsApp Image 2023-11-30 at 09.31.23_b588bfb0](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/835a319c320cbe23f38775080cf421b7e7dbde33.jpeg)

 ![WhatsApp Image 2023-11-30 at 09.31.50_740509b7](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/313fac45bd127bfd8ca199a9289ace0ade1f3a5e.jpeg)

![WhatsApp Image 2023-11-30 at 09.33.07_89e83db8](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1c0fe4eaa769357905ef6507b36b6b6a4e9b5f71.jpeg)

I don't know if yuo can understand excactly what is explainig dceejay, I don't know if he is eplaining the problem or the solution

---

<div class="post-metadata">

**Author:** ![cef](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cef/32/85277_2.png) [@cef](https://discourse.nodered.org/u/cef)\
**Post date:** [30 November 2023 12:45 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/28 "2023-11-30T12:45:19Z")

</div>

Thank you, I understand that version 2.0.0 of snmp node contains changes that requires a modification in the way that value must be showed. But in the code that you have posted, you show a solution or the source of he problem?  
I followed the link in giihub, I can download the code,, is this a version of snmp node? can I install it?  
sorry by my ignorance.

---

<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:** [30 November 2023 16:32 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/29 "2023-11-30T16:32:43Z")

</div>

@dceejay or anyone else, looking at my function and contents of the incoming payload can anyone understand how `typeof msg.payload[0].value.data` is undefined?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [30 November 2023 16:37 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/30 "2023-11-30T16:37:10Z")

</div>

as I said the snmp lib returns a weird non-standard object so yeah I don't know as I haven't dug into it (not using snmp for anything right now) - but is probably related to that.

---

<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:** [30 November 2023 16:51 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/31 "2023-11-30T16:51:09Z")

</div>

It makes it rather difficult to deal with the data if it cannot be accessed like a normal object.

@cef can you try this, which attempts to sanitise the data

```auto
msg.payload = JSON.parse(JSON.stringify(msg.payload))
node.warn(`payload in: ${JSON.stringify(msg.payload)}`)
node.warn(`typeof data: ${typeof msg.payload[0].value.data}`)
if (typeof msg.payload[0].value.data === "object") {
    node.warn("Modifying")
    msg.payload[0].value = Buffer.from(msg.payload[0].value.data).toString()
} 
node.warn(`payload out: ${JSON.stringify(msg.payload)}`)
return msg;

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [30 November 2023 16:54 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/32 "2023-11-30T16:54:12Z")

</div>

why turn it back into a Buffer ? It's a string at the end of the day.  
And if it's the .value that is the issue then maybe set msg.payload[0].foo instead - ie a new different property

---

<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:** [30 November 2023 16:58 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/33 "2023-11-30T16:58:56Z")

</div>

> [@dceejay](#):
>
> It's a string at the end of the day.

Is it? `typeof` says it is undefined.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [30 November 2023 17:07 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/34 "2023-11-30T17:07:49Z")

</div>

snmp data type 4 is an OctetString (also shown as the tstr property). so just bytes as a string. So in theory it's safe to turn into a string... BUT when I did that it caused other (real power) snmp users to have problems and so I "fixed" it to send what they expect and let them handle it.

---

<div class="post-metadata">

**Author:** ![cef](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cef/32/85277_2.png) [@cef](https://discourse.nodered.org/u/cef)\
**Post date:** [30 November 2023 19:45 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/35 "2023-11-30T19:45:05Z")

</div>

Hi Colin, tomorow I will probe it and I’ll tell you

---

<div class="post-metadata">

**Author:** ![cef](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cef/32/85277_2.png) [@cef](https://discourse.nodered.org/u/cef)\
**Post date:** [1 December 2023 12:23 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/36 "2023-12-01T12:23:07Z")

</div>

> [@Colin](#):
>
> ```auto
> msg.payload = JSON.parse(JSON.stringify(msg.payload))
> node.warn(`payload in: ${JSON.stringify(msg.payload)}`)
> node.warn(`typeof data: ${typeof msg.payload[0].value.data}`)
> if (typeof msg.payload[0].value.data === "object") {
> node.warn("Modifying")
> msg.payload[0].value = Buffer.from(msg.payload[0].value.data).toString()
> } 
> node.warn(`payload out: ${JSON.stringify(msg.payload)}`)
> return msg;
> 
> ```

Hi Colin  
congratulations...it is working!!!  
I don't know really what you are changing, I know what you were looking for but is working

Thank you for your time!!!

---

<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:** [1 December 2023 16:12 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/37 "2023-12-01T16:12:49Z")

</div>

Can I clarify something? On the system with the problem do all messages have this format, or do some have the old. The purpose of the `if` clause in the code is to determine which of those it is, so is not necessary if it does not need to handle both types of messages. Also now it is working you can remove the `node.warn()` lines which were for diagnostic output to help work out what is going on.

Also, in order to get to the simplest solution, could you try, as suggested by @dceejay just this in the function

```auto
msg.payload[0].value = msg.payload[0].value.toString()
return msg

```

and if that doesn't work

```auto
msg.payload[0].value = msg.payload[0].value.data.toString()
return msg

```

---

<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:** [30 January 2024 16:13 UTC](https://discourse.nodered.org/t/snmp-data-visualization/83147/38 "2024-01-30T16:13:17Z")

</div>

Closed on 30 January 2024 16:13 UTC

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

[Previous page](https://discourse.nodered.org/t/snmp-data-visualization/83147.md?page=1)
