How adapt a custom mqtt reading gas sensor to use with new dashboard

Hi to all!
I cannot use a sensor to read my gas counter.

So, I manually take the measure every saturday.

I put it into a custom mqtt sensor:

sensor:
  - platform: mqtt
    name: "Gas reading"
    icon: mdi:stove
    state_topic: "utilities/gas/reading"
    value_template: "{{ value_json.value }}"
    unit_of_measurement: "m3"
    payload_available: "true"
    payload_not_available: "false"

input_number:
  gas_reading:
    name: Lettura
    min: 0
    max: 9999
    mode: box

script:
  gas_reading_save:
    alias: "Salva lettura"
    sequence:
      service: mqtt.publish
      data_template:
        topic: utilities/gas/reading
        payload_template: >-
          {
            "value": "{{ states('input_number.gas_reading') | int }}"
          }
        retain: true

utility_meter:
  gas_meter_monthly:
    name: Consumo gas mensile
    source: sensor.gas_reading
    cycle: monthly
  gas_meter_yearly:
    name: Consumo gas annuale
    source: sensor.gas_reading
    cycle: yearly

Of course now I would use the new gas dashboard.

How I need to adapth the mqtt sensor? Is it possible?

Thank you very much!

EDIT:

I added the state_class: total_increasing value, but I cannot see it on dashboard.

Now sensor is:

sensor:
  - platform: mqtt
    name: "Gas reading"
    icon: mdi:stove
    state_topic: "utilities/gas/reading"
    value_template: "{{ value_json.value }}"
    unit_of_measurement: "m3"
    payload_available: "true"
    payload_not_available: "false"
    state_class: total_increasing
    device_class: gas

This is the image from developers tools:

Schermata del 2021-09-01 22-25-26

Instead this is the image from ShellyEM that I use to feed the energy dashboard - it seems to me very, very similar!

Schermata del 2021-09-01 22-27-08

But HASS complaints with me that with gas there are no statistics availableā€¦

Iā€™m having the same problem but I did find a temporary workaround - paste the below code (edited for your sensor & cost) into the ā€œenergy_sourcesā€ array in the /config/.storage/energy file:

            {
                "type": "gas",
                "stat_energy_from": "sensor.gas_meter",
                "stat_cost": null,
                "entity_energy_from": null,
                "entity_energy_price": null,
                "number_energy_price": 0.72866617
            }

2 Likes

Interesting, so you forced dashboardā€¦

Atm I can use this workaround, we will wait for somebody :slight_smile:

Looking into config/core.entity_registry I can see that

1 - Shelly EM has its own entry:

{
                "entity_id": "sensor.shellyem_c45bbe5fed52_channel_1_energy",
                "config_entry_id": "64c222f83fbcdcbfd522c1ad08f9eb64",
                "device_id": "d28117bcc92e721638e80e10f3eb8855",
                "area_id": null,
                "unique_id": "C45BBE5FED52-emeter_0-energy",
                "platform": "shelly",
                "name": null,
                "icon": null,
                "disabled_by": null,
                "capabilities": {
                    "state_class": "total_increasing"
                },
                "supported_features": 0,
                "device_class": "energy",
                "unit_of_measurement": "kWh",
                "original_name": "shellyem-C45BBE5FED52 channel 1 Energy",
                "original_icon": null
            },

My sensor.gas_reading it is not displayedā€¦

Only one I have it is the input_number:

{
                "entity_id": "input_number.gas_reading",
                "config_entry_id": null,
                "device_id": null,
                "area_id": null,
                "unique_id": "gas_reading",
                "platform": "input_number",
                "name": null,
                "icon": null,
                "disabled_by": null,
                "capabilities": {},
                "supported_features": 0,
                "device_class": null,
                "unit_of_measurement": null,
                "original_name": "Lettura",
                "original_icon": null
            },

So, do I need to add another sensor ā€œchildrendā€ of sensor.gas_reading MQTT sensor with these capabilities?

Another tryā€¦

I added a template sensor like this:

template:
  - sensor:
      - name: "Gas Reading Statistics"
        unique_id: sensorgasreadingstatistics20210902
        unit_of_measurement: "m3"
        device_class: gas
        state_class: measurement
        state: >-
          {{ states.sensor.gas_reading.state }}

It works butā€¦ Gas dashboard complains with ā€œno statistics availableā€ā€¦ :frowning:

Now, with last template sensor, in config/.storage/core.entity_registry I have another sensor:

{
                "entity_id": "sensor.gas_reading_statistics",
                "config_entry_id": null,
                "device_id": null,
                "area_id": null,
                "unique_id": "sensorgasreadingstatistics20210902",
                "platform": "template",
                "name": null,
                "icon": null,
                "disabled_by": null,
                "capabilities": {
                    "state_class": "measurement"
                },
                "supported_features": 0,
                "device_class": "gas",
                "unit_of_measurement": "m3",
                "original_name": "Gas Reading Statistics",
                "original_icon": null
            }

Very similar to Shelly EM, but, gas dashboard doesnā€™t see itā€¦

I think I may have discovered the problem - the device class for ā€˜gasā€™ isnā€™t an option in the ā€˜customizeā€™ page for my sensor. Iā€™m not sure if this means the device class isnā€™t being loaded at all for my installation or just that the customize page needs to be updated with the gas attribute

Just put ā€œenergyā€ instead of ā€œgasā€, but nothing. In both sensors, I changed from gas to energy.

Neither seen in energy itself dashboard.

BINGO!

@AnthonyPluth I changed unit of measurmeent from ā€œm3ā€ to ā€œmĀ³ā€ (do you see the difference?)

Now I have available the sensor.gas_reading value inside the dashboard!

Is that with ā€˜gasā€™ or ā€˜energyā€™ as the device_class?

@AnthonyPluth gas, absolutely gas

Do you mind posting your full working config? Iā€™m still struggling to get my sensor to show upā€¦

@AnthonyPluth

Sorry, I donā€™t receive email notification and need to enter.

This is the sensor that I see on dashboard:

sensor:
  - platform: mqtt
    name: "Gas reading"
    icon: mdi:stove
    state_topic: "utilities/gas/reading"
    value_template: "{{ value_json.value }}"
    unit_of_measurement: "mĀ³"
    payload_available: "true"
    payload_not_available: "false"
    state_class: total_increasing
    device_class: gas

@sineverba no worries. Iā€™ve copied your config exactly but Iā€™m still getting the no matching statistics found message :cry:

So, did you set a fake MQTT sensor?

I repost my package:

#==================
#
# UTILITIES - GAS
#
#==================

sensor:
  - platform: mqtt
    name: "Gas reading"
    icon: mdi:stove
    state_topic: "utilities/gas/reading"
    value_template: "{{ value_json.value }}"
    unit_of_measurement: "mĀ³"
    payload_available: "true"
    payload_not_available: "false"
    state_class: total_increasing
    device_class: gas


input_number:
  gas_reading:
    name: Lettura
    min: 0
    max: 9999
    mode: box

script:
  gas_reading_save:
    alias: "Salva lettura"
    sequence:
      service: mqtt.publish
      data_template:
        topic: utilities/gas/reading
        payload_template: >-
          {
            "value": "{{ states('input_number.gas_reading') | int }}"
          }
        retain: true

utility_meter:
  gas_meter_monthly:
    name: Consumo gas mensile
    source: sensor.gas_reading
    cycle: monthly
  gas_meter_yearly:
    name: Consumo gas annuale
    source: sensor.gas_reading
    cycle: yearly


1 Like

Had the same problem with the gassensor (mqtt) not showing in the dashboard despite changing from m3 to mĀ³

In the corelog I see:
2021-09-04 12:12:02 WARNING (Recorder) [homeassistant.components.sensor.recorder] sensor.haenergy_gasmeter has unknown unit m3
2021-09-04 12:12:02 WARNING (Recorder) [homeassistant.components.sensor.recorder] The unit of sensor.haenergy_gasmeter (mĀ³) does not match the unit of already compiled statistics (m3). Generation of long term statistics will be suppressed unless the unit changes back to m3

so because it was recorded with wrongly m3 it does not accept a change to mĀ³ in the statistics.
Since I do not know how to reset these statistics I recreated the sensor with slightly different name.
Now it is available as a gas source.

Interesting, but I didnā€™t have these errors on log.
In my recorder, by the way, I have only 3/4 sensors explicitally specified.

Probably is this the difference?

Should the stat_class note be total_increasing

This finally helped, from all the not working solutions.
Thanks for posting the full required parts!

edit: maybe my simpler version also worked actually, but the energy dashboard just doesnt update very often.