Extract same data from different objects

Hi all,

Nodered subscribes to a topic of a Zbridge which delivers Sensor data in its message:

{"ZbReceived":{"0xB342":{"Device":"0xB342","Name":"OFIC","Temperature":24.57,"Humidity":83.39,"Endpoint":1,"LinkQuality":81}}}

If I rightclick to copy the path to extract "Temperature", it will just extract the humidty value of this specific device but I would like to extract "Name" and "Humidity" of all devices, so the payload I wish to extract is not

payload.ZbReceived["0xB342"].Temperature

but

payload.ZbReceived["0xAB23"].Temperature

I manage to change the payload of each device to the desired values and extract them, but unfortunately, the device ID changes often and I need to adjust the code.

Is there a way to change any of these incoming messages to name and humidity independently?

Does each ZbReceived have other keys, or is the one of interest the only one? If it has others, how do you know which is the one of interest?

From that data structure I am guessing that you have tasmota running on the bridge. If so, take a look at the setoption83 command in tasmota. When enabled tasmota will replace that ID number with the ‘friendly’ name of the zb device.

(Edit) I’ve just managed to take a look at how my bridge at home is configured. You may find the setoption119 a better option. It removes the ID from that part of the message.

1 Like

You need to identify the msg.payload.ZbReceived 1 and only property, which will be the name, then use that to extract the info.
e.g.

const name = Object.keys(msg.payload.ZbReceived)[0];
const temp = msg.payload.ZbReceived[name].Temperature;
const hum = msg.payload.ZbReceived[name].Humidity
msg.payload = {name, hum, temp};
return msg;

Example flow

[{"id":"1b26608e566196d3","type":"inject","z":"da8a6ef0b3c9a5c8","name":"0xB342","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"ZbReceived\":{\"0xB342\":{\"Device\":\"0xB342\",\"Name\":\"OFIC\",\"Temperature\":24.57,\"Humidity\":83.39,\"Endpoint\":1,\"LinkQuality\":81}}}","payloadType":"json","x":230,"y":600,"wires":[["f6f3a0abdfcdd0ea"]]},{"id":"f6f3a0abdfcdd0ea","type":"function","z":"da8a6ef0b3c9a5c8","name":"function 19","func":"const name = Object.keys(msg.payload.ZbReceived)[0];\nconst temp = msg.payload.ZbReceived[name].Temperature;\nconst hum = msg.payload.ZbReceived[name].Humidity\nmsg.payload = {name, hum, temp};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":600,"wires":[["22e0d6e0318685cd"]]},{"id":"06fa988581035686","type":"inject","z":"da8a6ef0b3c9a5c8","name":"0xAB23","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"ZbReceived\":{\"0xAB23\":{\"Device\":\"0xB342\",\"Name\":\"OFIC\",\"Temperature\":25.57,\"Humidity\":73.39,\"Endpoint\":1,\"LinkQuality\":81}}}","payloadType":"json","x":230,"y":640,"wires":[["f6f3a0abdfcdd0ea"]]},{"id":"22e0d6e0318685cd","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 86","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":600,"wires":[]}]
2 Likes

Thanks for the speedy reply. Yes, that is the problem. Each ZbReceived has a different key and if this was not enough: they are changing. They are all of interest as they all send climate data: temperature and humidity. And although the the ZbReceived ID changes, the name does not.

@M-a-r-t-i-n
You have seen this correctly, I am using Tasmota and thanks a lot for those two solutions. Indeed, this will sort my problem.

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