Energy measurement with mqtt sensor

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