Energy measurement with mqtt sensor

Hi all,

I’m quite new to this, but I tried to configure the energy consumption using my own “pulse counter” and mqtt. I get kWh reading from mqtt at 30s interwall. It is working OK.
Now I tried to add this to HA Home Energy Management.

In my configuration YAML I have this:
----------------------------------------------------------------cut-----------------------------------------------------------

mqtt sensors

sensor:

  • platform: mqtt
    state_topic: “home/power/consumptionkWh”
    unique_id: “PowerConsumption”
    name: “Sähkön kulutus”
    device_class: energy
    state_class: measurement
    unit_of_measurement: “kWh”
    last_reset_topic: “home/power/consumptionkWh”
    last_reset_value_template: “1970-01-01T00:00:00+00:00”
    value_template: “{{ value | float | round (2) }}”
    ----------------------------------------------------------------cut-----------------------------------------------------------

However it does not work.
When I look from Settings-Dashboards-Energy there is a complaint that “…has state measurement, but last reset is missing…”

What is wrong?

Maybe this (or the following posts) is the answer, it uses a last reset attribute:

Thank you Edvin_D.

I suppose I went ahead with this information. Not totally there yet.

Now my configuration YAML has this

---------------------------------------------------cut--------------------------------
sensor:

  • platform: mqtt
    name: “Sahkon kulutus”
    state_topic: “home/power/consumptionkWh”
    value_template: “{{ value | float | round (2) }}”
    unit_of_measurement: “kWh”
    unique_id: “mains_consumed_energy”
    device_class: energy
    state_class: measurement
    last_reset_topic: “home/power/consumptionkWh”
    last_reset_value_template: “1970-01-01T00:00:00+00:00”

template:

  • sensor:
    • name: “Saarnitie sahkon kulutus”
      unit_of_measurement: “kWh”
      state: >
      {{ states(“sensor.mains_consumed_energy”) | float | round(2) }}
      device_class: energy
      state_class: measurement
      attributes:
      last_reset: “1970-01-01T00:00:00+00:00”
      ---------------------------------------------------cut--------------------------------

No more complaining about anything, but the sensor.saarnitie_sahkon_kulutus will not get any value.
The state: >
{{ states(“sensor.mains_consumed_energy”) | float | round(2) }}
must be wrong, but how to fix?

It is hard to tell with the code pasted in the message directly and not inside a code block. Can paste the code inside a </> block?

Also you should check the unique id of the first sensor. It is missing the right prefix, so it may have gotten a different unique_id alltogether. I’m guessing sensor.sahkon_kulutus

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