# Combining time and payload information

**URL:** <https://discourse.nodered.org/t/combining-time-and-payload-information/94531>\
**Category:** General\
**Created:** [11 January 2025 13:56 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531 "2025-01-11T13:56:09Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)\
**Post date:** [11 January 2025 13:56 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/1 "2025-01-11T13:56:09Z")

</div>

Based on the work of @notenoughtech, I've finally managed to get a battery powered Tuya TRV talking to Node-Red. There were a some breaking changes in his example flows which I can pass on to anyone who is interested.

I'm now stuck with a purely NR challenge;

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

Whenever target temperature is changed within NR the green 'BR Target Temp' node will fire with the new value as msg.payload. At that moment, I need to send it _ **AND** _ msg.time to 'Hash the Body Data' node. I can't work out how to do this.

Also, I need the the payload of 'Sign SignStr with Secret' to appear at the 'function' node at the same time as the payload from BR Target Temp - because BR Target Temp payload is used again and sent to 'http request' as a "code:value" pair. Again, I can't work out how to do this.

Any advice how to do these 2 things please?

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [11 January 2025 14:22 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/2 "2025-01-11T14:22:13Z")

</div>

Add a function node directly after BR Target Temp.

```auto
const now = new Date(); // make a date object (default timestamp is now)
msg.timestamp = now.getTime(); // add it to the incoming message

```

---

<div class="post-metadata">

**Author:** ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)\
**Post date:** [13 January 2025 13:36 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/3 "2025-01-13T13:36:26Z")

</div>

Thanks @ThingsTinkerer. I've used your suggestion (`var now = new Date();` works better than `const now = new Date();`). That's addressed that issue.

I'm now trying to combine 2 inputs to give me a msg.payload and a msg.header.

```auto
var bright = msg.bright;
msg.header = '{"commands":[{"code":"bright_value_v2","value":bright}]}';

```

In my head, the code above should take the incoming value of msg.bright, assign it to the var _bright_ and drop the value into the formula, but it doesn't. It outputs the work _bright_ instead of the value.

What am I doing wrong?

---

<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:** [13 January 2025 14:10 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/4 "2025-01-13T14:10:50Z")

</div>

> [@SomersetSmartHome](#):
>
> `msg.header = '{"commands":[{"code":"bright_value_v2","value":bright}]}'`

since the whole thing is encapsulated with single quotes, it is a string exactly as you wrote it.

try this instead

```auto
msg.header = {"commands":[{"code":"bright_value_v2","value":bright}]}

```

and if you REALLY need a string (not an object) then use stringify

```auto
msg.header = {"commands":[{"code":"bright_value_v2","value":bright}]}
msg.header = JSON.stringify(msg.header)

```

though as you attempting to set a header for the HTTP Request, I suspect is should probably be like this...

```auto
msg.header = {
    "commands": JSON.stringify([{"code":"bright_value_v2","value":bright}])
}

```

---

<div class="post-metadata">

**Author:** ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)\
**Post date:** [13 January 2025 14:32 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/5 "2025-01-13T14:32:15Z")

</div>

Thanks @Steve-Mcl that is SO helpful.

I don't think I'll ever get my head around scripting - 30 years of structured 'C' programming is holding me back and means I have to rely on my extended brain, which is people like you!

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [13 January 2025 15:07 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/6 "2025-01-13T15:07:07Z")

</div>

Why use `var` instead of `const`/`let`? Even football fans don't like it 😃

 ![images](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f9f03f6890738679806c1fa430aa591f96cfa7eb.png)

---

<div class="post-metadata">

**Author:** ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)\
**Post date:** [13 January 2025 17:24 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/7 "2025-01-13T17:24:32Z")

</div>

Hmm 😉

OK, next challenge.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4d40c149984c7c5103e6a406fbd6e94c5c32cb4.png)  
I now have msg.time, msg.command and msg.payload arriving at the _function_ node, but because they come from 2 different nodes, the data arrives at different times, firing _function_ twice, neither with all of the information required.

How can I combine information from 2 nodes and have it arrive and be processed at the same time?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [13 January 2025 17:54 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/8 "2025-01-13T17:54:55Z")

</div>

[https://cookbook.nodered.org/basic/join-streams](https://cookbook.nodered.org/basic/join-streams)

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [13 January 2025 18:57 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/9 "2025-01-13T18:57:32Z")

</div>

That's gonna be more complicated. There are 2 options:

1. context (store values in memory)
2. join node

Context is manual so you need to control the logic yourself. I suggest using node context in the function node where the messages are to be merged. With join node, you can achieve this grouping automatically, but you need to add a parts object to all the incoming messages.

---

<div class="post-metadata">

**Author:** ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)\
**Post date:** [14 January 2025 08:00 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/10 "2025-01-14T08:00:04Z")

</div>

Thanks @ThingsTinkerer. Which way would you do it?  
It's difficult to see why Tuya feel the need to encrypt twice; 256 bit encryption is a challenge once, so encrypting my radiator valve setting twice seems a bit OTT!  
Presumably someone, somewhere has implemented a Tuya Cloud flow using NR, so a solution should already exist....

---

<div class="post-metadata">

**Author:** ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)\
**Post date:** [14 January 2025 14:56 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/11 "2025-01-14T14:56:53Z")

</div>

Think I'm nearly there.

The only thing (I think!) I need to do now is to convert msg.payload from an object to a string in order for the message to look like it does in the 'hard coded' version of my code where the value is injected.

.toString() doesn't seem to do it.....?

---

<div class="post-metadata">

**Author:** ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)\
**Post date:** [14 January 2025 16:49 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/12 "2025-01-14T16:49:55Z")

</div>

OK, sorted and working. Used the JSON node for the object to string conversion.

Thanks for all your help; I definitely would not have got there without it

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [14 January 2025 19:59 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/13 "2025-01-14T19:59:11Z")

</div>

In Javascript, it's as easy as doing `JSON.stringify(msg.payload);`. For combining 2 messages, I think I would try with join node (automatic), adding parts object to each message first.

---

<div class="post-metadata">

**Author:** ![notenoughtech](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/notenoughtech/32/40995_2.png) [@notenoughtech](https://discourse.nodered.org/u/notenoughtech)\
**Post date:** [16 January 2025 22:15 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/14 "2025-01-16T22:15:20Z")

</div>

Sorry, I wasn't able to check in this despite the tag as I was very ill last week. I'm glad you had great help with this

---

<div class="post-metadata">

**Author:** ![SomersetSmartHome](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@SomersetSmartHome](https://discourse.nodered.org/u/SomersetSmartHome)\
**Post date:** [16 January 2025 22:47 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/15 "2025-01-16T22:47:26Z")

</div>

No worries @notenoughtech your input was a big help.  
I intend to post a full description of my solution and hope other folk will find it helpful (or can advise me how to do better!)  
Hope you're fully recovered now.

---

<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:** [30 January 2025 22:47 UTC](https://discourse.nodered.org/t/combining-time-and-payload-information/94531/16 "2025-01-30T22:47:35Z")

</div>

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