# Change node not working as expected!

**URL:** <https://discourse.nodered.org/t/change-node-not-working-as-expected/54011>\
**Category:** General\
**Created:** [19 November 2021 20:14 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011 "2021-11-19T20:14:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [19 November 2021 20:14 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/1 "2021-11-19T20:14:35Z")

</div>

It's been a long day, so this could be something stupid, so I'll apologise first!

I have an output from a `node-red-contrib-google-smarthome` node;

```auto
object
topic: ""
device_name: "driveway alert"
command: "OnOff"
params: object
payload: object
online: true
on: true
_msgid: "656fb6dc47400779"

```

...which I'm feeding into a change node which sets the `msg.payload` to the value of `msg.payload.on`  
I expected that the payload from the change node would be `true`, but there is no output from the node, nothing...

Here's a quick video

![changenode](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/6/56607b09a51ad28e1e704810a77903e90f5c0888.gif)

---

<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:** [19 November 2021 20:26 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/2 "2021-11-19T20:26:45Z")

</div>

If it’s already an object you can’t just set it to a new type, use move instead as that does a delete/replace

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [19 November 2021 20:31 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/3 "2021-11-19T20:31:20Z")

</div>

> [@dceejay](#):
>
> already an object you can’t just set it to a new type, use move instead as that does a delete/replace

Hmm, not sure about that. My instinct is I would expect this to work.

There certainly are issues going the other way... Setting sub-properties of a non object type. But replacing payload should be okay.

Would need to try it out

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [19 November 2021 20:32 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/4 "2021-11-19T20:32:39Z")

</div>

Move does not work either

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [19 November 2021 20:34 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/5 "2021-11-19T20:34:18Z")

</div>

I might be tired as well but I see no msg.payload.on

I see msg.on

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [19 November 2021 20:36 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/6 "2021-11-19T20:36:45Z")

</div>

> [@cymplecy](#):
>
> I might be tired as well but I see no msg.payload.on

There's some indentation missing in the pasted message output that makes them all appear to be at the top level. The screenshot shows it is msg.payload.on

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [19 November 2021 20:38 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/7 "2021-11-19T20:38:56Z")

</div>

Here is the full output from the contrib node;

```auto
{
   "topic":"",
   "device_name":"driveway alert",
   "command":"OnOff",
   "params":{
      "on":true
   },
   "payload":{
      "online":true,
      "on":true
   },
   "_msgid":"6fd9bbe90bfd4906"
}

```

I'm getting `payload.on` by right clicking on `on: true` in debug and selecting/copying it's path.

![copi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0c411c0bd5e008cdbbd18d0ddd2ee34fa9df62da.gif)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [19 November 2021 21:12 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/8 "2021-11-19T21:12:37Z")

</div>

Strange thing is, if I copy the whole output from the contrib node, then paste it into a function node like this

 ![works](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/3814a53aeceb217f60a9d4cb63fa168e02217201.jpeg)

...it works OK, and I get the expected output - `true`???

```auto
[{"id":"9566b6e6bd3614e2","type":"function","z":"f8cf7ea5344d6287","name":"","func":"msg = { \"topic\": \"\", \"device_name\": \"driveway alert\", \"command\": \"OnOff\", \"params\": { \"on\": true }, \"payload\": { \"online\": true, \"on\": true }, \"_msgid\": \"6fd9bbe90bfd4906\" }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":535,"y":1225,"wires":[["685a8731f1ec3bad"]]},{"id":"ff97bb624a7b3820","type":"inject","z":"f8cf7ea5344d6287","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":390,"y":1225,"wires":[["9566b6e6bd3614e2"]]},{"id":"43cbd11c92216a6b","type":"debug","z":"f8cf7ea5344d6287","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":885,"y":1225,"wires":[]},{"id":"685a8731f1ec3bad","type":"change","z":"f8cf7ea5344d6287","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.on","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":1225,"wires":[["43cbd11c92216a6b"]]}]

```

What is the difference?

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [20 November 2021 01:13 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/9 "2021-11-20T01:13:28Z")

</div>

what software do you use to get this video ?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [20 November 2021 10:00 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/10 "2021-11-20T10:00:24Z")

</div>

> [@smanjunath211](#):
>
> what software do you use

I'm using [LICEcap](https://licecap.en.softonic.com/) for Windows, but let's keep on topic please.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 November 2021 10:10 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/11 "2021-11-20T10:10:17Z")

</div>

What version of NR, node.js and what platform?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [20 November 2021 10:14 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/12 "2021-11-20T10:14:55Z")

</div>

I'm using;

- node-RED v2.1.3
- node.js v14.18.1
- OS Raspbian (Linux 5.10.63-v7+ arm LE)
- Raspberry Pi 3b+

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [20 November 2021 10:34 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/13 "2021-11-20T10:34:34Z")

</div>

If you use a function node with  
`node.warn(JSON.stringify(msg))`  
right after your google-home node  
do you notice any weird characters or extra spaces in the property names ?

[EDIT] here is a modified version of your test flow that shows what i mean

```auto
[{"id":"9566b6e6bd3614e2","type":"function","z":"5847b7aa62131d37","name":"","func":"msg = { \"topic\": \"\", \"device_name\": \"driveway alert\", \" command \": \" OnOff \", \"params\": { \"on\": true }, \"payload\": { \"online\": true, \"on\": true }, \"_msgid\": \"6fd9bbe90bfd4906\" }\n\nnode.warn(JSON.stringify(msg))\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":1920,"wires":[["b070be71099eec5e","50bfcbdd103cbeb2"]]},{"id":"ff97bb624a7b3820","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":380,"y":1920,"wires":[["9566b6e6bd3614e2"]]},{"id":"b070be71099eec5e","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":1880,"wires":[]},{"id":"50bfcbdd103cbeb2","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"command","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":1940,"wires":[]}]

```

i modified the "command" property to have extra spaces  
in the debug window those are not rendered and the msg property value is actually accesible only with extra spaces `msg[" command "]`  
its a long shot but if its the case it would show with `stringify`

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 November 2021 10:41 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/14 "2021-11-20T10:41:00Z")

</div>

Paul, What do you see if you set the second debug to display the complete msg.object?  
and what happens if you check the 'deep copy' option in the change node?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [20 November 2021 11:07 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/15 "2021-11-20T11:07:02Z")

</div>

> [@UnborN](#):
>
> do you notice any weird characters or extra spaces in the property names ?

This is what I get using the output from the contrib node and your function node (edited to use the incoming msg)

```auto
node.warn(JSON.stringify(msg))
return msg;

```

![test](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fc98a44de3c480b7f03769cc6081da60f4f9b7c4.jpeg)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [20 November 2021 11:16 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/16 "2021-11-20T11:16:12Z")

</div>

the msg looks clean ... dont know 🙄

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [20 November 2021 11:30 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/17 "2021-11-20T11:30:45Z")

</div>

AAaah!! I've just deleted the entire flow, recreated and for some unknown reason it's now working!

As you can see from the video above, it was not working last night, and I spent an hour this morning where it still did not work.  
Up until the point where I recreated the flow, I had not updated or changed anything, either in my flows or the system.

I can offer no explanation... but thank for trying to help.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [20 November 2021 23:01 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/18 "2021-11-20T23:01:47Z")

</div>

There must have been something weird in the original message because I tried it in an inject node (which was happy that it was valid JSON) and, like you, got nothing out of the switch node. I even tried using the `params` version of "on". (I also tried the deep copy) and nothing worked. It would be interesting to try a copy of the output you are getting now.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [20 November 2021 23:39 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/19 "2021-11-20T23:39:38Z")

</div>

> [@Buckskin](#):
>
> and, like you, got nothing out of the switch node

At last, a believer!!!  
Although it seems functional, it's still not working as expected, although I've been reluctant to post here...  
Take a look at the attached gif, it shows no output from the contrib node, yet the change node successfully changes the global context.

There are no other references to this elsewhere in my flows.

![still here](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/ab2a30bb7bf91ff8ac5cc1f75b7e4edb151c553a.gif)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [21 November 2021 00:35 UTC](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011/20 "2021-11-21T00:35:12Z")

</div>

Paul, if you have a MQTT broker available, try this. Send the output of the 'Switch Garden Alert' to a `mqtt-out` node (with an appropriate topic) and disconnect it from the `change` node. Add a `mqtt-in` node (subscribing to the topic) and connect it to the `change` node and see what happens.

Just grasping to see if it's something with the msg the 'Switch Garden Alert' node is generating.

[Next page](https://discourse.nodered.org/t/change-node-not-working-as-expected/54011.md?page=2)
