MQTT-discovery: Config-payload for sensor problem

Hello. I’m trying to implement the MQTT Sensor: Get temperature and humidity example on my ESP8266 but with the useful MQTT-discovery. I tried different approaches, searched the forum and am currently at an dead end. [See EDIT below: SOLVED]

Initially I tried to submit the following array-type config-payload to “homeassistant/sensor/sensorRoom/config”:

[{
“device_class”: “sensor”,
“name”: “Temperature”,
“state_topic”: “homeassistant/sensor/sensorRoom/state”,
“unit_of_measurement”: “°C”,
“value_template”: “{{ value_json.temperature | float/100}}”
},
{
“device_class”: “sensor”,
“name”: “Humidity”,
“state_topic”: “homeassistant/sensor/sensorRoom/state”,
“unit_of_measurement”: “%”,
“value_template”: “{{ value_json.humidity | float/100}}”
}]

This payload does not add anything and produces an error in the log:

Traceback (most recent call last):
File “/usr/lib/python3.6/asyncio/tasks.py”, line 179, in _step
result = coro.send(None)
File “/usr/lib/python3.6/site-packages/homeassistant/components/mqtt/discovery.py”, line 60, in async_device_message_received
payload = dict(payload)
ValueError: dictionary update sequence element #0 has length 5; 2 is required

Submitting the two sub-payloads subsequently to “homeassistant/sensor/sensorRoom/config” results in no error, but only the “temperature” reading shows up in HA. According to the logs the second config-payload is a duplicate:

2017-10-03 04:41:03 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/sensor/sensorRoom/config: {“device_class”:“sensor”,“name”:“Temperature”,“state_topic”:“homeassistant/sensor/sensorRoom/state”,“unit_of_measurement”:“°C”,“value_template”:“{{ value_json.temperature | float/100}}”}
2017-10-03 04:41:03 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: sensor sensorRoom

2017-10-03 04:41:03 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on homeassistant/sensor/sensorRoom/config: {“device_class”:“sensor”,“name”:“Humidity”,“state_topic”:“homeassistant/sensor/sensorRoom/state”,“unit_of_measurement”:“%”,“value_template”:“{{ value_json.humidity | float/100}}”}
2017-10-03 04:41:03 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: sensor sensorRoom

So am I right to say that HA identifies duplicates of MQTT-discovery by the object_id given in the config topic and that the above example cannot be reproduced with MQTT-discovery because it has identical entity_ids for both sensors? Or am I doing a big mistake? Thanks.

EDIT

After reading my post once more I found my obvious mistake. I need 2 different configuration topics to have 2 different object_ids:

“homeassistant/sensor/sensorRoomT/config”
“homeassistant/sensor/sensorRoomH/config”

Should this thread be deleted or kept for archive purposes?

I’m sorry that I’m getting into the resolved question, but I would like to ask for additional comments on the topic.

Is it possible to configure sensor’s parameter “json_attributes” via MQTT Discovery? By nested object etc?

Can anybody post an example for config topic payload with all possible parameters?

I’ have 1 state topic containing all 3 sensor values as JSON but 3 configuration topics. One for each sensor.

I’m not sure if this helps. But this is my config written in C using the <ArduinoJson.h> library to easier create JSON paylods in C. The code below produces the output which is shown at the end.

#define MQTT_T_S "homeassistant/sensor/sensorRoom/state"    
void createConfig(int sensorNumber) { //this will fill CONFIG_PAYLOAD
      StaticJsonBuffer<200> jsonBuffer1;
      JsonObject& root = jsonBuffer1.createObject();
      if (sensorNumber==0) {
        root["name"] = "Room Temperature";
        root["state_topic"] = MQTT_T_S;
        root["unit_of_measurement"] = "°C";
        //root["value_template"] = "{{ value | float/100}}";
        root["value_template"] = "{{ value_json.temperature | float/100}}";
      } else if (sensorNumber==1) {
        root["name"] = "Room Humidity";
        root["state_topic"] = MQTT_T_S;
        root["unit_of_measurement"] = "%";
        //root["value_template"] = "{{ value | float/100}}";
        root["value_template"] = "{{ value_json.humidity | float/100}}";
      } else if (sensorNumber==2) {
        root["name"] = "Room CO2";
        root["state_topic"] = MQTT_T_S;
        root["unit_of_measurement"] = "ppm";
        //root["value_template"] = "{{ value | int}}";
        root["value_template"] = "{{ value_json.CO2 | int}}";
      } else if (sensorNumber==3) {
        root["name"] = "Room Air Quality";
        root["state_topic"] = MQTT_T_S;
        root["unit_of_measurement"] = "ppm";
        //root["value_template"] = "{{ value | int}}";
        root["value_template"] = "{{ value_json.AQ | int}}";
      }
      root.printTo(CONFIG_PAYLOAD, sizeof(CONFIG_PAYLOAD)); //must be last
    //  root.prettyPrintTo(Serial);
    //  Serial.println("");
      Serial.print("Real config payload length: ");
      Serial.println(strlen(CONFIG_PAYLOAD));
    //  Serial.println("");
    }

JSON configuration strings sent one after one to different configuration topics:

Config published to: “homeassistant/sensor/sensorRoomT/config”

{
  "name": "Room Temperature",
  "state_topic": "homeassistant/sensor/sensorRoom/state",
  "unit_of_measurement": "°C",
  "value_template": "{{ value_json.temperature | float/100}}"
}

Config published to: “homeassistant/sensor/sensorRoomH/config”

{
  "name": "Room Humidity",
  "state_topic": "homeassistant/sensor/sensorRoom/state",
  "unit_of_measurement": "%",
  "value_template": "{{ value_json.humidity | float/100}}"
}

Config published to: “homeassistant/sensor/sensorRoomC/config”

{
  "name": "Room CO2",
  "state_topic": "homeassistant/sensor/sensorRoom/state",
  "unit_of_measurement": "ppm",
  "value_template": "{{ value_json.CO2 | int}}"
}

And this is my data payload sent periodically to “homeassistant/sensor/sensorRoom/state”.

{
  "temperature": "2759",
  "humidity": "4170",
  "CO2": "1122"
}

I realize this is an old thread, but I just spent way too long trying to get some sensors to report their config. Finally found an answer on a random website (I won’t share it publicly, because it’s a little … um… out there), but basically I didn’t realize that the pubsubclient (by default) only allowed for a max packet size of 128 for a message. Setting the #define MQTT_MAX_PACKET_SIZE to 256 in pubsubclient.h solved the issue for me.

If anyone finds this thread in Google search, like I did, change your max packet size. :wink:

Chalk this one up to me being away from my Arduino code for too long. :stuck_out_tongue: