Help to generate in JSON

Help to generate in JSON the form

{“devices”: [{“Mac”: “MAC”, “sensors”: [{“ID”:“…”, “value”: 00.00 }, {…} ]}, {…} ]}

to send to the post HTTP requests. The “value” is the id(my_sensor).state

I tried it this way, it doesn’t compile.

> interval:
>   - interval: 5min
>     then:
>      - http_request.post:
>         url: http://narodmon.ru/post
>         verify_ssl: false
>         headers:
>           Content-Type: application/x-www-form-urlencoded
>         json: |-
>           root["devices"] = [
>              json: |-
>                root["mac"] = "50:64:2B:4D:22:2E";     
>                root["sensors"] = [;
>                   json: |-
>                     root["id"] = "T1";
>                     root["value"] = id(test_ds).state;
>                ];
>                                 
>           ];
1 Like

Doesn’t compile? Yet no error messages are generated? Unbelievable.

          http_request.post:
            url: http://narodmon.ru/post
            json: |-
              JsonObject &device = root.createNestedArray("devices").createNestedObject();
              device["mac"] = "<00:00:00:00:00:00>";
              device["name"] = "<Device Name>";
              device["owner"] = "<Owners Email>";
              device["lat"] = <00.0000>;
              device["lon"] = <00.0000>;
              JsonArray &sensors = device.createNestedArray("sensors");
              JsonObject &temp = sensors.createNestedObject();
              temp["id"] = "temperature";
              temp["value"] = id(temperature).state;
              temp["unit"] = "C";
              temp["name"] = "Температура";
              JsonObject &co2 = sensors.createNestedObject();
              co2["id"] = "co2";
              co2["value"] = id(co2ppm).state;
              co2["unit"] = "ppm";
              co2["name"] = "CO2»;
2 Likes

I don’t know why, but that didn’t work. Perhaps updates changed things?

I found an example on which reads a little easier.

json: |-
  root["seg"][0]["on"] = false;
  root["seg"][0]["id"] = 0;
  root["seg"][1]["on"] = false;
  root["seg"][1]["id"] = 1;
  root["seg"][2]["on"] = false;
  root["seg"][2]["id"] = 2;
  root["seg"][3]["on"] = false;
  root["seg"][3]["id"] = 3;

To date, this does not work. The compiler swears at the lines:

JsonObject &device = root.createNestedArray("devices").createNestedObject();
JsonArray &sensors = device.createNestedArray("sensors");
JsonObject &temp = sensors.createNestedObject();

I just removed the & symbol and everything worked. I.e.:

JsonObject device = root.createNestedArray("devices").createNestedObject();
JsonArray sensors = device.createNestedArray("sensors");
JsonObject temp = sensors.createNestedObject();

Блок “interval” тоже немного изменил:

http_request:
  timeout: 10s

interval:
  - interval: 5min 
    then:
      http_request.post:
        url: https://narodmon.com/post
        verify_ssl: false

        json: |-
          JsonObject device = root.createNestedArray("devices").createNestedObject();
          ...

Otherwise, everything is unchanged.