源自於 http://developer.opto22.com/nodered/general/getting-started/node-red-hello-world-2/

Overview

The Hello, world! example is great, but we can make it more useful and interesting. We’ll add the current time to the message, and execute it every two seconds.

Step 1 - Add a Function node

First off, let’s add the current time to the message. To do that, we’ll use the Function block, which allows you to enter JavaScript code to manipulate the msg object.
  1. Select a Function node and drag it out onto the flow.
  2. Carefully place it over the existing wire between the existing Inject and Debug nodes. Node-RED will insert the new node between the two existing nodes, and rewire the nodes together.
Static Image
 Click the image to start and stop the animation.

Step 2 - Add Code and Run

  1. Double-click on the Function node to open the “Edit function node” view.
  2. Copy and paste the following JavaScript code into the Function field:
    var dateNow = new Date();
    var timeAsString = dateNow.toLocaleTimeString();
        
    msg.payload = msg.payload + 
                  ' The time is ' + 
                  timeAsString + '.';
                      
    return msg;
    
  3. In the Name field, enter “Add time to msg”.
  4. Click Done.
  5. Click Deploy.
  6. Click the Inject node’s button.
  7. In the Debug tab, you should see the new output. For example, “Hello, world! The time is 4:25:29 PM”.
Static Image
 Click the image to start and stop the animation.

Step 3 - Add Two-Second Interval

Now let’s adjust the flow to automatically inject a new message every two seconds. The existing Inject node can already do that, we just need to adjust its settings.
  1. Double-click on the existing Inject node to open the “Edit inject node” view.
  2. For the Repeat field, select interval and enter 2 seconds for the period.
  3. Click Done.
  4. Click Deploy.
  5. In the Debug tab, you should now see your output every two seconds.
Static Image
 Click the image to start and stop the animation.