0

I'm playing with NodeRed. Esp32 with Tasmota sending mqtt message like following example:

{
  "Time": "2021-12-05T20:46:40",
  "MJ_HT_V1391ef1": {
    "mac": "582d34684ef1",
    "Temperature": 22.6,
    "Humidity": 50.9,
    "DewPoint": 11.9,
    "Battery": 100,
    "RSSI": -92
  },
  "MJ_HT_V1385877": {
    "mac": "582d31385557",
    "Temperature": 22,
    "Humidity": 53.3,
    "DewPoint": 12,
    "Battery": 85,
    "RSSI": -86
  }
}

Please! How do I get the names MJ_HT_V1391ef1 and MJ_HT_V1385877 without knowing them and also json can have more similar object then only two. Also I will need to get to the information like MJ_HT_V1391ef1.mac or MJ_HT_V1391ef1.Temperature.

If I use something like

var zprava=msg.payload;
msg.payload=zprava["MJ_HT_V1391ef1"]["Temperature"];

It works and I get a value 22.6. But I need to do it more dynamic. Some loop which will go through the json?

I spend almost 5 days with finding solution. Please help me.

donvonhill
  • 13
  • 2
  • flow this to next function node and use javascript to iterate "for(var v in payload) { console.log(v); //MJ_HT_V1391ef1, MJ_HT_V1385877 }". then its javascript skill to manipulate data . payload[v]['mac'], payload[v]['Temperature'] etc – Immanuel Dec 06 '21 at 17:48
  • Why does it have to be dynamic, surely you will need to know the values ahead of time to because you will know which sensors you have deployed where. – hardillb Dec 06 '21 at 18:06

1 Answers1

0

JSONata to the rescue! Take a look here:

https://try.jsonata.org/gKcGY7Y2_

So you do $keys in a change node, then a split node, then just check if the key name starts with MJ (again, using JSONata):

$contains(payload, /^MJ/)