Combining time and payload information

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;

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?

Add a function node directly after BR Target Temp.

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

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.

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?

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

try this instead

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

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

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

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

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!

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

3 Likes

Hmm :wink:

OK, next challenge.


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?

https://cookbook.nodered.org/basic/join-streams

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.

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

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

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

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.