I am using the node s7 to read some data from a PLC, https://www.npmjs.com/package/node-red-contrib-s7
I am reading and getting the data correctly.
In the node configuration exist a checkbox to get data only if a value has changed, that is, if one value changed, I get an actualization of the complete list of variables I am reading.
The thing is that I would like to write on mysql only the data that has changed.... I dont have idea how to do it being efficiently...
I have made a test making a function for each variable and works ok... But is the right way??? I could do it, but what will happen if I am reading 400 variables? 400 functions? hahah...
Test I did for some variables...(for each PLC Variable...)
You will need to retain the previous value. Then when you get a new value, walk through each property on the payload and compare with the same property from the saved value. If different, write to a new object and then output that object.
In JavaScript, you can walk through all the properties of an object with something like:
Being honest, I dont have idea how can I retain each previous value and then construct a query with the only the data has changed... could you give me some advice?
You can use a context variable for retention. context.get and context.set - you will find the details in the official docs.
You can use a filter function if you want or can do things manually with a simple comparison. Filter is probably slightly more efficient but I doubt you would notice unless really pushing a lot of traffic.
context variables are memory based and will be lost if Node-RED restarts. You can, however, set up a file-based persistent variable store in your settings.js file should you wish to.
While certainly do-able - depending on the total number of variables you are potentially writing I can't help feeling that trying to figure out just the changed fields and constructing a suitable insert/update for just those is going to be more complex and less efficient than just re-writing the entire row - and indeed if they are timeseries type data is MySQl even the right sort of database for that sort of data?