Dynamic MS SQL Query Chart

Hi all,
this is my second topic.
I started with a project a few days ago. Thanks to the help of kind people in this forum I was able to implement some features in the project.
Here you can find the beginning of the story:
First Post

A few learnings later I am facing a new problem where I cant get my head around.
What I have so far:
I write accelerometer values from a modbus-device into an mssql db.
I can parse the table content into a line chart

Using the following flow:

[
    {
        "id": "f5c107a16d194936",
        "type": "inject",
        "z": "4e100d14e53b3afc",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 540,
        "wires": [
            [
                "8c2ee81a9259c669"
            ]
        ]
    },
    {
        "id": "a4e8711562181b92",
        "type": "function",
        "z": "4e100d14e53b3afc",
        "name": "function 3",
        "func": "//var data = [[{\"x\":1537348236000,\"y\":2493},{\"x\":1537348326000,\"y\":2493}]];\n//\nvar series = [\"timestamp\", \"IMUValues\"];\nvar labels = [\"Labels\"];\nvar data = [];\n\nvar i, len; //string; string = \"\";\n\nfor (i = 1, len = msg.payload.length;  i < len; i++) {\n\n    var thetime = Date.parse(msg.payload[i].timestamp);\n    var thevalue = Number(msg.payload[i].IMUValue);\n\n    data.push({x: thetime, y: thevalue});\n\n    //data.push({ \"x\": Date.parse(msg.payload[i].timestamp) + 2 * 60 * 60, \"y\": Number(msg.payload[i].IMUValue) / 100 });\n}\n\ndata = [data];\n\nmsg.payload = [{ series, data, labels }];\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 600,
        "y": 540,
        "wires": [
            [
                "e13660ec95d6c4f1"
            ]
        ]
    },
    {
        "id": "e13660ec95d6c4f1",
        "type": "ui_chart",
        "z": "4e100d14e53b3afc",
        "name": "",
        "group": "54831fc8fbeb93e3",
        "order": 0,
        "width": "6",
        "height": "16",
        "label": "Acceleration Values",
        "chartType": "line",
        "legend": "false",
        "xformat": "HH:mm:ss",
        "interpolate": "bezier",
        "nodata": "",
        "dot": true,
        "ymin": "",
        "ymax": "",
        "removeOlder": 1,
        "removeOlderPoints": "",
        "removeOlderUnit": "3600",
        "cutout": 0,
        "useOneColor": false,
        "useUTC": false,
        "colors": [
            "#1f77b4",
            "#aec7e8",
            "#f77502",
            "#2ca02c",
            "#98df8a",
            "#d62728",
            "#ff9896",
            "#9467bd",
            "#c5b0d5"
        ],
        "outputs": 1,
        "useDifferentColor": false,
        "className": "",
        "x": 830,
        "y": 540,
        "wires": [
            []
        ]
    },
    {
        "id": "8c2ee81a9259c669",
        "type": "MSSQL",
        "z": "4e100d14e53b3afc",
        "mssqlCN": "5558b0cd5f739949",
        "name": "",
        "outField": "payload",
        "returnType": 0,
        "throwErrors": 1,
        "query": "SELECT timestamp, IMUValue \r\nFROM t_test\r\nWHERE meassurementID = 3\r\nORDER BY timestamp \r\n  ",
        "modeOpt": "queryMode",
        "modeOptType": "query",
        "queryOpt": "payload",
        "queryOptType": "editor",
        "paramsOpt": "",
        "paramsOptType": "editor",
        "rows": "rows",
        "rowsType": "msg",
        "params": [
            {
                "output": false,
                "name": "ID",
                "type": "int",
                "valueType": "msg",
                "value": "ID",
                "options": {
                    "nullable": true,
                    "primary": false,
                    "identity": false,
                    "readOnly": false
                }
            }
        ],
        "x": 340,
        "y": 540,
        "wires": [
            [
                "a4e8711562181b92"
            ]
        ]
    },
    {
        "id": "54831fc8fbeb93e3",
        "type": "ui_group",
        "name": "Standard",
        "tab": "48c6c05b20fdbbaf",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "5558b0cd5f739949",
        "type": "MSSQL-CN",
        "tdsVersion": "7_4",
        "name": "",
        "server": "localhost",
        "port": "1433",
        "encyption": true,
        "trustServerCertificate": true,
        "database": "Node_Red_TestDB",
        "useUTC": true,
        "connectTimeout": "15000",
        "requestTimeout": "15000",
        "cancelTimeout": "5000",
        "pool": "5",
        "parseJSON": false,
        "enableArithAbort": true
    },
    {
        "id": "48c6c05b20fdbbaf",
        "type": "ui_tab",
        "name": "Home",
        "icon": "home",
        "disabled": false,
        "hidden": false
    }
]

The query actually pics the readings with a certain ID

SELECT timestamp, IMUValue 
FROM t_test
WHERE meassurementID = 3
ORDER BY timestamp 
  

What I am now trying to do is to make the chart changing dynamically acording to a numeric control:

My approach is to hand over the ID to the mssql node but I dont get any values then, the chart is staying empty.

Debug 7 outout:

{"payload":[],"socketid":"jnCGJBUvAZZ2HWCWAAAR","_msgid":"c5b10456bf18d77b","query":"SELECT timestamp, IMUValue \r\nFROM t_test\r\nWHERE meassurementID = @ID\r\nORDER BY timestamp \r\n  ","queryMode":"query","queryParams":[{"output":false,"name":"ID","type":{},"options":{"nullable":true,"primary":false,"identity":false,"readOnly":false}}],"sqlInfo":[]}

If this is the output of the MSSQL-PLUS, only the query is retuned, no data.

Are you sure the data is in the table?

1 Like

yes an I dont get why.
When I query against the db I get my values as expected

image

What I hand over to the SQL Node is
image

{"payload":2,"socketid":"neJQqvCW37M0sDWTAAAB","_msgid":"4d3e33c4e97630ba"}

After Measurement ID use a change node and
set msg. payload.ID
to msg. payload

I don't use the mssql nodes but the read me seems to suggest that @ID needs to me in msg.payload.ID.

1 Like

I was able to fix it by using the template node. Now the charts are "dynamic".

[
    {
        "id": "e13660ec95d6c4f1",
        "type": "ui_chart",
        "z": "4e100d14e53b3afc",
        "name": "",
        "group": "94e907ca56e3b24e",
        "order": 0,
        "width": "6",
        "height": "16",
        "label": "Acceleration Values",
        "chartType": "line",
        "legend": "false",
        "xformat": "HH:MM:SSSS",
        "interpolate": "bezier",
        "nodata": "",
        "dot": true,
        "ymin": "",
        "ymax": "",
        "removeOlder": 1,
        "removeOlderPoints": "",
        "removeOlderUnit": "3600",
        "cutout": 0,
        "useOneColor": false,
        "useUTC": false,
        "colors": [
            "#1f77b4",
            "#aec7e8",
            "#f77502",
            "#2ca02c",
            "#98df8a",
            "#d62728",
            "#ff9896",
            "#9467bd",
            "#c5b0d5"
        ],
        "outputs": 1,
        "useDifferentColor": false,
        "className": "",
        "x": 970,
        "y": 480,
        "wires": [
            []
        ]
    },
    {
        "id": "d195a6024399b10c",
        "type": "ui_numeric",
        "z": "4e100d14e53b3afc",
        "name": "",
        "label": "Meassurement ID",
        "tooltip": "",
        "group": "94e907ca56e3b24e",
        "order": 1,
        "width": "2",
        "height": "2",
        "wrap": false,
        "passthru": false,
        "topic": "payload",
        "topicType": "msg",
        "format": "{{msg.payload}}",
        "min": "1",
        "max": "999",
        "step": 1,
        "className": "",
        "x": 150,
        "y": 480,
        "wires": [
            [
                "cc99de36f155f64e"
            ]
        ]
    },
    {
        "id": "cc99de36f155f64e",
        "type": "template",
        "z": "4e100d14e53b3afc",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "SELECT timestamp, IMUValue  FROM t_test WHERE meassurementID = {{payload}}\n\n\n\n\n\n\n\n",
        "output": "str",
        "x": 360,
        "y": 480,
        "wires": [
            [
                "67a7fb535c9ce43d"
            ]
        ]
    },
    {
        "id": "67a7fb535c9ce43d",
        "type": "MSSQL",
        "z": "4e100d14e53b3afc",
        "mssqlCN": "5558b0cd5f739949",
        "name": "",
        "outField": "payload",
        "returnType": 0,
        "throwErrors": 1,
        "query": "SELECT timestamp, IMUValue \r\nFROM t_test\r\nWHERE meassurementID = @ID\r\nORDER BY timestamp \r\n  ",
        "modeOpt": "queryMode",
        "modeOptType": "query",
        "queryOpt": "payload",
        "queryOptType": "msg",
        "paramsOpt": "",
        "paramsOptType": "editor",
        "rows": "rows",
        "rowsType": "msg",
        "params": [],
        "x": 560,
        "y": 480,
        "wires": [
            [
                "ea7695f47ed3dfbc"
            ]
        ]
    },
    {
        "id": "ea7695f47ed3dfbc",
        "type": "function",
        "z": "4e100d14e53b3afc",
        "name": "function 6",
        "func": "//var data = [[{\"x\":1537348236000,\"y\":2493},{\"x\":1537348326000,\"y\":2493}]];\n//\nvar series = [\"timestamp\", \"IMUValues\"];\nvar labels = [\"Labels\"];\nvar data = [];\n\nvar i, len; //string; string = \"\";\n\nfor (i = 1, len = msg.payload.length;  i < len; i++) {\n\n    var thetime = Date.parse(msg.payload[i].timestamp);\n    var thevalue = Number(msg.payload[i].IMUValue);\n\n    data.push({x: thetime, y: thevalue});\n\n    //data.push({ \"x\": Date.parse(msg.payload[i].timestamp) + 2 * 60 * 60, \"y\": Number(msg.payload[i].IMUValue) / 100 });\n}\n\ndata = [data];\n\nmsg.payload = [{ series, data, labels }];\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 760,
        "y": 480,
        "wires": [
            [
                "e13660ec95d6c4f1"
            ]
        ]
    },
    {
        "id": "94e907ca56e3b24e",
        "type": "ui_group",
        "name": "Diisplay Meassurments",
        "tab": "72dd0e176fbce39f",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "5558b0cd5f739949",
        "type": "MSSQL-CN",
        "tdsVersion": "7_4",
        "name": "",
        "server": "localhost",
        "port": "1433",
        "encyption": true,
        "trustServerCertificate": true,
        "database": "Node_Red_TestDB",
        "useUTC": true,
        "connectTimeout": "15000",
        "requestTimeout": "15000",
        "cancelTimeout": "5000",
        "pool": "5",
        "parseJSON": false,
        "enableArithAbort": true
    },
    {
        "id": "72dd0e176fbce39f",
        "type": "ui_tab",
        "name": "Create new meassurement",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

And always the same^^ one problem solved the next one is waiting.

When I try to display the Max of a column and pass it to a text node it shows always

[{"":5}]

Query:

image

Debug after mssql node:

{"_msgid":"9512321323912e81","maxID":0,"payload":[{"":5}],"query":"SELECT MAX(meassurementID) FROM t_test\n\n\n\n\n\n\n\n","queryMode":"query","queryParams":[],"sqlInfo":[]}

Settings for the text node:

Is it necessary to use "somehow" a function node to convert the msg.payload?

You query needs to be something like 'SELECT MAX(measurementID) AS xxxTempxxx FROM t_test;'

You can use the change node, dependent on all the changes you want to do, it will handle most of them.

1 Like

when I try so with

SELECT MAX(meassurementID) AS maxID FROM t_imu

I get get my value like expected in a number.

{"_msgid":"b19c61b64b2c6bdd","payload":[{"maxID":3}],"query":"SELECT MAX(meassurementID) AS maxID FROM t_imu\n\n\n\n\n\n\n\n","queryMode":"query","queryParams":[{"output":true,"name":"param1","type":{},"options":{"nullable":true,"primary":false,"identity":false,"readOnly":false},"value":null},{"output":false,"name":"param2","type":{},"value":0,"options":{"nullable":true,"primary":false,"identity":false,"readOnly":false}}],"sqlInfo":[]}

image

But when I try to parse it to the txt node it say NaN.

[
    {
        "id": "0cf99bf3c4da315d",
        "type": "template",
        "z": "dd2a1e9ef74cc87e",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "SELECT MAX(meassurementID) AS maxID FROM t_imu\n\n\n\n\n\n\n\n",
        "output": "str",
        "x": 300,
        "y": 380,
        "wires": [
            [
                "5679ea5108442032"
            ]
        ]
    },
    {
        "id": "5679ea5108442032",
        "type": "MSSQL",
        "z": "dd2a1e9ef74cc87e",
        "mssqlCN": "5558b0cd5f739949",
        "name": "",
        "outField": "payload",
        "returnType": "0",
        "throwErrors": 1,
        "query": "SELECT timestamp, IMUValue \r\nFROM t_test\r\nWHERE meassurementID = @ID\r\nORDER BY timestamp \r\n  ",
        "modeOpt": "queryMode",
        "modeOptType": "query",
        "queryOpt": "payload",
        "queryOptType": "msg",
        "paramsOpt": "",
        "paramsOptType": "editor",
        "rows": "rows",
        "rowsType": "msg",
        "params": [
            {
                "output": true,
                "name": "param1",
                "type": "VarChar",
                "options": {
                    "nullable": true,
                    "primary": false,
                    "identity": false,
                    "readOnly": false
                }
            },
            {
                "output": false,
                "name": "param2",
                "type": "int",
                "valueType": "num",
                "value": "0",
                "options": {
                    "nullable": true,
                    "primary": false,
                    "identity": false,
                    "readOnly": false
                }
            }
        ],
        "x": 500,
        "y": 380,
        "wires": [
            [
                "d8cc003bf3f31218",
                "fd1a20bb186020c2"
            ]
        ]
    },
    {
        "id": "0d8ed7df651928d5",
        "type": "inject",
        "z": "dd2a1e9ef74cc87e",
        "name": "",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 130,
        "y": 380,
        "wires": [
            [
                "0cf99bf3c4da315d"
            ]
        ]
    },
    {
        "id": "d8cc003bf3f31218",
        "type": "debug",
        "z": "dd2a1e9ef74cc87e",
        "name": "debug 16",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 280,
        "wires": []
    },
    {
        "id": "fd1a20bb186020c2",
        "type": "function",
        "z": "dd2a1e9ef74cc87e",
        "name": "function 20",
        "func": "var highestID = parseInt(msg.payload);\nmsg.payload = highestID;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 730,
        "y": 380,
        "wires": [
            [
                "9c744a82c33a946a"
            ]
        ]
    },
    {
        "id": "9c744a82c33a946a",
        "type": "debug",
        "z": "dd2a1e9ef74cc87e",
        "name": "debug 28",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 920,
        "y": 300,
        "wires": []
    },
    {
        "id": "5558b0cd5f739949",
        "type": "MSSQL-CN",
        "tdsVersion": "7_4",
        "name": "",
        "server": "localhost",
        "port": "1433",
        "encyption": true,
        "trustServerCertificate": true,
        "database": "Node_Red_TestDB",
        "useUTC": true,
        "connectTimeout": "15000",
        "requestTimeout": "15000",
        "cancelTimeout": "5000",
        "pool": "5",
        "parseJSON": false,
        "enableArithAbort": true
    }
]

![image|334x281](upload://d0lJ5Ee9gTS0QSim0qpELBHOY6z.png)

You can use a change node to manipulate your JSON.

[{"id":"30b23a960afab3d2","type":"tab","label":"Flow 4","disabled":false,"info":"","env":[]},{"id":"d8cc003bf3f31218","type":"debug","z":"30b23a960afab3d2","name":"debug 16","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":280,"y":160,"wires":[]},{"id":"fd1a20bb186020c2","type":"function","z":"30b23a960afab3d2","name":"function 20","func":"var highestID = parseInt(msg.payload.payload[0].maxID);\nmsg.payload = highestID;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":200,"wires":[["9c744a82c33a946a"]]},{"id":"9c744a82c33a946a","type":"debug","z":"30b23a960afab3d2","name":"debug 28","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":480,"y":220,"wires":[]},{"id":"cfb5cb574bf52b07","type":"inject","z":"30b23a960afab3d2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"_msgid\":\"b19c61b64b2c6bdd\",\"payload\":[{\"maxID\":3}],\"query\":\"SELECT MAX(meassurementID) AS maxID FROM t_imu\\n\\n\\n\\n\\n\\n\\n\\n\",\"queryMode\":\"query\",\"queryParams\":[{\"output\":true,\"name\":\"param1\",\"type\":{},\"options\":{\"nullable\":true,\"primary\":false,\"identity\":false,\"readOnly\":false},\"value\":null},{\"output\":false,\"name\":\"param2\",\"type\":{},\"value\":0,\"options\":{\"nullable\":true,\"primary\":false,\"identity\":false,\"readOnly\":false}}],\"sqlInfo\":[]}","payloadType":"json","x":130,"y":220,"wires":[["fd1a20bb186020c2","d8cc003bf3f31218","4f305fec71c25d95"]]},{"id":"4f305fec71c25d95","type":"change","z":"30b23a960afab3d2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.payload[0].maxID","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":240,"wires":[["9c744a82c33a946a"]]}]
1 Like

aaah. THX! I tried it a 100 times in a similar way, but missed to declare "[0]".
I use your solution now and it works flawless.

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