mySQL INSERT INTO with multiple msg.xxx failing

Been working on this for several days and have exhausted my abilities and various suggestions via the web and these forums...so thank you in advance for any assistance!

Basis:

  1. Have 3 MQTT inputs/message payloads
  2. In node red, converting payload to number(s)
  3. Since all messages had same designation of msg.payload, using a change node/move rule to make unique transformations of 3 msg.payload (s) to (msg.Temp, msg.Humidity, msg.Pressure)
  4. Have PHP MySQL running on Pi, Node red connection tested successfully with simple insert statements where the insert statement contains static values, verifying successful DB insert

Issue/Error
The "INSERT INTO" function is:

var newMsg = {
topic : "INSERT INTO exterior_climate (Temp, Pressure, Humidity, Light, Voltage) VALUES ('"+msg.Temp+"','"+msg.Pressure+"','"+msg.Humidity+"',30,20)"
};
return newMsg

However no records are being inserted into the DB, shown below is the output from the debug window:

All inserts into the DB fail. I am confused that the debug is outputting 3 messages staggered that in sum do show the proper msg.XXX in their correct location (BTW: by design the last 2 values are static for the moment), but that the insert does not appear to be fully populated with the 3 values, then transferred to the insert.

Hi. I think this might help. Can't be certain as you haven't posted your flow or a screenshot...

See this article in the cookbook for an example of how to join messages into one object.

Just to add further clarification, in case you didn't realise, the problem is that your function assumes that each message contains all three values whereas, as you can see, each message has only one of them defined. The solution is to use a Join node as suggested. You won't need to move the values into individual properties, the Join node will do that for you.

Thank you both! I had tried the Join earlier, but missed the "Send the message after xxx message parts" so obviously the join was not properly configured. With this properly configured I am able to get the proper values into the insert statement as shown:

nr3

However, the insert statement is not actually inserting any records into the DB, everything looks OK to me. Can anyone see an issue with my Insert statement?

var newMsg = {
topic : "INSERT INTO exterior_climate (Temp, Pressure,Humidity, Light, Voltage) VALUES ("+msg.payload.Outside_Temp+","+msg.payload.Outside_Pressure+","+msg.payload.Outside_Humidity+",30,20)"
}
return newMsg;

As previously stated, I was able to do inserts with hand entered numbers prior.

Are there any messages coming out of the sql node? Does a Catch node catch any errors from the sql node?

Catch node does not capture any errors. DB is connected in NR.

I changed the insert function to this, but still no success in inserting a new record:

msg.topic="INSERT INTO exterior_climate (Temp,Pressure,Humidity,Light,Voltage) VALUES ("+msg.payload.Outside_Temp+","+msg.payload.Outside_Pressure+","+msg.payload.Outside_Humidity+",30,20)";
msg.payload=[msg.payload.Temp,msg.payload.Pressure,msg.payload.Humidity,msg.payload.Light,msg.payload.Voltage];
return msg;

Don't show us what is in the function, what matters is what is going into the sql node. Use the little Copy Value button next to the value in the debug window.

You didn't answer the question about what is coming out of the sql node.

Also are you using node-red-node-mysql (look in Manage Palette to check). Have you installed any other sql nodes?

Thank you for your patience !

Here is what is coming out of the debug node that is wired after the insert statement:

[null,null,null,null,null]

nr4

Yes I am using the Node Red MY SQL Node, properly configured and connected (as shown by the green dot). During previous troubleshooting using static values in the insert command, I was able to successfully insert records into the DB.

I Found and fixed some typos in the insert which now reads:

msg.topic="INSERT INTO exterior_climate (Temp,Pressure,Humidity,Light,Voltage) VALUES ("+msg.payload.Outside_Temp+","+msg.payload.Outside_Pressure+","+msg.payload.Outside_Humidity+",30,20)";
msg.payload=[msg.payload.Outside_Temp,msg.payload.Outside_Pressure,msg.payload.Outside_Humidity,20,30];
return msg;

Now to output from the debug node shows:

So now the values are in, but still no record being inserted into the DB

Show us the debug node output from your test insert of static data. But before showing us, does it look the same as your actual data?

All, again...many thanks. I continued to fiddle with the insert statement and even though I am uncertain which of the many things I tried did work. The data is now inserting to SQL. I hope to get more proficient with NR so that at some time I may be of assistance to others. Kind Regards

Moving to the next step of selecting data from the MYSQL DB and pushing X records out to a chart. I have learned a lot and copied a lot of examples with marginal success, but have exhausted my abilities...so once again asking for assistance.

My goal is to take the outputs of the select statement which contain multiple fields/data points which use this within a template node:

SELECT Timestamp,Temp,Pressure,Humidity,Light FROM exterior_climate ORDER BY TIMESTAMP DESC LIMIT 20

Which is wired to the MYSQL node. The output is:


With of course 20 objects from the select statement.

From there I have tried dozens of different functions, formats, and set nodes to prepare this message for output to a chart but with no real success. My goal is (perhaps lofty) to dynamically set the data series from the SQL table names as moving ahead I may add fields to the database and would like Node Red to accommodate these new fields without re-coding. To-date I have only been able to get the following results:

Using the following code within a function node:


let r = msg.payload;
let series = [r[0]];
let data = [r.map( v => ({
"x": v.Timestamp,
"y": v.Temp
}))];

msg.payload = [{"series": series, "data": data}];
return msg;


Obviously only currently getting the first series of Temp data, and unable to parse the field name out of the first object. Thank you in advance for any guidance!

This thread has veered off from "mySQL INSERT INTO with multiple msg.xxx failing" to "how to send mySQL data to a chart"

It should really be a new thread (to attract the correct people)!

Anyhow - the below thread is almost identical - it should give you a head start.

1 Like

Thank you so very much Steve! I will take your suggestion and move to a new thread. The solution you directed me to provides valuable progress, and I will continue to work towards dynamic series identification (if indeed that is at all possible).

Kind Regards

New topic created at Dynamic series identification from message, send data to Chart

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