# How select and send to BBDD only the changed values

**URL:** <https://discourse.nodered.org/t/how-select-and-send-to-bbdd-only-the-changed-values/33147>\
**Category:** Industrial\
**Created:** [21 September 2020 13:43 UTC](https://discourse.nodered.org/t/how-select-and-send-to-bbdd-only-the-changed-values/33147 "2020-09-21T13:43:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jotarod](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jotarod](https://discourse.nodered.org/u/Jotarod)\
**Post date:** [21 September 2020 13:43 UTC](https://discourse.nodered.org/t/how-select-and-send-to-bbdd-only-the-changed-values/33147/1 "2020-09-21T13:43:04Z")

</div>

Dear all,

I am using the node s7 to read some data from a PLC, [https://www.npmjs.com/package/node-red-contrib-s7](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.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/3584e32d0b22ce6980907d9ac8bc25c9861c3f0c.png)

Data I am getting from PLC:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0c0c47a7faecb760c4d7f5d5d983779f4da0f8b7.png)

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...)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1d7861297cc4720a11888f74fc1d9508b1b98c93.png)

Let me know your opinions, please!!!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 September 2020 16:37 UTC](https://discourse.nodered.org/t/how-select-and-send-to-bbdd-only-the-changed-values/33147/2 "2020-09-21T16:37:02Z")

</div>

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:

```javascript
const out = {} // create output object

Object.keys(msg.payload).forEach( keyName => {
    // ...
})
```

---

<div class="post-metadata">

**Author:** ![Jotarod](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jotarod](https://discourse.nodered.org/u/Jotarod)\
**Post date:** [22 September 2020 07:06 UTC](https://discourse.nodered.org/t/how-select-and-send-to-bbdd-only-the-changed-values/33147/3 "2020-09-22T07:06:18Z")

</div>

Many Thanks @TotallyInformation !!!

I have been making some test like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6d069d3c87488e43a76cd8b0197822cf43dbeff7.png)

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?

Thanks again!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [22 September 2020 09:23 UTC](https://discourse.nodered.org/t/how-select-and-send-to-bbdd-only-the-changed-values/33147/4 "2020-09-22T09:23:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 September 2020 09:38 UTC](https://discourse.nodered.org/t/how-select-and-send-to-bbdd-only-the-changed-values/33147/5 "2020-09-22T09:38:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![machadotiago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/machadotiago/32/23776_2.png) [@machadotiago](https://discourse.nodered.org/u/machadotiago)\
**Post date:** [10 November 2020 02:00 UTC](https://discourse.nodered.org/t/how-select-and-send-to-bbdd-only-the-changed-values/33147/6 "2020-11-10T02:00:10Z")

</div>

You don´t need any function for that. Just use the "All variables, one per message" mode of S7 node and add the RBE node right after that.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 January 2021 02:00 UTC](https://discourse.nodered.org/t/how-select-and-send-to-bbdd-only-the-changed-values/33147/7 "2021-01-09T02:00:16Z")

</div>

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