How to combine two messages

I have a message with an array.

image

Using
payload["000104391731"].state.remainingTime[0]
and
payload["000104391731"].state.remainingTime[1]

I can create two seperate messages.

However I want to combine these values and create the following message 2h 45min

John

You can do, in a function node

let timeString = `${payload["000104391731"].state.remainingTime[0]}h ${payload["000104391731"].state.remainingTime[1]}min`

or you can do the same thing using jsonata in a change node.

There seems to be a bug in JSONata/Change node.
I can set the inject to payload["000104391731"].state.remainingTime[0] = 2
but JSONata or change node can not see it, but it visable in debug.


example change JSONata flow

[{"id":"8b40e32d.d25c28","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload[\"000104391731\"].state.remainingTime[1]","v":"45","vt":"num"},{"p":"topic","vt":"str"},{"p":"payload[\"000104391731\"].state.remainingTime[0]","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":1880,"wires":[["15adae6d.934902"]]},{"id":"15adae6d.934902","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"hold0","pt":"msg","to":"payload[\"000104391731\"].state.remainingTime[0]","tot":"msg"},{"t":"set","p":"hold1","pt":"msg","to":"payload[\"000104391731\"].state.remainingTime[1]","tot":"msg"},{"t":"set","p":"timestrin","pt":"msg","to":"$string(hold0)  & 'h '  & $string(hold1)  & 'min'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":1940,"wires":[["5e1bd60c.64727"]]},{"id":"5e1bd60c.64727","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":1960,"wires":[]}]

Set your debug node to Show Full Message and see what it shows

My mistake , while investigating.
But the issue still remains in JSONata but not change node

[{"id":"d8f3269b.a43d9","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload[\"000104391731\"].state.remainingTime[1]","v":"45","vt":"num"},{"p":"topic","vt":"str"},{"p":"payload[\"000104391731\"].state.remainingTime[0]","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":2120,"wires":[["6f97de47.86a5b"]]},{"id":"6f97de47.86a5b","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"timestrin","pt":"msg","to":"$string(payload[\"000104391731\"].state.remainingTime[0])  & 'h '  & $string(payload[\"000104391731\"].state.remainingTime[1])  & 'min'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":230,"y":2180,"wires":[["e8309938.fb1628"]]},{"id":"e8309938.fb1628","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":500,"y":2200,"wires":[]}]

Did you realise the first one worked?

Yes, but you would have to move the payloads to get to the values.
JSONata does not seem to like the path.

edit/ seems to be the 0 at start of object key.

Oh, I see the problem I think. JSONata syntax for properties with odd characters is different, can't remember what it is at the moment.

[Edit] Take out the square brackets and use dots instead (keep the quotes)

Yes the ["000104391731"] seems to be the issue,
JSONata does not like the [ ] for objects, or object keys starting with numbers.
Edit, That's it , was driving me potty, Cheers

Here is the JSONata example

[{"id":"e8309938.fb1628","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":2120,"wires":[]},{"id":"d8f3269b.a43d9","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload[\"000104391731\"].state.remainingTime[1]","v":"45","vt":"num"},{"p":"topic","vt":"str"},{"p":"payload[\"000104391731\"].state.remainingTime[0]","v":"2","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":2120,"wires":[["6f97de47.86a5b"]]},{"id":"6f97de47.86a5b","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"timestrin","pt":"msg","to":"   $string(payload.\"000104391731\".state.remainingTime[0])  \t   & 'h '  & $string(\t   payload.\"000104391731\".state.remainingTime[1]\t)  & 'min'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":2120,"wires":[["e8309938.fb1628"]]}]

I did search the online documentation, but could not see where this is mentioned, do you have a link?

I found it here https://docs.jsonata.org/simple

I read that, but fail to see where it says use " " around path properties with odd characters.

Thanks a lot.

This did the trick. In first instance I was using the JSONNata but I did not manage to get it working.


John

The section I saw was
Field references containing whitespace or reserved tokens can be enclosed in backticks

JSONata      Other.`Over 18 ?`

But I now realise that says backticks, so it will need an expert to pronounce on how this is supposed to work in node-red.

Ok i see it now , cheers. It seems that " ` or ' will all work.

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