How get and route a single pair of key/value from an object?

Hi. I'm moving my first steps in node-red, and I'm facing a lot of problems since the template flow that I need to use for my needs contains a lot of stuff that I still don't understand.
My first need is to create some objects that contains each one the values of the object "dps" that I have retrieved within a payload:

{"devId":"228780332cf432062e35","dps":{"1":false,"2":15,"3":24,"7":false,"10":"0","11":0,"101":"0","102":false}}

I've checked the path of the keys, for example payload.dps["1"] but how could I obtain a pair of key/value to send via mqtt?
Thank you

I am sorry, I don't understand exactly what you need to send to MQTT. Can you give us an example of exactly what you want to send?

You can create an object with key/value like this in a function node...

msg.payload = {
  key: "value"
}

so if you want the value of dps in a key/val you would...

msg.payload = {
  dps: msg.payload.dps["1"]
}

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

https://nodered.org/docs/user-guide/messages

Of course @Colin: I need to control locally a tuya appliance that I can't integrate in home Assistant with the LocalTuya custom component.
With the node-red-contrib-tuya-local node I can control that appliance, and my main idea is to use MQTT as a bridge between Node-Red and HA, and to create a climate device based on MQTT.
The node's payload {"devId":"228780332cf432062e35","dps":{"1":false,"2":15,"3":24,"7":false,"10":"0","11":0,"101":"0","102":false}} showed me the dps keys/values that I should send to MQTT, for example the payload.dps["7"] routed to something like "/tuya/bimar_local/swing/" (that is a nice example, since I can't control that function with HA), and so on.

Use a change node

image

image

NOTE: topics should never start and end with a / slash.

Thank you, that worked like a charm!
Looking around I've found the way to convert also the value of my key:

msg.payload = {
  power: msg.payload.dps["1"] ?  "on" : "off"
}
return msg;

Is that language JavaScript?

Sorry I've replied before reading your post.
Is it possible to set not only the key 7 as msg.payload but also convert its value from boolean to "on" and "off"?
I'm trying to obtain that with the expression of my previous reply but without success. I imagine that it happens because I should write in JSonata?

Just use a function node & set the payload and topic as desired...

msg.payload = {
  power: msg.payload.dps["1"] ?  "on" : "off"
}
msg.topic ="tuya/bimar_local/swing";
return msg;

Ps, yes, it is JavaScript.

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