Help to reduce these two nodes please (appending time to payload)

Hi all,

I feel I am getting the syntax wrong when trying to combine these two nodes into one function node.

The first node sets the payload to JSON $moment().tz("Europe/London").format("ddd DD/MM/YYYY HH:mm:ss")

The second function node appends the message:
msg.payload = (msg.payload + " Battery level at 75% "); return msg;

I'd like to reduce clutter and make it work with only one function node.
But this does not work:

msg.payload = (($moment().tz("Europe/London").format("ddd DD/MM/YYYY HH:mm:ss")) + " Battery level at 75% "); return msg;

Flow exported:

[{"id":"476ec4eb.518bdc","type":"function","z":"3b92f612.6f7b5a","name":"Battery level 75%","func":"msg.payload = (msg.payload + \" Battery level at 75% \");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1770,"y":1830,"wires":[["b3a323f1.d0188"]]},{"id":"e3b25975.cd1ec8","type":"change","z":"3b92f612.6f7b5a","name":"Add time","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().tz(\"Europe/London\").format(\"ddd DD/MM/YYYY HH:mm:ss\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1580,"y":1830,"wires":[["476ec4eb.518bdc"]]}]

Any help would be appreciated.

Try this, just using the change node (or do you really want to use a function node??);

[{"id":"e3b25975.cd1ec8","type":"change","z":"b3b413d1.05b1b","name":"Add time","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().tz(\"Europe/London\").format(\"ddd DD/MM/YYYY HH:mm:ss\")","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"payload&\" Battery level at 75% \"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":1295,"wires":[["e531186332827979"]]},{"id":"e531186332827979","type":"debug","z":"b3b413d1.05b1b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":495,"y":1295,"wires":[]},{"id":"d896b492c537fb01","type":"inject","z":"b3b413d1.05b1b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":85,"y":1295,"wires":[["e3b25975.cd1ec8"]]}]

time

I would suggest using the change node, because AFAIK, the moment library is not available in the function node, and you would have to load the library as an external module.

1 Like

Yes that works thanks! I just assumed I would need a function node.

If I wanted to dynamically replace the text with another message e.g. msg.payload.info, would it work if that second "Set" became payload& msg.payload.info ?

I would tend to use a topic to define the text - just personal preference.
so msg.level = "10" and the jsonata would be payload&" Battery level at "&level&"%" would give you "Sat 24/07/2021 20:39:19 Battery level at 10%"

It will also work if the level is a number - msg.level = 10

1 Like

Excellent thank you very much Paul :+1:

1 Like

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