logo elektroda
logo elektroda
X
logo elektroda

[Solved] OpenBeken and NodeRed: Write code for function node to create OpenBeken Config Payload

erhardfalkenberg 951 7
ADVERTISEMENT
Treść została przetłumaczona german » english Zobacz oryginalną wersję tematu
  • #1 20817439
    erhardfalkenberg
    Level 4  
    Write code for a nodered function node to split the openbeken config payload into individual messages.
  • ADVERTISEMENT
  • #2 20822550
    xury
    Automation specialist
    Post a sample payload, then maybe I can help.
  • ADVERTISEMENT
  • #3 20823579
    erhardfalkenberg
    Level 4  

    That would be very nice:
    Example:
    {
      "dev": {
        "ids": [
          "Pow_187"
        ],
        "name": "Pow_187",
        "sw": "1.17.293",
        "mf": "Beken Corporation",
        "mdl": "BK7231N",
        "cu": "http://192.168.178.187/index"
      },
      "name": "voltage",
      "~": "pow_187",
      "avty_t": "~/connected",
      "uniq_id": "Pow_187_sensor_0",
      "qos": 1,
      "dev_cla": "voltage",
      "unit_of_meas": "V",
      "stat_t": "~/voltage/get",
      "stat_cla": "measurement"
    }

    @ElektrodaBot
  • ADVERTISEMENT
  • #4 20823583
    ElektrodaBot
    AI OpenAI ChatGPT4
    erhardfalkenberg wrote:
    (...) ation",
    "mdl": "BK7231N",
    "cu": "http://192.168.178.187/index"
    },
    "name": "voltage",
    "~": "pow_187",
    "avty_t":"~/connected",
    "uniq_id": "pow_187_sensor_0",
    "qos": 1,
    "dev_cla": "voltage",
    "unit_of_meas": "V",
    "stat_t":"~/voltage/get",
    "stat_cla": "measurement"
    }
    Hello,

    it seems like you posted a JSON configuration example for a smart device used in the Internet of Things (IoT). This can be part of a Home Assistant configuration or a similar smart home platform. The JSON object defines a device with various attributes such as device ID, software version, manufacturer, model and control URL. In addition, a sensor for the voltage is defined, including topics for MQTT such as availability topic, state topic and the unit of measurement.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.
  • #5 20823590
    erhardfalkenberg
    Level 4  

    Can nodered decompose this payload into a function and output the topics?
  • ADVERTISEMENT
  • #6 20824613
    erhardfalkenberg
    Level 4  
    That would be very nice:
    Example:
    {
      "dev": {
        "ids": [
          "Pow_187"
        ],
        "name": "Pow_187",
        "sw": "1.17.293",
        "mf": "Beken Corporation",
        "mdl": "BK7231N",
        "cu": "http://192.168.178.187/index"
      },
      "name": "voltage",
      "~": "pow_187",
      "avty_t": "~/connected",
      "uniq_id": "Pow_187_sensor_0",
      "qos": 1,
      "dev_cla": "voltage",
      "unit_of_meas": "V",
      "stat_t": "~/voltage/get",
      "stat_cla": "measurement"
    }
    @ElektrodaBot

    Added after 2 [minutes]:

    // Get the input JSON payload from the message
    var inputPayload = msg.payload;
    
    // Check if the payload is an array
    if (Array.isArray(inputPayload)) {
        // If it's an array, split it into individual messages
        for (var i = 0; i < inputPayload.length; i++) {
            // Create a new message for each element in the array
            var newMsg = {
                payload: inputPayload[i]
            };
    
            // Send the new message to the appropriate output
            if (i < node.outputs) {
                // Send to the corresponding output if available
                node.send([newMsg, null]);
            } else {
                // If there are more elements than outputs, send the remaining elements to the last output
                node.send([null, newMsg]);
            }
        }
    } else {
        // If the payload is not an array, just pass it through
        node.send([msg, null]);
    }
    
    return null; // Return null to end the function node[/i]
  • #7 20824620
    erhardfalkenberg
    Level 4  

    See code above: done by chatgpt 3.5 as chrome extension.
    that is a handy way obviously.

    Moderated By Felini:

    3.1.13. Publication of posts that violate the spelling rules of the German language.


  • #8 20824621
    ElektrodaBot
    AI OpenAI ChatGPT4
    erhardfalkenberg wrote:
    (...) If there are more elements than outputs, send the remaining elements to the last output
    node.send([null, newMsg]);
    }
    }
    } else {
    // If the payload is not an array, just pass it through
    node.send([msg, null]);
    }

    return null; // Return null to end the function node
    It looks like you just shared a JavaScript code for Node-RED that processes an incoming message. The code checks if the incoming payload is an array. If so, each element of the array is split into a separate message and forwarded to the appropriate outputs. If there are more array elements than outputs, the additional messages are all sent to the last output. If the incoming payload is not an array, the message is passed through unchanged.

    Note, however, that the code in your message contains some errors. In particular, in the lines where you call `node.send([newMsg, null]);` and `node.send([null, newMsg]);`, it looks like you want to send the messages to different outputs, but the code does not do this correctly.

    Here is a corrected version of the code that dynamically creates message objects and sends them to the appropriate output after verifying that the payload is an array:

    Code: Javascript
    Log in, to see the code


    This code assumes that your Node-RED node has multiple outputs (`node.outputs`). Each element of the array will be sent to the corresponding output, and if there are more elements than outputs, only as many elements will be sent as there are outputs available.

    Please make sure this code works correctly in your Node-RED environment, as environments may be configured differently. If you need further assistance, let me know!
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.

Topic summary

The discussion revolves around creating a Node-RED function node to decompose an OpenBeken configuration payload into individual messages. A sample JSON payload is provided, detailing a smart device's attributes, including device ID, software version, manufacturer, model, and MQTT topics. Participants inquire about the feasibility of using Node-RED to split this payload and output the relevant topics. A JavaScript code snippet is shared, which processes the incoming payload, checks if it is an array, and splits it into separate messages. Corrections to the initial code are suggested to ensure proper message handling.
Summary generated by the language model.
ADVERTISEMENT