MQTT value Gas "Value": "10350420",

Hey,
I receive my meter reading via MQTT and want to display it in Home Assistant (HAS). Now I have the problem that my gas meter outputs “10350420” as m³, but in HAS is nothing to see.

MQTT Output:

{
  "MBusData": {
    "SlaveInformation": {
      "Id": "33437569",
      "Manufacturer": "ELS",
      "Version": "129",
      "ProductName": null,
      "Medium": "Gas",
      "AccessNumber": "20",
      "Status": "00",
      "Signature": "0000"
    },
    "DataRecord": {
      "_id": "0",
      "Function": "Instantaneous value",
      "StorageNumber": "0",
      "Unit": "Volume (m m^3)",
      "Value": "10350420",
      "Timestamp": "2023-04-29T09:45:12Z"
    }
  }
}

HAS Config:

    - name: "Erdgas"
      unique_id: mbusmeters.gas
      state_topic: "mbusmeters/3343756993158103"
      unit_of_measurement: "m³"
      device_class: "GAS"
      state_class: "total_increasing"
      value_template: >
        {%- for rec in value_json.MBusData.DataRecord -%}
        {%- if ( 
                ( rec.Function == "Instantaneous value" )
           and  ( rec.StorageNumber == "0" )
              ) -%}
        {{ rec.Value }}
        {% endif -%}
        {% endfor -%} 

Anything in the logs ?

Device class should be

device_class: gas

Here’s a screenshot of MQTT.


I’ve now adjusted the MQTT configuration for testing purposes, but I still don’t receive any data.

    - name: "Erdgas"
      unique_id: mbusmeters.gas
      state_topic: "mbusmetersgas/3343756993158103"
      unit_of_measurement: "m3"
      value_template: "{{ value_json.MbusData.DataRecord.Value }}"

Your iteration is not needed, and it’d be safer to use bracket notation particularly for keys that are like reserved words (value). This will return the Value if the other two things are correct, otherwise it’ll retain the prior sensor reading:

      value_template: >
        {% if value_json['MBusData']['DataRecord']['Function'] == "Instantaneous value" and 
              value_json['MBusData']['DataRecord']['StorageNumber'] == "0" %}
          {{ value_json['MBusData']['DataRecord']['Value'] }}
        {% else %}
          {{ states('sensor.erdgas') }}
        {% endif %}

Do you have other manually-configured MQTT sensors that are working?