MySQL chain of queries without loosing data

40 views
Skip to first unread message

Alexander Kirchner

unread,
Feb 18, 2018, 1:49:02 AM2/18/18
to Node-RED
Hello,
How can i make a chain of decisions in node red without loosing the original information from the incomming MQTT data?
I want to write a parser, witch translate an incoming mqtt-data and write the data to a sql database if data is allowed.
First i splitt the mqtt-data into pieces.
Then i look into the database if one piece already exist to make the decisio if i can write the rest to the database.

But how can i make this with node-red?
After the SQL-Node i lost the data from the original MQTT-Data.
Is this possible?

Thank you
Alex.
database_mqtt_chain.jpg

Mark Setrem

unread,
Feb 18, 2018, 3:05:22 AM2/18/18
to Node-RED
as you found the nodes tend to place the interesting output in msg.payload over writing what was there.

If you want to retain the original msg.payload the trick is to move this to something else in the msg object e.g. msg.mqtt
This should then be retained through the flow.

You should be able to do this copy with a change node or a function node

Garry Hayne

unread,
Feb 18, 2018, 5:41:07 AM2/18/18
to Node-RED
Mark' suggestion is the easiest way:

Garry

Alexander Kirchner

unread,
Feb 18, 2018, 6:07:06 AM2/18/18
to Node-RED
Hi Guys THANK YOU. i ave to try this. I'm not the big coder, so i need a bit time.
Message has been deleted

Alexander Kirchner

unread,
Feb 18, 2018, 7:24:35 AM2/18/18
to Node-RED

The Picture again.

Garry Hayne

unread,
Feb 18, 2018, 7:39:20 AM2/18/18
to Node-RED
Alexander, can you show us the contents of the function node (SQL query), I suspect that you are not returning the msg object there.

Garry

On Sunday, February 18, 2018 at 12:24:35 PM UTC, Alexander Kirchner wrote:

The Picture again.

Alexander Kirchner

unread,
Feb 18, 2018, 12:02:28 PM2/18/18
to Node-RED
Sorry, but my Post with the code was deleted. (by google?)

var separator = msg.topic.split("/");
var temp0 = String(separator[0]);
var temp1 = String(separator[1]);
var temp2 = String(separator[2]);
var COT = {payload:temp0};
var CID = {payload:temp1};
var DIR = {payload:temp2};

var sqlstring = {
     topic: "SELECT connectors.id FROM connectors WHERE connector_uuid='"+temp1+"'"
 };
 
return sqlstring;


Thank you
Alex.

Colin Law

unread,
Feb 18, 2018, 12:14:27 PM2/18/18
to node...@googlegroups.com
Instead of the last 5 lines do

msg.mqtt = msg.payload;
msg.topic = "SELECT connectors.id FROM connectors WHERE connector_uuid='"+temp1+"'"
return msg;

Then the original payload will still be available downstream in msg.mqtt, if that is what you are trying to achieve.

Colin


 


Thank you
Alex.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/fb5bb1f9-8025-462f-8e3f-95bf0b5501d1%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Alexander Kirchner

unread,
Feb 18, 2018, 1:04:04 PM2/18/18
to Node-RED
Hello Colin Law!
you really saved my Weekend! That easy and that logic.

Thank you very much!
Alex.

Zenofmud

unread,
Feb 20, 2018, 9:42:32 AM2/20/18
to node...@googlegroups.com
In your function node you are returning a new message (sqlstring) and not the original msg so of course the new msg will not have msg.mqtt set.

> On Feb 18, 2018, at 7:21 AM, Alexander Kirchner <ma...@alexanderkirchner.at> wrote:
>
> <payload_mqtt_chnge.jpg>

Zenofmud

unread,
Feb 20, 2018, 9:45:44 AM2/20/18
to node...@googlegroups.com
Try changing that function to:
--------------------------
var separator = msg.topic.split("/");
var temp0 = String(separator[0]);
var temp1 = String(separator[1]);
var temp2 = String(separator[2]);
var COT = {payload:temp0};
var CID = {payload:temp1};
var DIR = {payload:temp2};

msg.topic = "SELECT connectors.id FROM connectors WHERE connector_uuid='"+temp1+"'";

return msg;
--------------------------
Reply all
Reply to author
Forward
0 new messages