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

**URL:** <https://discourse.nodered.org/t/help-to-reduce-these-two-nodes-please-appending-time-to-payload/48856>\
**Category:** General\
**Created:** [24 July 2021 19:11 UTC](https://discourse.nodered.org/t/help-to-reduce-these-two-nodes-please-appending-time-to-payload/48856 "2021-07-24T19:11:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [24 July 2021 19:11 UTC](https://discourse.nodered.org/t/help-to-reduce-these-two-nodes-please-appending-time-to-payload/48856/1 "2021-07-24T19:11:45Z")

</div>

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.

---

<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:** [24 July 2021 19:17 UTC](https://discourse.nodered.org/t/help-to-reduce-these-two-nodes-please-appending-time-to-payload/48856/2 "2021-07-24T19:17:15Z")

</div>

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

```auto
[{"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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e32e423ecc7a1ad0c164a1fd82a0102b1bf5ff4.jpeg)

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

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [24 July 2021 19:28 UTC](https://discourse.nodered.org/t/help-to-reduce-these-two-nodes-please-appending-time-to-payload/48856/3 "2021-07-24T19:28:22Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/a/aad3dd84360b23f62cfc514811e4918d32e20f40.png)

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

---

<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:** [24 July 2021 19:40 UTC](https://discourse.nodered.org/t/help-to-reduce-these-two-nodes-please-appending-time-to-payload/48856/4 "2021-07-24T19:40:59Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [24 July 2021 19:48 UTC](https://discourse.nodered.org/t/help-to-reduce-these-two-nodes-please-appending-time-to-payload/48856/5 "2021-07-24T19:48:44Z")

</div>

Excellent thank you very much Paul 👍

---

<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:** [7 August 2021 19:48 UTC](https://discourse.nodered.org/t/help-to-reduce-these-two-nodes-please-appending-time-to-payload/48856/6 "2021-08-07T19:48:58Z")

</div>

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