Pass S7 variable to Mysql query

Hi there, I've go through different posts here, but wasn't able to find a proper solution.
It's my first go with Node-Red and my objective is the following:
I have sensors variables from S7 PLC from which I have to get value to populate a Mysql db.
I've mounted the Mysql db node with no problems and start testing the first "Insert into" queries.

Actually I'm stuck in this situation:
I have a PLC S7 In node that is creating a msg.payload named "metri" that is a number (as I can see from the debug console).
I've created a function node with the following:

var metri=msg.payload.metri;
msg.topic = "INSERT INTO test_iot (metri) VALUE ('" + metri + "')";
return msg;

From the debug console, I see this message:
INSERT INTO test_iot (metri) VALUE ('undefined').

What I'm missing?

Thanks in advance for your help

Welcome to the forum @Eimerich.

That error probably means that msg.payload.metri does not exist. Connect a debug node showing what is going into the function node and seewhat it shows.
First though, if you are a beginner with node-red then 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.
Also read through the node-red doc Working with Messages which should also help, and in particular will show you how to get the path to an item in the debug pane (the path is how to address it, msg.payload.metri for example).

Then if you are still stuck come back again and show us what is going into the function.

Hi Colin,
thanks a lot for your reply.
I'll surely have a look into the suggested Essentials.
In the meantime this is the debug from the S7 Node

23/2/2021, 15:04:51node: 3f5809af.a2d146
metri : msg.payload : number

I'm passing the wrong value to the function?
In the meantime I'm going to "EssentialsMe"...


Hi found out the error @Colin !
You were right! My msg.payload.metri wasn't there.
I wrongly assumed that the "variable" name you give to a node was to be consider as its "name", when it wasn't.
Now I'm properly writing on the MySQL db the meters.
Time to a new adventure!

1 Like

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