Energy Cost Calculation went haywire after 2022.8

Hello everyone,

Seeing the amazing additions to 2022.8 I upgraded HA and my energy cost calculations have gone haywire. My google foo has failed me and I can’t figure out where my calculations are going crazy so I’m reaching out in desperation. Wall of text and images below. Thank you to everyone in advance.

Some background. I’m running HA in Docker with Enphase micro inverters for my solar setup. Following this thread I was able to get my energy dashboard running great and even found some areas where I can reduce wasteful consumption. All was running well from June up until the upgrade to 2022.8. After upgrading I started seeing energy costs in the thousands of dollars and started trouble shooting with no luck. The dashboard appears to be reporting the data correctly and the variable price is set correctly in the helper, but the cost calculations are getting lost in translation. Below are screenshots of the sensor configs along with the data they’re outputting.

Miscalculated costs.


Energy dashboard readings VS Enphase dashboard
image
image
Configuration file

Sensor states from developer tools




Energy Price Sensor


I sincerely appreciate your time and help.

Just so we have all the information, click on settings, dashboards and edit the energy dashboard. Click on the little pencil for both your grid consumption and return to grid entities and let us know how each one is configured to track costs and which entity is used if any.

Hello and welcome to the forum.

Please don’t post screen shots of configuration text. We can’t copy and edit those to help you. Paste the text correctly formatted.

This may not be your issue but you need to add availability templates to your template sensors.

Thank you for your help, I thought I got all of the info needed.
please see screen shots below.


Gotcha that makes sense thank you.
here is the formatted text

template:
  - sensor:
        name: Grid Import Power
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ [0, states('sensor.envoy_122115015836_current_power_consumption') | int - states('sensor.envoy_122115015836_current_power_production') | int ] | max }}
  - sensor:
        name: Grid Export Power
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ [0, states('sensor.envoy_122115015836_current_power_production') | int - states('sensor.envoy_122115015836_current_power_consumption') | int ] | max }}

sensor:
  - platform: integration
    name: Grid Import Energy
    source: sensor.grid_import_power
    unit_prefix: k
    unit_time: h
    method: left
    round: 2

  - platform: integration
    name: Grid Export Energy
    source: sensor.grid_export_power
    unit_prefix: k
    unit_time: h
    method: left
    round: 2

I’ve seen you mention availability sensors before, Do those go into the config file?
I will research this more.

Yes, like this:

template:
  - sensor:
        name: Grid Import Power
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ [0, states('sensor.envoy_122115015836_current_power_consumption') | int(0) - states('sensor.envoy_122115015836_current_power_production') | int(0) ] | max }}
        availability: >
            {{ states('sensor.envoy_122115015836_current_power_consumption') | is_number and states('sensor.envoy_122115015836_current_power_production') | is_number }}
  - sensor:
        name: Grid Export Power
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ [0, states('sensor.envoy_122115015836_current_power_production') | int(0) - states('sensor.envoy_122115015836_current_power_consumption') | int(0) ] | max }}
        availability: >
            {{ states('sensor.envoy_122115015836_current_power_consumption') | is_number and states('sensor.envoy_122115015836_current_power_production') | is_number }}

That was very easy to do with your pasted config. :slight_smile: Thanks for updating.

Note also that the |int filters now also have defaults, as they should.

Hey Tom, Could you please help me and OP understand why this is the issue. I’m failing to see the cause and if troubleshooting myself would be looking at the charts of the relevant sensors next. Thanks

Edit: also OP please note that Tom has added default values to your int functions which you also didn’t have. int(0) vice int

1 Like

It likely isn’t, which is why I said:

However it will prevent odd results if either or both of the sensors becomes unavailable.

1 Like

Hi,

I noticed you are using the same energy price for both grid export and grid import.
Did you want your energy price the same for export as it is for import?
Or a better way to express it, is this a 1:1 feed in tariff?

This looks different to my setup.

Here is my sensor

  - sensor:
      - name: Tariff Price
        unit_of_measurement: AUD/kWh
        state: >
            {% if is_state('utility_meter.daily_energy', 'DE_Peak') %}
                {{ 0.52872 * 0.8 }}
            {% elif is_state('utility_meter.daily_energy', 'DE_Shoulder') %}
                {{ 0.25247 * 0.8 }}
            {% elif is_state('utility_meter.daily_energy', 'DE_OffPeak') %}
                {{ 0.17704 * 0.8 }}
            {% else %}
                {{ 0.17704 * 0.8 }}
            {% endif %}

  - sensor:
      - name: Tariff Price Export
        unit_of_measurement: AUD/kWh
        state: >
                {{ 0.076 }}

and here is what mine looks like in dev tools

and here is what it looks like in dashboard

image

image

image

1 Like

Hi del13r,
Thank you for the guide to get the energy dashboard working! and helping with my question. Yes we are credited at retail rate so 1:1 for us which was a pleasant surprise after seeing what others are getting.

1 Like

I noticed your entity starts with input_number. instead of sensor.
I have no experience with input_number type of entity.

Wow, that is nice. We get 8c//kWh for feed in and 15 to 30c/kWh use (off-peak and peak).

I use an input number. It works fine.

For the record, everything you have posted so far looks functionally correct apart from the $ figure shown on the energy dashboard.

If i had to guess that $490 figure is close to your total lifetime credit given the export sensor is around 1200kwh (average price around $0.40)

Unfortunately I don’t think that was it. Here are three hours of costs compared.


Rate was $0.52 from above to below. 2.76kWh X $0.52 = $1.43 not $11.20

Rate was $0.33 from above to below. 10.56kWh X 0.33 = $3.48 not $31.40

I’ll keep an eye on it tomorrow perhaps it needs a fresh start?

That’s good too know. I’m not sure what it could be anymore at first it seemed like it a simple rounding error. See This Reply for updated costs over a 3 hour period.

While hunting around I did notice these sensors that go back to 0 whenever I reboot home assistant

I don’t remember creating these sensors, so perhaps energy dashboard creates them for use by energy dashboard

Maybe check what these are doing.

The names will probably be different for you. Try searching for _compensation and _cost

This is correct. I have the compensation one but not the cost one because I use my own sensor calculating the total cost. This means HA doesn’t need to do any further calculations for my cost, but for compensation I have a flat rate configured in the energy dashboard and therefore HA needs to calculate the total itself.

The compensation and cost sensors should be identical to the entity name used in the energy dashboard but with cost/compensation on the end.

@cjueden would be interesting to see what your two sensors look like in developer tools > states

I stumbled upon those as well once and assumed the same as you did. Mine never seem to reset except for one day at 12:00 I may have force them to 0 in the dev tools on that day.

Interesting I seem to have a second set of the sensors with a suffix of the number 2