Search code examples
jsonsnmpnode-red

Use data from dropdownlist to set value in SNMP-set on Node-red


In Node-red I would like to use e.g. the values from a dropdown list to send to an SNMP-set command. As I understand the snmp-set node uses msg.varbinds to get its data. But how do I refer to the value coming from the dropdown list in the msg.varbinds input, as it looks JSON to me

This is what I have in the Varbinds window. I thought I just had to refer to the msg.payload coming from the dropdown element, but that seems to be too easy.

[ {
            "oid" : "1.3.6.1.4.1.5835.3.1.3.1.38.1.1",
            "type" : "OctetString",
            "value" : msg.payload
        }
    ]

This is by the way the output from my dropdown list in the debug window.

9/23/2019, 8:13:04 PMnode: b4c9ef70.0f38d
msg : Object
object
payload: "14300000000"
socketid: "Xc-CPsZX-CouQVRAAAAA"
_msgid: "b77a7c3f.8645c"

Here is the flow

[{"id":"fd51033f.82b34","type":"snmp set","z":"e5486a7.f6cf798","host":"192.168.0.35","community":"public","version":"1","varbinds":"[ {\n            \"oid\" : \"1.3.6.1.4.1.5835.3.1.3.1.38.1.1\",\n            \"type\" : \"OctetString\",\n            \"value\" : msg.payload\n        }\n    ]","timeout":5,"name":"set freq ","x":620,"y":1940,"wires":[]},{"id":"596c4ff0.67b7b","type":"ui_dropdown","z":"e5486a7.f6cf798","name":"","label":"","tooltip":"","place":"Select option","group":"ca614dce.5eeca","order":11,"width":0,"height":0,"passthru":true,"options":[{"label":"14200","value":"14200000000","type":"str"},{"label":"14300","value":"14300000000","type":"str"},{"label":"14400","value":"14400000000","type":"str"}],"payload":"","topic":"","x":340,"y":1940,"wires":[["fd51033f.82b34","b4c9ef70.0f38d"]]},{"id":"b4c9ef70.0f38d","type":"debug","z":"e5486a7.f6cf798","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":690,"y":2060,"wires":[]},{"id":"ca614dce.5eeca","type":"ui_group","z":"","name":"Modulator 5","tab":"fe640bc5.c66b48","order":4,"disp":true,"width":"6","collapse":false},{"id":"fe640bc5.c66b48","type":"ui_tab","z":"","name":"Modulators","icon":"rss_feed","order":5,"disabled":false,"hidden":false}]

Solution

  • OK, so the info sidebar help for the SNMP set node has the bit you are missing:

    msg.varbinds may contain varbinds as an array of json objects containing multiple oids, types and values.

    This means you need to build the varbinds string and store in the msg.varbinds key of the message object and then pass it to the SNMP set node. The easiest way to do this is probably with a function node between the UI dropdown node and the SNMP set node.

    msg.varbinds = "[ {\n" +
            "\"oid\" : \"1.3.6.1.4.1.5835.3.1.3.1.38.1.1\",\n" +
            "\"type\" : \"OctetString\",\n" +
            "\"value\" : \"" + msg.payload + "\"\n"
        "}\n" + 
    "]";
    
    return msg;
    

    You should leave the varbinds empty in the SNMP set node config.