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

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:

[{"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:

{"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?

Try this as the change node

[{"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"]]}]

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

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.

Really?? This is what I get running the inject to the change
Screen Shot 2020-08-29 at 2.05.38 PM

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

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

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

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!

What do you see if you put in a function node

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

and feed it into debug node.

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.

1 Like

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

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

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.

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

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

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.

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.

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