Tips

In this page you will find several tips related to Node-RED

1.0 How to format the timestamps

Sometimes you need to use the timestamps in your Node-RED project. You can use the timestamp node to extract the timestamp like below:

Source code
[
    {
        "id": "3c9e9659a9a48e65",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "82ae8f7a87f62c48",
        "type": "inject",
        "z": "3c9e9659a9a48e65",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 140,
        "y": 80,
        "wires": [
            [
                "6e27c4f9b4be2f8f"
            ]
        ]
    },
    {
        "id": "853dbe45d82473a8",
        "type": "debug",
        "z": "3c9e9659a9a48e65",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 780,
        "y": 80,
        "wires": []
    },
    {
        "id": "6e27c4f9b4be2f8f",
        "type": "change",
        "z": "3c9e9659a9a48e65",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$now()",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 360,
        "y": 80,
        "wires": [
            [
                "71738532703c5a2c"
            ]
        ]
    },
    {
        "id": "71738532703c5a2c",
        "type": "function",
        "z": "3c9e9659a9a48e65",
        "name": "Optimizing String",
        "func": "msg.payload = msg.payload.substr(0, 10) + \" \" + msg.payload.substr(12, 7);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 590,
        "y": 80,
        "wires": [
            [
                "853dbe45d82473a8"
            ]
        ]
    }
]

1.1 Bits-wise operation

1.2 Safe mode

Many a time your flow caused your Node-RED to crash. And when you restart your Node-RED, it crashes again and makes it difficult for you to troubleshoot the flow. In this situation, you can use the safe mode of Node-RED where Node-RED will not start right after initializing the server. This allows you to edit the faulty nodes and deploy them again. This will save you hours of troubleshooting.

Just open the command prompt and type node-red --safe

1.3 Template code for the Table in Node-RED

<style>
    #history {
      font-family: "Arial";
        border-collapse: collapse;
        width: 100%;
        }
        
        #history td, #history th {
        border: 1px solid #ddd;
        padding: 8px;
        }
        #history tr:nth-child(even){background-color: #A8EEF8;}
        
        #history tr:hover {background-color: #40aeea;}
        
        #history th {
        padding-top: 12px;
        padding-bottom: 12px;
        text-align: center;
        background-color: #696969;
        color: white;
        }
        </style>
        
        <table id="history" border="1">
            <tr align="center">
                 <th>Timestamp</th>
                 <th>Tank 1 Value in cm</th>
                 <th>Tank 1 Value in %</th>
                 <th>Tank 2 Value in cm</th>
                 <th>Tank 2 Value in %</th>
            </tr>
                <tbody>
                    <tr align="center" ng-repeat="row in msg.payload">
                        <td ng-repeat="item in row" >{{item}}</td>
                        </tr>
                        </tbody>
                        </table>

Last updated