Energy setup using MQTT energy sensors

My energy dashboard readings are way too high and I don’t know why!

I have an Open Energy Monitor system that gathers energy data measured at the solar inverter and grid meter. This system has been running for a few years and has served me well but I would like to display this data in Home Assistant Energy dashboard.

This data is ingested by an EmonCMS instance running on a raspberry pi and processed to generate instant power readings in watts, each of those power readings are logged and used to generate three cumulative readings of energy in kWh. These readings never reset and are published over MQTT.
The readings are:
total_energy - net total of all energy imported and exported
solar_energy - sum of all energy generated by the solar system
solar_export_energy - sum of all exports - this value was only created yesterday and so is much lower than the other readings

With some difficulty I have managed to get home assistant to recognise these energy readings with the following declarations in my configuration.yaml

  - platform: mqtt
    name: "total_energy"
    state_topic: "grid/energy_total"
    unit_of_measurement: 'kWh'
    device_class: energy
    state_class: total
  - platform: mqtt
    name: "solar_energy"
    state_topic: "grid/energy_solar"
    unit_of_measurement: 'kWh'
    device_class: energy
    state_class: total_increasing
  - platform: mqtt
    name: "solar_export_energy"
    state_topic: "grid/solar_export_energy"
    unit_of_measurement: 'kWh'
    device_class: energy
    state_class: total_increasing

      - name: "use_energy"
        unit_of_measurement: 'kWh'
        device_class: energy
        state_class: total_increasing
        state: >
            {% set grid = states('sensor.total_energy') | int %}
            {% set solar = states('sensor.solar_energy') | int %}
            {{ (grid + solar) }}

As I understand it, the energy dashboard requires energy readings in kWh, so I have configured it with the “use_energy” value for Grid Consumption, “solar_export_energy” for Return to grid and “solar_energy” for Solar production.

As you can see in the attached screenshots this does not produce valid readings, can anyone suggest what I’m doing wrong?

It almost seems like it was expecting instantaneous power reading rather than energy readings but the documentation seems very explicit that it wants energy sensors with units in kWh.

I have also attached a screenshot of my EmonCMS energy dashboard over the same period for comparison.



Ok, I think I have it sorted. Turns out the issue was with EmonCMS not Home Assistant.
Within the EmonCMS input config I was taking my power reading then converting to a kWh energy reading then publishing to MQTT.
EmonCMS will log to a local feed when converting from power to kWh but won’t modify the value that is then passed to the MQTT publish service.
This meant that I had power readings in watts in my kWh MQTT energy feeds.
To solve this I chose an input in EmonCMS that was always 0 then added the kWh feed to it (a function that does pass it’s modified value to the next step) then used the publish to MQTT function.
It’s been a couple of hours and the values still aren’t quite right but I’m guessing there is a bunch of garbarge historical data in there and it is at least in the right orer of magnitude.
Here’s hoping that after 24hrs it will give accurate readings.

Nope, still wasn’t right but I’m pretty sure I’ve got it sorted now.
So, what the energy configuration wants is:
Grid Consumption: A positive increasing number counting only the energy that flows IN across your meter into your home
Return to grid: A positive increasing number counting only the energy that flows OUT across your meter into the grid
Solar production: A postive increasing number counting all the energy that flows out of your solar system

This is quite counter intuitive if you are measuring your electricity usage using CT clamps as they give you power readings that will go positive or negative depending on the flow of power. For these systems you have to split your power measured at the meter into two readings, one for when the reading is positive and one for when it is negative. You then need to convert the negative reading to positive and convert both of these, plus power measured at you solar inverter, to an incrementing energy reading in kWh. Once you have done that then you are ready to export those values over MQTT to Home Assistant using the correct device_class and state_class tags to use in the Energy dashboard configuration.

I’ll keep an eye on the readings over the next 24hrs and if they appear correct, I will mark this as solved.

Can you post your yaml for the sensors and adjustments?

1 Like

For this I have to create an mqtt sensor in order to maintain it? Do you have a working configuration?
It’s just frightening that everyone works for themselves and there are no instructions to be found on the net that absolute beginners can use.

An update for those who have asked, I never ended up getting readings that matched the emonCMS dashboard or manual meter readings closely enough to be considered useful and basically gave up on the energy dashboard in home assistant. Instead I have embedded the page from EmonCMS into another home assistant dashboard and I use that for my energy monitoring and ignore the home assistant one.
Really wish I could hide that Energy dashboard as it upsets me every time I see it.

1 Like

If you click on your name, bottom left menu of HA you can then select edit button for ‘change the order and hide items from sidebar’ then just click the x next to energy to hide it.