# What is the best way to preserve data after a change node

**URL:** <https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267>\
**Category:** General\
**Created:** [8 November 2022 15:53 UTC](https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267 "2022-11-08T15:53:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [8 November 2022 15:53 UTC](https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267/1 "2022-11-08T15:53:04Z")

</div>

I have a message coming into a change node. The message consists of  
payload.status "22.4 C" and payload.value "22"  
the change node sets payload to payload.status  
after the change node, I loose the value. I am trying to use the value in a switch value to split my flow into \<21 and \>=21  
But after the switch node all I see in the debug node is payload "22.4 C", nothing else

 ![NR](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2ab754ef152052994bffe32e379e330502aeb5e4.png)

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [8 November 2022 16:01 UTC](https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267/2 "2022-11-08T16:01:37Z")

</div>

Probably better if I explain what I am trying to do.  
Temp hot water is sending payload.status "22.4 C" and payload.value "22"  
For the dashboard, I need "22.4 C"  
But I want to split it into \<21 and \>22.4, so I can assign different colours (with the 2 set colours change nodes)

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [8 November 2022 16:06 UTC](https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267/3 "2022-11-08T16:06:22Z")

</div>

I have found a way which works, but that is not very elegant ? It has identical set payload, it just splits before the set payload

 ![NR](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fdab53c7dcd84dcb0f334be45a29bfc038255603.png)

---

<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:** [8 November 2022 16:18 UTC](https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267/4 "2022-11-08T16:18:37Z")

</div>

or set the payload after the set colours - ie bring it back together then ....  
but as the ui widget is just text can you not just specify msg.payload.status in the ui node ?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 November 2022 16:24 UTC](https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267/5 "2022-11-08T16:24:06Z")

</div>

> [@Mikee1234K](#):
>
> Temp hot water is sending payload.status "22.4 C" and payload.value "22"  
> For the dashboard, I need "22.4 C"  
> But I want to split it into \<21 and \>22.4, so I can assign different colours (with the 2 set colours change nodes)

Since "22.4 C" is a string, you will have to get rid of " C" and convert the value to a floating point number so that you can have different processing according to the value.  
Since you need "22.4 C" for your dashboard, probably best to use a new element msg.payload.numericStatus to store the converted value.

> [@Mikee1234K](#):
>
> But after the switch node all I see in the debug node is payload "22.4 C", nothing else

If your switch node sets msg.payload to "22.4 C" then it's surely no surprise if it subsequently contains "22.4 C"? You can always put msg.payload.value in a new element msg.value to protect it from your actions.  
msg.value is not the same as msg.payload.value.

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [8 November 2022 16:29 UTC](https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267/6 "2022-11-08T16:29:28Z")

</div>

> [@dceejay](#):
>
> but as the ui widget is just text can you not just specify msg.payload.status in the ui node ?

Yes, that worked ! Makes sense.

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [8 November 2022 16:34 UTC](https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267/7 "2022-11-08T16:34:57Z")

</div>

> [@jbudd](#):
>
> You can always put msg.payload.value in a new element msg.value to protect it from your actions

I thought of that, and tried but it did not work. I probably made a mistake when I tried.  
I set payload to payload.status, then in the same node value to payload.value  
But that did not work, the result was only payload showed in the debug

This was the result in debug  
 ![NR](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1ef7458addc8f0e8f45d18669edbc5dcc64afc89.png)  
 ![NR2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/b/5b885e49e0aa1f505fdcfcc6ba78632d239c1d02.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 November 2022 16:42 UTC](https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267/8 "2022-11-08T16:42:45Z")

</div>

The rules in a change node are executed in order. Your first rule changes msg.payload to the value of msg.payload.status. So when you get to the second rule msg.payload.value does not exist as the first rule overwrites msg.payload. Try setting msg.value first then set msg.payload.

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [8 November 2022 16:49 UTC](https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267/9 "2022-11-08T16:49:15Z")

</div>

Yes that was the problem.  
I was trying to convert 22.4 C back to a number with a funcion. I did not get that to work. The is a $number or number function, but when trying to deploy there was always a error. As I managed to solve my problem I dont need to do that any more but would be interesting of how to that for future reference

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 November 2022 17:05 UTC](https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267/10 "2022-11-08T17:05:55Z")

</div>

Number(var) would not work.  
you would do  
`msg.payload.status = parseFloat(msg.payload.status);` to get a float or `parseInt(var)` if you want an integer, in a function.

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [8 November 2022 17:24 UTC](https://discourse.nodered.org/t/what-is-the-best-way-to-preserve-data-after-a-change-node/70267/11 "2022-11-08T17:24:04Z")

</div>

> [@jbudd](#):
>
> If your switch node sets msg.payload to "22.4 C" then it's surely no surprise if it subsequently contains "22.4 C"? You can always put msg.payload.value in a new element msg.value to protect it from your actions.  
> msg.value is not the same as msg.payload.value.

This is usually my go to method for this kind of thing too. It comes in very handy if you're using nodes that can only operate on msg.payload like the split node or some of the community encryption nodes out there.
