# What is wrong with my change node (or split node)?

**URL:** <https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188>\
**Category:** General\
**Created:** [29 August 2020 16:29 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188 "2020-08-29T16:29:43Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [29 August 2020 16:29 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/1 "2020-08-29T16:29:43Z")

</div>

I am trying to strip off some text from the date in the output from a influxDB node, to use in a dashboard chart node. But it does not work. The date is not altered by the change node.

When I try to create the same message with an injection node, it seems to work.

So I belive there is a difference here, that I don't get...

This is my flow:

```auto
[{"id":"ee91f817.afad68","type":"influxdb in","z":"a42ecbde.f79ce8","influxdb":"3712e327.ec6fac","name":"running","query":"SELECT MEAN(value) FROM running GROUP BY time(1d) LIMIT 1","rawOutput":false,"precision":"","retentionPolicy":"","x":300,"y":380,"wires":[["8403e3b7.bdd9f","ddc5ea89.c7edd8"]]},{"id":"57ecc154.fa056","type":"debug","z":"a42ecbde.f79ce8","name":"unchanged","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":260,"wires":[]},{"id":"3820987c.e490d8","type":"inject","z":"a42ecbde.f79ce8","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"0","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":380,"wires":[["ee91f817.afad68"]]},{"id":"ddc5ea89.c7edd8","type":"split","z":"a42ecbde.f79ce8","name":"","splt":"","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":470,"y":380,"wires":[["fe92ea38.f48e78","59465a9e.76b4c4","57ecc154.fa056"]]},{"id":"5788d0f6.72237","type":"inject","z":"a42ecbde.f79ce8","name":"","topic":"","payload":"{\"time\":\"2020-08-24T00:00:00.000Z\",\"mean\":0.2}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":320,"wires":[["59465a9e.76b4c4","57ecc154.fa056"]]},{"id":"59465a9e.76b4c4","type":"change","z":"a42ecbde.f79ce8","name":"change","rules":[{"t":"delete","p":"parts","pt":"msg"},{"t":"change","p":"payload.time","pt":"msg","from":"[0-9][0-9][0-9][0-9]-","fromt":"re","to":"","tot":"str"},{"t":"change","p":"payload.time","pt":"msg","from":"T.*","fromt":"re","to":"","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"payload.time","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload.mean","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":320,"wires":[["3d8fb850.db7408"]]},{"id":"3d8fb850.db7408","type":"debug","z":"a42ecbde.f79ce8","name":"changed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":860,"y":320,"wires":[]},{"id":"3712e327.ec6fac","type":"influxdb","z":"","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"varmepumpe","name":"","usetls":false,"tls":""}]

```

Example of message coming out of the split node:

```auto
{"topic":"","payload":{"time":"2020-08-24T00:00:00.000Z","mean":0.05555555555555555},"parts":{"id":"9b7d829d.49cad","type":"array","count":1,"len":1,"index":0},"_msgid":"949f4826.20a628"}

```

Can someone offer an explanation?

---

<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:** [29 August 2020 17:21 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/2 "2020-08-29T17:21:29Z")

</div>

Try this as the change node

```auto
[{"id":"7a6cf034.3b505","type":"change","z":"ca69b5fc.7f36e","name":"change","rules":[{"t":"delete","p":"parts","pt":"msg"},{"t":"change","p":"payload.time","pt":"msg","from":"[0-9][0-9][0-9][0-9]-","fromt":"re","to":"","tot":"str"},{"t":"change","p":"payload.time","pt":"msg","from":"T.*","fromt":"re","to":"","tot":"str"},{"t":"move","p":"payload.time","pt":"msg","to":"topic","tot":"msg"},{"t":"move","p":"payload.mean","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":200,"wires":[["a24ae32d.721538"]]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 August 2020 17:23 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/3 "2020-08-29T17:23:29Z")

</div>

> [@harelabb](#):
>
> I am trying to strip off some text from the date in the output from a influxDB node, to use in a dashboard chart node. But it does not work. The date is not altered by the change node

Without seeing the debug output or the db schema, my guess is the output from influx is a date object or an Epoch (I.e. not a string like your inject data)?

---

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [29 August 2020 18:03 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/4 "2020-08-29T18:03:39Z")

</div>

Thanks.  
@zenofmud, with your change node, same thing, the time string is unchanged.  
@Steve-Mcl, I did post the the message going into the change node (the output from the db after split). payload.time is clearly text.  
The payload coming from the inject node is cut&paste from the payload of this. I cannot see any difference in the two messages, except the order of message\_id, topic, payload.  
The message from the inject nodes is changed as I would expect.

---

<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:** [29 August 2020 18:07 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/5 "2020-08-29T18:07:47Z")

</div>

> [@harelabb](#):
>
> with your change node, same thing, the time string is unchanged.

Really?? This is what I get running the `inject` to the `change`  
 ![Screen Shot 2020-08-29 at 2.05.38 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/db7c7921c2921a9a4cee96d05cc2712ec11ecde7.png)

Can you show what you are getting pressing th `inject` that goes right to the change node.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 August 2020 18:37 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/6 "2020-08-29T18:37:40Z")

</div>

> [@harelabb](#):
>
> payload.time is clearly text

You can't rely on the debug output. It does it's best to show you a value (i.e. converting objects/dates to strings)

I'm not saying I'm correct on this matter but it's always best to check.

Send the payload through a function node to be certain...

```auto
node.warn(typeof msg.payload.time);
return msg;

```

---

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [29 August 2020 19:04 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/7 "2020-08-29T19:04:27Z")

</div>

> [@Steve-Mcl](#):
>
> You can't rely on the debug output.

Yes function node says object.  
I believe that cannot be changed in the change node.  
(json node) -\> (change node) -\> (json node) fixed it.  
(but it looks ugly)  
Tanks!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 August 2020 19:47 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/8 "2020-08-29T19:47:53Z")

</div>

What do you see if you put in a function node

```auto
msg.payload = msg.payload.time.getTime()
return msg

```

and feed it into debug node.

---

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [29 August 2020 20:22 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/9 "2020-08-29T20:22:40Z")

</div>

> [@Colin](#):
>
> What do you see

payload: 1598227200000

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 August 2020 20:35 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/10 "2020-08-29T20:35:43Z")

</div>

> [@harelabb](#):
>
> payload: 1598227200000

That means that payload.time is actually a js Date object, which is what it should be. You can use the js Date methods to get what you want, or node-red-contrib-moment or node-red-contrib-simpletime or I believe that the jsonata in the change node has built in date manipulation now, but I have never used that.

---

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [29 August 2020 20:38 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/11 "2020-08-29T20:38:26Z")

</div>

Brilliant, @Colin!  
Replaced the change node with a function node using JS time functions:

```auto
msg1 = {}
d = msg.payload.time.getDate()
m = msg.payload.time.getMonth()+1
msg1.topic = d + "." + m
msg1.payload = msg.payload.mean
return msg1;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 August 2020 20:50 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/12 "2020-08-29T20:50:57Z")

</div>

Just to note that you don't need to make a new message. It is generally best to avoid that if possible so that any other attributes are passed through, plus avoiding the slight overheads of creating a new object. Also it is good practice to use `let` when declaring variables, or even `const` in this case as you are not changing them.

```auto
const d = msg.payload.time.getDate()
const m = msg.payload.time.getMonth()+1
msg.topic = d + "." + m
msg.payload = msg.payload.mean
return msg;

```

---

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [29 August 2020 21:25 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/13 "2020-08-29T21:25:35Z")

</div>

Thanks, @Colin.  
I sure have a way to go with JavaScript

---

<div class="post-metadata">

**Author:** ![harelabb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/harelabb/32/27450_2.png) [@harelabb](https://discourse.nodered.org/u/harelabb)\
**Post date:** [29 August 2020 21:32 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/14 "2020-08-29T21:32:23Z")

</div>

Is there any reason to delete the unused stuff, e.g. the parts? I guess it will be lost anyway at the next node which is a chart.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 August 2020 08:07 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/15 "2020-08-30T08:07:31Z")

</div>

> [@harelabb](#):
>
> Is there any reason to delete the unused stuff,

Very rarely it is necessary to delete attributes in case they upset subsequent nodes. A much more common pattern is to add attributes at one point in the flow and then pick them up later on down the flow, relying on the fact that intermediate nodes should not remove them. So it is good practice to get into the habit of re-using the existing message. In addition it generally saves a line or two of code.

---

<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:** [13 September 2020 08:07 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-change-node-or-split-node/32188/16 "2020-09-13T08:07:32Z")

</div>

Closed on 13 September 2020 08:07 UTC

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