Getting Zigbee device info from node-red-contrib-tasmota JSON

760 Views Asked by At

I'm just getting started with Tasmota on a Sonarr Zigbee Bridge and trying to interpret the data it spits out. Here's an example:

{
  "ZbReceived": {
    "0x11FE": {
      "Device": "0x11FE",
      "Name": "DoorContact",
      "Power": 1,
      "Endpoint": 1,
      "LinkQuality": 147
    }
  }
}

How do I address the info in here, without knowing/using the device number("0x11Fe") - for example, I want to get the device name, power setting, etc.

I was hoping I could do something like ZbReceived[1].Name, but that doesn't work.

Suggestions?

1

There are 1 best solutions below

0
On

What you are looking for is the Object.keys() function (docs)

var devices = Object.keys(msg.payload);
var name = msg.payload[devices[0]].Name;
var power = msg.payload[devices[0]].Power;

Object.keys(obj) returns and array of all the keys into the JSON object (one layer deep) so since we know that there is only one key in the msg.payload we can access it in slot 0 (arrays are always indexed from 0).