Configer a MQTT sensor other than temperature/humidity

Hi,

I have spent many hours on getting this configured, but without getting it done fully

Contexte:

  • ESP8266 attached to a pulse sensor for water meter.
  • such ESP is connected through wifi/internet to hivemq (the sensor will be in the future outside my Intranet, so I need to go through Internet).
  • home assistant with mqtt, the mqtt acting as bridging and receiving all mqtt traffic from hivemq
  • MQTT discovery messages send by the esp to HA for automatic discovery.
  • sensor update reguarly to get water volumes/flow/timestamp/pulsecount to be sent to HA and get it into graphes etc.

what I got so far:

  • managed all steps above, except the last one: the esp is recognized by HA and I see the four sensors in the Development / States page
  • if I modify the MQTT discovery message so that it mimics as a temperature sensor, then the values sent by the esp are recognised by HA and shown on the Development / States pages.
  • if I declare them as volumes/flow/timestamp/pulsecount, then the value don’t get on the Development / States page (nor to any other graphe).

My intuition is saying me, that I don’t have the right parameters, so that HA can allocate the ongoing MQTT values to the right sensor. it works for temperature, but not for volume.

below extract of the (quite long) script:

MQTT discovery message working: (the integrated sensor will show ongoing values received by MQTT

void sendMQTTWaterMeterDiscoveryPulseCountMsg() {
  String discoveryTopic = "homeassistant/sensor/lr_watermeter/lrwm_pulsec/config";
  DynamicJsonDocument doc(1024);
  char buffer[256];
  doc["name"] = "LR_wm_pulsec";
  doc["stat_t"]   = stateTopic;
  doc["dev_cla"] = "temperature";
  doc["frc_upd"] = true;
  doc["val_tpl"] =  "{{ value_json.temperature|default(0)}}";
  size_t n = serializeJson(doc, buffer);
  client->publish(discoveryTopic.c_str(), buffer, n);

with:

      // preparation of the MQTT message
      DynamicJsonDocument doc(1024);
      char buffer[256];
      doc["temperature"]    = pulseCount;
      
      bool published = client->publish(stateTopic.c_str(), buffer, n);       

but this is not working (sensor is integrated, but picking up the ongoing values).

void sendMQTTWaterMeterDiscoveryVolumeMsg() {
  String discoveryTopic = "homeassistant/sensor/lr_watermeter/lrwm_volume/config";
  DynamicJsonDocument doc(1024);
  char buffer[256];
  doc["name"] = "LR_wm_volume";
  doc["stat_t"]   = stateTopic;
  doc["unit_of_meas"] = "m3";
  doc["dev_cla"] = "volume";
  doc["frc_upd"] = true;
  doc["val_tpl"] = "{{ value_json.value }}";
  size_t n = serializeJson(doc, buffer);
  client->publish(discoveryTopic.c_str(), buffer, n);

  Serial.print("Discovery message: ");
  Serial.println(buffer);
}

with:

      // preparation of the MQTT message
      DynamicJsonDocument doc(1024);
      char buffer[256];
      doc["volume"]    = volume;
      bool published = client->publish(stateTopic.c_str(), buffer, n);       

      Serial.println(buffer);


Any idea, where I need to search to get this working?

I thank you in advance !

That looks like it should be value_json.volume

Unless the value is being changed elsewhere.

Some output messages would probably help.

thank you, it worked

I should have seen it myself :slight_smile:

anyway, it’s done and thank you again !