# How replace two nodes of "changes" with one node of "function"?

**URL:** <https://discourse.nodered.org/t/how-replace-two-nodes-of-changes-with-one-node-of-function/35039>\
**Category:** General\
**Created:** [29 October 2020 19:56 UTC](https://discourse.nodered.org/t/how-replace-two-nodes-of-changes-with-one-node-of-function/35039 "2020-10-29T19:56:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zerg](https://avatars.discourse-cdn.com/v4/letter/z/6de8d8/32.png) [@Zerg](https://discourse.nodered.org/u/Zerg)\
**Post date:** [29 October 2020 19:56 UTC](https://discourse.nodered.org/t/how-replace-two-nodes-of-changes-with-one-node-of-function/35039/1 "2020-10-29T19:56:54Z")

</div>

I want to replace two nodes of "changes" with one node of "function". But, I do something wrong.  
Could you help me, please!  
Idea is enable / disable the dashboard switch by "function" node.

my code of "function" node.

```auto
if (msg.payload === "On") { 
    msg.payload = {'enabled':"true"};
    return [msg];
} 
else if (msg.payload === "Off"){   
       msg.payload = {'enabled':"false"};
       return [msg];
}
else {                          
       return [null];
}

```

my flow

```auto
[{"id":"c22901ee.cddb8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"4c18cc5b.2438a4","type":"ui_switch","z":"c22901ee.cddb8","name":"","label":"switch","tooltip":"","group":"298a6214.c57fde","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":550,"y":300,"wires":[[]]},{"id":"b2723bce.8e2718","type":"change","z":"c22901ee.cddb8","name":"Enable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":200,"wires":[[]]},{"id":"4dc147c7.dfb608","type":"change","z":"c22901ee.cddb8","name":"Disable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":260,"wires":[[]]},{"id":"90d39172.e8eef","type":"inject","z":"c22901ee.cddb8","name":"Enable","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"On","payloadType":"str","x":150,"y":280,"wires":[["b2723bce.8e2718","a781deac.dd63d"]]},{"id":"dea5ceb5.fced3","type":"inject","z":"c22901ee.cddb8","name":"Disable","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Off","payloadType":"str","x":150,"y":340,"wires":[["4dc147c7.dfb608","a781deac.dd63d"]]},{"id":"533e2cb3.14e894","type":"debug","z":"c22901ee.cddb8","name":"222222","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":520,"y":400,"wires":[]},{"id":"a781deac.dd63d","type":"function","z":"c22901ee.cddb8","name":"","func":"\n\nif (msg.payload === \"On\") { \n msg.payload = {'enabled':\"true\"};\n return [msg];\n} \nelse if (msg.payload === \"Off\"){ \n msg.payload = {'enabled':\"false\"};\n return [msg];\n}\nelse { \n return [null];\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":360,"wires":[["533e2cb3.14e894","4c18cc5b.2438a4"]]},{"id":"298a6214.c57fde","type":"ui_group","name":"A1","tab":"cf795f11.f1c7b","order":1,"disp":true,"width":"6","collapse":false},{"id":"cf795f11.f1c7b","type":"ui_tab","name":"TEST","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/eff2c9d5af2e7b509cd58153ac4545053ed11873.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:** [29 October 2020 20:11 UTC](https://discourse.nodered.org/t/how-replace-two-nodes-of-changes-with-one-node-of-function/35039/2 "2020-10-29T20:11:16Z")

</div>

You are senting msg.payload.enabled to `"true"` .  
You want to set msg.enabled to `true` .  
`msg.enabled = true;`.

you can also do it in one change node e.g.

```auto
[{"id":"1823c52e.f89b4b","type":"debug","z":"e412bf07.04765","name":"222222","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":240,"wires":[]},{"id":"d36f3af0.99b8d","type":"change","z":"e412bf07.04765","name":"Enable","rules":[{"t":"set","p":"enabled","pt":"msg","to":"payload = \"On\" ? true : (payload = \"Off\" ? false : null)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":200,"wires":[["1823c52e.f89b4b","156f6518.6c213b"]]},{"id":"156f6518.6c213b","type":"ui_switch","z":"e412bf07.04765","name":"","label":"switch","tooltip":"","group":"ee77c8fe.154dc","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":550,"y":300,"wires":[[]]},{"id":"acf054a3.aef528","type":"inject","z":"e412bf07.04765","name":"Enable","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"On","payloadType":"str","x":150,"y":280,"wires":[["d36f3af0.99b8d"]]},{"id":"18167e30.256102","type":"inject","z":"e412bf07.04765","name":"Disable","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Off","payloadType":"str","x":160,"y":360,"wires":[["d36f3af0.99b8d"]]},{"id":"ee77c8fe.154dc","type":"ui_group","name":"A1","tab":"86d61473.cb198","order":1,"disp":true,"width":"6","collapse":false},{"id":"86d61473.cb198","type":"ui_tab","name":"TEST","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![Zerg](https://avatars.discourse-cdn.com/v4/letter/z/6de8d8/32.png) [@Zerg](https://discourse.nodered.org/u/Zerg)\
**Post date:** [29 October 2020 20:39 UTC](https://discourse.nodered.org/t/how-replace-two-nodes-of-changes-with-one-node-of-function/35039/3 "2020-10-29T20:39:01Z")

</div>

> [@E1cid](#):
>
> You are senting msg.payload.enabled to `"true"` .  
> You want to set msg.enabled to `true` .  
> `msg.enabled = true;` .

I didn't get it!  
Do you mean that?

```auto

if (msg.payload === "On") { 
    msg.enabled = true;
    return [msg];
} 
else if (msg.payload === "Off"){   
       msg.enabled = false;
       return [msg];
}
else {                          
       return [null];
}

```

That doesn't work too. Or I do again something wrong?

I need to implement this in node of "function", after I want to add more code ...

---

<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:** [29 October 2020 20:42 UTC](https://discourse.nodered.org/t/how-replace-two-nodes-of-changes-with-one-node-of-function/35039/4 "2020-10-29T20:42:22Z")

</div>

> [@Zerg](#):
>
> ```auto
> return [msg];
> 
> ```

`return msg;`

---

<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:** [12 November 2020 20:42 UTC](https://discourse.nodered.org/t/how-replace-two-nodes-of-changes-with-one-node-of-function/35039/5 "2020-11-12T20:42:38Z")

</div>

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