Energy measurement with mqtt sensor

Thank you again Edvin_D

You are right about my sensor having wrong unique_id
It was indeed sensor.sahkon_kulutus

What did you mean by not having right prefix? I copied that line from the example you linked, so what went wrong there?

It was missing the “sensor.” prefix. But I’ve also seen the entry in yaml being ignored and the unique_id being generated from the name. Hence my guess. That can also have been the case here. In that case you can always change it in the gui if you do not like what was autogenerated.

OK, now I have this in configuration.yaml:

#mqtt & sensor definitions
sensor:
  - platform: mqtt
    name: "mqtt sahkon kulutus"
    state_topic: "home/power/consumptionkWh"
    value_template: "{{ value | float(0)  | round (2) }}"
    unit_of_measurement: "kWh"
    device_class: energy
    state_class: measurement

template:
  - sensor:
      - name: "Saarnitie sahkon kulutus"
        unit_of_measurement: "kWh"
        state: >
          {{ states("sensor.mqtt_sahkon_kulutus") | float(0) | round(2) }}
        device_class: energy
        state_class: measurement
        attributes:
          last_reset: "1970-01-01T00:00:00+00:00"

There is no errors/warnings on energy dashboard. No items in logs.
Energy consumption uses sensor.saarnitie_sahkon_kulutus as sensor.

image

sensor.saarnitie_sahkon_kulutus has value:

Still not getting any data on energy dashboard.

???

I think maybe the state_class must be total_increasing. Are the values for the sensor as expected?

Yes, both sensors (sensor.mqtt_sahkon_kulutus and sensor.saarnitie_sahkon_kulutus) have the same value. I will try the total_increasing and lets see how it goes.

With total_increasing I have this in log:
Entity sensor.mqtt_sahkon_kulutus from integration mqtt has state class total_increasing, but its state is not strictly increasing. Triggered by state 2.52 (2.64) with last_updated set to 2022-05-30T15:35:11.987589+00:00.

Still not working. I have made small changes to configuration.yaml, but I do not think they matter.
I also deleted file .storage\energy, then restarted HA and reconfigured the energy management.

configuration.yaml

mqtt:
  sensor:
    - name: "mitattu sahkon kulutus"
      state_topic: "home/power/consumptionkWh"
      value_template: "{{ value | float  | round (2) }}"
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: measurement



template:
  - sensor:
      - name: "Saarnitie sahkon kulutus"
        unit_of_measurement: "kWh"
        state: >
          {{ states("sensor.mitattu_sahkon_kulutus") | float(0) }}
        device_class: energy
        state_class: measurement
        attributes:
          last_reset: "1970-01-01T00:00:00+00:00"

The sensor.saarnitie_sahkon_kulutus gets values OK

image

image

But the energy page is still not working.

Any ideas, what to try next?

Take a look at the development tools - statistics, find your sensor and check if it has a problem. There is a possibility to fix it

No problems there.

I have no idea what it’s like with an MQTT sensor, but what I have Shelly, Esphome (api) sensors have all the attributes state_class: total_increating

Blockquote
I have no idea what it’s like with an MQTT sensor, but what I have Shelly, Esphome (api) sensors have all the attributes state_class: total_increating

I tried that earlier, but it did not help.

This topic however had the solution:

I changed the last_reset to a random value, but not the c-language “zero”, that it used to be:

template:
  - sensor:
      - name: "Saarnitie sahkon kulutus"
        unit_of_measurement: "kWh"
        state: >
          {{ states("sensor.mitattu_sahkon_kulutus") | float(0) }}
        device_class: energy
        state_class: measurement
        attributes:
          last_reset: "2022-06-01T12:00:00+00:00"

and now it looks like I have data coming to Energy Management:

image

:rage: It broke during the night.

image

however the sensor.saarnitie_sahkon_kulutus is still getting values OK.

image

Looks like configuration is OK, but there is something else that prevents proper function.

First of all, I’ll say that I don’t really know what the answer is but wanted to share some pieces of information.

I recall there were changes made sometime last year regarding the last_reset so that post you referenced may or may not work.

Here is a web page that might help (or may confuse you even more), and at this particular place on the page it says that state_class: measurement can’t be used with device_class: energy. Again, I don’t know if this is accurate, and others have somehow gotten this to work with Energy Management, but that’s what it says.

Also check your log as they made a change to the code to print out a log warning if last_reset was also used with state_class: measurement.

Thank you wmaker. That web page I haven’t read before. Confusing, yes, but I made some experiments.

mqtt:
  sensor:
    - name: "mitattu sahkon kulutus"
      state_topic: "home/power/consumptionkWh"
      value_template: "{{ value | float  | round (2) }}"
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total

template:
  - sensor:
      - name: "Saarnitie sahkon kulutus"
        unit_of_measurement: "kWh"
        state: >
          {{ states("sensor.mitattu_sahkon_kulutus") | float(0) }}
        device_class: energy
        state_class: total

Changing state_class to total seems to work.

image

Let’s see, what I have in the morning, but looks promising for now.

OK, now i’m getting values. But these setting are not right for my energy sensor. I’m counting led blinks from energy meter and then calculating what the measured number of pulses means as kWh. The calculated value is sent through mqtt to HA sensor every 60 seconds. So the state_class: total is not right.

If I change my mqtt sensor and sensor.saarnitie_sahkon_kulutus state_class back to measurement,
then I get error on my energy dashboard settings page, that sensor is missing last_reset.

This I do not understand, since measurement state_class is current value. Why should that have last_reset???

With state_class: measurement I do not get any values to energy management page??

So, experimenting continues, or does anyone have solution to my problem?

I have solved this another way. Now I get the number of pulses (led blinks) from my energy meter to a mqtt sensor in HA. Then add these pulses to ( 1 pulse is 1 Wh so I divide by 1000 to change unit to kWh) utility meter and use that as energy management source.

So I think this problem is solved. I thank everyone who answered to this topic. Each answer took me further on solving the problem.

1 Like

Can you show the final config for that sensor? Thanks.

1 Like

Sorry for the delay. I did not read this topic anymore (since it is solved).
Anyway, here is the solution.

FIrst the mqtt sensor from configuration.yaml:

mqtt: 
  sensor:
    - name: "mitattu kulutus pulssit kwh"
      state_topic: "home/power/numOfWhPulses"
      value_template: "{{ value | float(0) / 1000 }}"
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: measurement
      force_update: true

Then the utility meter. I configured it in UI from helpers.
Here is the code, looked from developers tools - states (I’m not sure about the terms I use, because my HA is in Finnish language)

state_class: total_increasing
source: sensor.mitattu_kulutus_pulssit_kwh
status: collecting
last_period: '0.016'
last_reset: '2022-06-14T11:00:00.009003+00:00'
unit_of_measurement: kWh
device_class: energy
icon: mdi:counter
friendly_name: Saarnitie kulutus pulssit kWh summa

2 Likes

Hello,
will be possible to paste here both sensor and template from tha yaml file?

Hi @tapanileppanen
thank you for your post. Thanks to this thread (and Chatgpt ;)) I was able to include my energymeasurement with mqtt. It is going smooth since yesterday evening. Maybe this helps someone:

  1. in configuration.yaml I included mqtt: !include mqtt.yaml
  2. in mqtt.yaml I defined the following code:
sensor:
  # MQTT-Sensors (3 phases) in Watt collecting the actual consumption per phase 
  - name: "strom_phase1_aktuell"
    state_topic: "raspi4/Technik/OWL/Strom/powerCurr0"
    unit_of_measurement: "W"
    device_class: "power"
    state_class: "measurement"

  - name: "strom_phase2_aktuell"
    state_topic: "raspi4/Technik/OWL/Strom/powerCurr1"
    unit_of_measurement: "W"
    device_class: "power"
    state_class: "measurement"

  - name: "strom_phase3_aktuell"
    state_topic: "raspi4/Technik/OWL/Strom/powerCurr2"
    unit_of_measurement: "W"
    device_class: "power"
    state_class: "measurement"

  # MQTT-Sensors (3 phases) in Watt collecting the cummulated consumption per day and phase
  - name: "strom_phase1_tag"
    state_topic: "raspi4/Technik/OWL/Strom/powerDay0"
    unit_of_measurement: "W"

  - name: "strom_phase2_tag"
    state_topic: "raspi4/Technik/OWL/Strom/powerDay1"
    unit_of_measurement: "W"

  - name: "strom_phase3_tag"
    state_topic: "raspi4/Technik/OWL/Strom/powerDay2"
    unit_of_measurement: "W"
  1. in configuration.yaml i defined the following code:
#Templates to calculate the kWh of the daily consumption per phase. Needed for the energy dashboard.
template:
  - sensor:
      - name: "strom_phase1_tagesverbrauch_kwh"
        unit_of_measurement: "kWh"
        device_class: "energy"
        state_class: "total_increasing"
        state: "{{ (states('sensor.strom_phase1_tag') | float / 1000) | round(2) }}"

      - name: "strom_phase2_tagesverbrauch_kwh"
        unit_of_measurement: "kWh"
        device_class: "energy"
        state_class: "total_increasing"
        state: "{{ (states('sensor.strom_phase2_tag') | float / 1000) | round(2) }}"

      - name: "strom_phase3_tagesverbrauch_kwh"
        unit_of_measurement: "kWh"
        device_class: "energy"
        state_class: "total_increasing"
        state: "{{ (states('sensor.strom_phase3_tag') | float / 1000)
  1. Restart Home Assistant (only Yaml worked for me)
    good luck.

If it works, there should be 9 new entities.

Br Tom