• I have my project working fine from an engineer's perspective but I need to tidy it up a bit.

    When my Puck.js detects a button press, the Espruino-Hub/Node-Red servers send an SMS using Twilio. In Node-Red it finds the msg-payload value which is the cumulative number of button presses since power cycle.

    The problem I am having is my lack of understanding of how to work in Node-Red with messages.

    In many programming languages I wand something like

    printf("Alert, the button was pressed. This is the %d press since power cycle." msg.payload)
    

    Does anyone know how to do this in Node-Red?

    Thanks

  • I'd really like to combine two values, but it seems like each topic is triggered separately.

    It would be cleaner from the SMS reader's point of view for the message to be:

    printf("Alert, the button was pressed. This is press %d since powered up.  Battery level is %d.",  msg1.payload,  msg2.payload)
    

    but it isn't clear to me how to do this graphically in Node-Red.

  • Well, in the first instance, I'd drag in a 'function' block and add the following code inside it:

    msg.payload = `Alert, the button was pressed. This is the ${msg.payload} press since power cycle.`;
    return msg;
    

    And for the second option, you can use flow.get/set:

    Add a function block to the battery message

    flow.set('battery', msg.payload);
    return msg;
    

    Add a function block to the click message:

    msg.payload = `Alert, the button was pressed. This is the ${msg.payload} press since power cycle. Battery level is ${flow.get("battery")}`;
    return msg;
    

    Hope that helps!

  • Awesome! Now I know how to format message payloads. Thank you @Gordon , this works perfectly as you suggested. The method for referring to Message-A from Message-B is going to be useful in the future.

    I also used this formatting function to not only format my lack of motion alerts, but also limit them to waking hours only.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Only slighly off topic... Node-Red - how to build a text string that referenced a msg.payload

Posted by Avatar for HeneryH_(Henery_Hawk) @HeneryH_(Henery_Hawk)

Actions