Page 1 of 1

ESP Easy MQTT ARRAYs

Posted: 19 Aug 2017, 18:00
by oarchie
I am trying to connect ESP Easy to HomeAssistant.
The MQTT string that I get from the ESP Easy with a DHT22 configured device.
I have enabled Controller 1 as domoticz mqtt.

My question relates to the mqtt protocol format.

I am successfully receiving mqtt messages.
My configuration.yaml looks like:

Code: Select all

  - platform: mqtt
    state_topic: "/home/ESPSensor/DHT001"
    name: "RoomT"
    unit_of_measurement: "°C"
    value_template: '{{ value_json.svalue[0] }}'
  - platform: mqtt
    state_topic: "/home/ESPSensor/DHT001"
    name: "RoomH"
    unit_of_measurement: "%"
    value_template: '{{ value_json.svalue[1] }}'
  - platform: mqtt
    state_topic: "/home/ESPSensor/WaterTemp"
    name: "WaterT"
    unit_of_measurement: "°C"
    value_template: '{{ value_json.svalue }}'
The received mqtt encoded data looks like:

Code: Select all

/home/ESPSensor/DHT001 {"idx":22,"nvalue":0,"svalue":"15.90;32.70;0"}
/home/ESPSensor/WaterTemp {"idx":18,"nvalue":0,"svalue":"16.19"}
but HA will not read the DHT001 svalues.
It is OK with the WaterTemp svalue.

In order for the svalue to be encoded using JSON array notation - (svalue[0]) - shouldn't the mqtt JSON values look like this:

Code: Select all

/home/ESPSensor/DHT001 {"svalue":[16.40,32.50,0],"idx":22,"nvalue":0}
/home/ESPSensor/WaterTemp {"svalue":"15.44","idx":18,"nvalue":0}
If I change the JSON encoding (in _C012.ino for test) to look like:

Code: Select all

          JsonObject& root = jsonBuffer.createObject();
          JsonArray& svalue = root.createNestedArray("svalue");
          
          ...
                      
          case SENSOR_TYPE_TEMP_HUM:                      // temp + hum + hum_stat, used for DHT11
              root[F("nvalue")] = 0;
              svalue.add(UserVar[event->BaseVarIndex], ExtraTaskSettings.TaskDeviceValueDecimals[0]);
              svalue.add(UserVar[event->BaseVarIndex + 1], ExtraTaskSettings.TaskDeviceValueDecimals[1]);
              svalue.add(0);
//              values  = toString(UserVar[event->BaseVarIndex], ExtraTaskSettings.TaskDeviceValueDecimals[0]);
//              values += ";";
//              values += toString(UserVar[event->BaseVarIndex + 1], ExtraTaskSettings.TaskDeviceValueDecimals[1]);
//              values += ";0";
              // values.toCharArray(str, 80);
//              root[F("svalue")] =  values.c_str();
              // root[F("svalue")] =  str;

home assistant works and the values are captured.

Can you help me with this?

Re: ESP Easy MQTT ARRAYs

Posted: 19 Aug 2017, 18:55
by grovkillen
If you want to create custom MQTT messages:

https://www.letscontrolit.com/wiki/inde ... th_own_IDX

But I would not use the Domoticz settings if I don't connect to a Domoticz controller.

Re: ESP Easy MQTT ARRAYs

Posted: 20 Aug 2017, 03:20
by oarchie
Yes, I saw that method.
I was hoping to use the device config for setting up a lot of different WiFi based sensors.
I have implemented _C012.ino as HomeAssistant MQTT control plugin, using the mega branch, and PIO, upload, test, 1024
I can now select that from the Controllers Protocol.
Image
My question is really about the MQTT protocol and JSON, which I have used before.
From bblanchon (https://bblanchon.github.io/ArduinoJson/doc/encoding/) :
svalue":[16.40,32.50,0] defines an array of three elements svalue[0],[1],[2]
What does"svalue":"15.90;32.70;0" mean and how do you access it?

I am happy just to use the modified _C012.ino, which works.
From reading the docs, I thought that defining a DHT22 sensor, and MQTT protocol would generate a JSON string defining RoomT and RoomH as per the attached.
I would rather find out if I am missing something with respect to the current code base.
Cheers
Archie