# Msg from function block

**URL:** <https://discourse.nodered.org/t/msg-from-function-block/61207>\
**Category:** General\
**Tags:** function-node\
**Created:** [12 April 2022 22:51 UTC](https://discourse.nodered.org/t/msg-from-function-block/61207 "2022-04-12T22:51:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonkers71](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonkers71/32/59714_2.png) [@jonkers71](https://discourse.nodered.org/u/jonkers71)\
**Post date:** [12 April 2022 22:51 UTC](https://discourse.nodered.org/t/msg-from-function-block/61207/1 "2022-04-12T22:51:44Z")

</div>

Hello. I have been using net-snmp inside a function node to get some snmpv3 information.

I thanks to some help earlier I was able to get it to send the message with the information I need.

I am wanting to use a switch node though to send the information like temp, voltage, ect to different nodes but I am unable to get it to work. I have 0 js knowledge so i am finding it hard. Hoping somebody can point me in the right direction.

Here is what I have so far.

```auto
var user = {
name: " ********",
level: snmp.SecurityLevel.authPriv,
authProtocol: snmp.AuthProtocols.md5,
authKey: " **********",
privProtocol: snmp.PrivProtocols.aes,
privKey: " ********"
};

var session = snmp.createV3Session (" ***.***. ***.***", user);

var oids = ["*.*.*.*.*.*.*.*.*", "*.*.*.*.*.*.*.*.*"];

session.get (oids, function (error, varbinds) {
    if (error) {
        console.error (error);
    } else {
        for (var i = 0; i < varbinds.length; i++) {
            if (snmp.isVarbindError (varbinds[i])) {
                console.error (snmp.varbindError (varbinds[i]));
            } else {
                msg.payload.temp = varbinds[0].value;
                msg.payload.current= varbinds[1].value
                msg.payload.voltage = varbinds[2].value
node.send(msg)
            }
        }
    }
    session.close ();
});

```

Thanks.

---

<div class="post-metadata">

**Author:** ![homeplayer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/homeplayer/32/12388_2.png) [@homeplayer](https://discourse.nodered.org/u/homeplayer)\
**Post date:** [15 April 2022 10:13 UTC](https://discourse.nodered.org/t/msg-from-function-block/61207/2 "2022-04-15T10:13:58Z")

</div>

In your function node click setup, set outputs to 3

```auto
var varbinds = [1,2,3];

var first = {payload: varbinds[0]};
var second = {payload: varbinds[1]};
var last = {payload: varbinds[2]};

return [first,second,last];

```

```auto
[
    {
        "id": "9edca76c921390d3",
        "type": "inject",
        "z": "6c108793ad655479",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "x": 170,
        "y": 660,
        "wires": [
            [
                "b5d217c77f1cbbd0"
            ]
        ]
    },
    {
        "id": "b5d217c77f1cbbd0",
        "type": "function",
        "z": "6c108793ad655479",
        "name": "",
        "func": "var varbinds = [1,2,3];\n\nvar first = {payload: varbinds[0]};\nvar second = {payload: varbinds[1]};\n var last = {payload: varbinds[2]};\n\nreturn [first,second,last];\n",
        "outputs": 3,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 360,
        "y": 660,
        "wires": [
            [
                "1149932b5a5cc64b"
            ],
            [
                "9ab82f9ea4289141"
            ],
            [
                "c11293604c2c05b1"
            ]
        ]
    },
    {
        "id": "1149932b5a5cc64b",
        "type": "debug",
        "z": "6c108793ad655479",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 600,
        "y": 640,
        "wires": []
    },
    {
        "id": "9ab82f9ea4289141",
        "type": "debug",
        "z": "6c108793ad655479",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 610,
        "y": 700,
        "wires": []
    },
    {
        "id": "c11293604c2c05b1",
        "type": "debug",
        "z": "6c108793ad655479",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 590,
        "y": 740,
        "wires": []
    }
]

```

---

<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:** [15 April 2022 13:47 UTC](https://discourse.nodered.org/t/msg-from-function-block/61207/3 "2022-04-15T13:47:44Z")

</div>

If you had used the code I gave you in your other post, you would see I pushed the oid into the topic.

> [@Function output payload](https://discourse.nodered.org/t/function-output-payload/61098/2):
>
> Firstly, is there a reason you are not using [node-red-node-snmp](https://flows.nodered.org/node/node-red-node-snmp)? Secondly, as you are running async code, you cannot return msg you need to use [node.send](https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously) e.g.... // NPM module exposed as variable, npm\_module const session = snmp.createSession ("192.168.10.250", "public"); const oids = ["1.3.6.1.2.1.1.1.0"]; session.get (oids, function (error, varbinds) { if (error) { node.error(error, msg); } else { for (let i = 0; i \< varbinds.length; i++) { const e = sn…

Then after the function, you can simply use a switch node to route the message based on `msg.topic`

---

<div class="post-metadata">

**Author:** ![jonkers71](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonkers71/32/59714_2.png) [@jonkers71](https://discourse.nodered.org/u/jonkers71)\
**Post date:** [16 April 2022 04:32 UTC](https://discourse.nodered.org/t/msg-from-function-block/61207/4 "2022-04-16T04:32:18Z")

</div>

Yes I started to play with this at home where I have some more freedom and realised I was to blame for it not working and had my switch node wrong.

Is there a way to push a list in order instead of the oid? So for them oid have it make the topic Temperature?

Thanks again for your help.

```auto
var session = snmp.createV3Session ("10.20.155.10", user);

var nastemp = "1.3.6.1.4.1.6574.1.2.0";
var hddtemp1 = "1.3.6.1.4.1.6574.2.1.1.6.0";
var hddtemp2 = "1.3.6.1.4.1.6574.2.1.1.6.1";
var hddtemp3 = "1.3.6.1.4.1.6574.2.1.1.6.2";
var hddtemp4 = "1.3.6.1.4.1.6574.2.1.1.6.3";
var hddtemp5 = "1.3.6.1.4.1.6574.2.1.1.6.4";
var pwrstatus = "1.3.6.1.4.1.6574.1.3.0";

var oids = [nastemp, hddtemp1, hddtemp2, hddtemp3, hddtemp4, hddtemp5, pwrstatus];

session.get (oids, function (error, varbinds) {
    if (error) {
        console.error (error);
    } else {
        for (var i = 0; i < varbinds.length; i++) {
            if (snmp.isVarbindError (varbinds[i])) {
                console.error (snmp.varbindError (varbinds[i]));
            } else {
                msg.payload = varbinds[i].value;
                msg.topic = varbinds[i].oid;
                node.send(msg)
            }
        }
    }
    session.close ();
});

```

---

<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:** [28 April 2022 21:49 UTC](https://discourse.nodered.org/t/msg-from-function-block/61207/5 "2022-04-28T21:49:35Z")

</div>

@jonkers71 fyi

I have updated the node-red-node to v1.0.0 complete with snmpve support. You might want to give that a go?

I'd appreciate the feedback

Thanks.

---

<div class="post-metadata">

**Author:** ![jonkers71](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonkers71/32/59714_2.png) [@jonkers71](https://discourse.nodered.org/u/jonkers71)\
**Post date:** [29 April 2022 00:12 UTC](https://discourse.nodered.org/t/msg-from-function-block/61207/6 "2022-04-29T00:12:30Z")

</div>

@Steve-Mcl

Have just done a little testing with it. Seems to work very well.

I need to learn more so I can help with this stuff.

---

<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:** [28 June 2022 00:13 UTC](https://discourse.nodered.org/t/msg-from-function-block/61207/7 "2022-06-28T00:13:01Z")

</div>

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