Visualize Data from Database to Chart

I am new to Node-Red, and I am facing a simple problem. Connection of my nodes is explained bellow. There is an inject node which is my matrix: "CREATE TABLE RANDOMNUM( TIMESTAMP INT PRIMARY KEY NOT NULL, VALUE INT NOT NULL, BOOL INT NOT NULL)". This node is connected to a "litedb" node and "litedb" is connected to a debug node. There is another inject node which is connected to the following function.

var randomNum = Math.round(Math.random()*100);
var largeBool = (randomNum > 50?1:0);
var newMsg = {
"topic": "INSERT INTO RANDOMNUM VALUES ( "+msg.payload+","+randomNum+","+largeBool+")"}
return newMsg;

This function is connected as well to the litedb node and what I want to do is to visualize the variable "newMsg" in a chart. I have tried different connections with chart but it none of them works.

Thanks for your time, appreciate your help !

By 'litedb' are you refering to sqlite??
When explaining a issue, it is helpful (and time saving for anyone going to give you some of their time) to provide the name of the exact node you are using like: The flow is using node-red-node-sqlite.

What is the debug node showing when you insert a value? (please change the debug node to show the 'complete msg object')

Excuse me for my wrong formulation, indeed I am refering to sqlite. The debug node shows the values bellow.


INSERT doesn't SELECT data from the database.

Are you wanting to add the incoming value to the chart - or - SELECT data from the database to display many points on the chart in one go?

Display many points on the chart in one go. Thanks for your help!

Then you need to do a SELECT against the database & then you need to format the returned rows of data to match the described format in the built-in help on the chart node.

NOTE: the SELECT should have a suitable WHERE / TOP / LIMIT clause to avoid sending more data to the chart than there are pixels (e.g. if you try to display 10000 points on a 400px chart - not only will it be pointless but it will make your browser crawl)

I was thinking about creating a new function which will have only selected values from the previous function, for example "randomNum" and "largeBool". I want these to values to be visualized in a chart but I don't know how to write the code for this. Its simple but I am confused.

Thanks again for your time.

To add points to a chart at the same time as INSERTing to database, just send a msg with the topic and payload set.

e.g. your "write query" function you could return these values out of a 2nd output wired directly to the chart.

Share your function code.

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.


var randomNum = Math.round(Math.random()*100);
var largeBool = (randomNum > 50?1:0);
var newMsg = {
"topic": "INSERT INTO RANDOMNUM VALUES ( "+msg.payload+","+randomNum+","+largeBool+")"}
return newMsg;

my function code is this, but I am still confused. Thanks for you reply!

I will thank you!

Try this...

Demo function to import... (use CTRL+I)

[{"id":"19bd1cee.2a2653","type":"function","z":"553814a2.1248ec","name":"","func":"var randomNum = Math.round(Math.random() * 100);\nvar largeBool = (randomNum > 50 ? 1 : 0);\nvar newMsg = {\n    \"topic\": \"INSERT INTO RANDOMNUM VALUES ( \" + msg.payload + \",\" + randomNum + \",\" + largeBool + \")\"\n}\n\nvar msg2 = {\n    topic: \"number\",\n    payload: randomNum\n}\nvar msg3 = {\n    topic: \"bool\",\n    payload: largeBool\n}\n\nreturn [newMsg, msg2, msg3];\n\n","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":930,"y":1580,"wires":[["697d7863.8f00a8"],["a5b75a41.540018"],["c167501b.98c19"]]}]

The function code (with comments to help you understand)...

var randomNum = Math.round(Math.random() * 100);
var largeBool = (randomNum > 50 ? 1 : 0);
var newMsg = {
    "topic": "INSERT INTO RANDOMNUM VALUES ( " + msg.payload + "," + randomNum + "," + largeBool + ")"

//create a msg with topic and payload set - for the number
var msg2 = {
    topic: "number",
    payload: randomNum

//create a msg with topic and payload set - for the bool
var msg3 = {
    topic: "bool",
    payload: largeBool

//return the 3 messages (array element 0 goes to output 1, element 1 goes to output 2 etc)
return [newMsg, msg2, msg3];

Thanks for your help! It was that simple, I am new to Node-Red and I guess I need to watch the tutorial above... Appreciate it buddy!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.