Help needed! Energy database costs calculation wrong, after investigating everything

Before i forget, i have another test system running mariaDB from scratch now 5 days.
I can check how the data is build up, but where can i find the static_cost entry in the database ? as i did set that one as a static price

Is it just the auto created one with the same name as the source than with _cost after it ?

Ok this is from my main system (using homeassistant_v2)

this is from my third test system running mariaDB from scratch, same settings as my main system, using also static price

you can see it creates the data different, this data is both from short_term
and when ready ill add the migrated one under here

Until the first moment where HA thinks…lets reset :rofl:

:rofl: bet the developer will reply in the near future and just tell us a 2 second way to fix this.

1 Like

I am trying to find a dev on the discord chanel…if I would only get the code/doc on how this works then maybe this can be tackled.

Lets hope, i did update my post in the GitHub ticket… but you right its hard to track, but lets see after my migration is done how the database looks like, and if indeed the short_term is messing up.

I have the code but this is a lot of work analysing so I hope there is some designdoc (guess not?)
One thing I found is that when the sensor resets, the cost sensor also resets… but why or when does either of them need a reset???
core/sensor.py at dev · home-assistant/core (github.com)

Maybe it hangs in the initialisation ?

 # Initialize as it's the first time all required entities are in place.
            self._reset(energy_state)

or this can be the issue, copy the old stats after or before a reset

  # Energy meter was reset, reset cost sensor too
            energy_state_copy = copy.copy(energy_state)
            energy_state_copy.state = "0.0"
            self._reset(energy_state_copy)
 # Update with newly incurred cost
        old_energy_value = float(self._last_energy_sensor_state.state)
        cur_value = cast(float, self._attr_native_value)

Migration is ready, this is how it looks like.

This is my main, using homeassistant_v2

This is test server 2 using MARIADB from scratch, and no migration

this is from my new migration, migrated from the first screenshot

you can see the migrated one is correct, no issue with the migration at all…
So we can exclude the problem comes from the migration.

i did not start home assistant test server 3 yet, but as you can see some time has passed so it will add new data, and thats where it goes wrong, it will reset to 0, thats what happened last time

@vingerha let me know if u need something else before i start up my third test server. so we can see whats going to happen

// wait i noticed something, see screenshot 1
the entry at 11:15:00 is this = 1.291619999999892(state) 1176.3091600000134(sum)

the migrated one
the entry at 11:15:00 is this = 1.13905(state) 1176.16(sum)

//UPDATE2

i did start up my third system with the new migrated DB
getting this message now, running 2022.12 beta
image

lets wait till an update has happened, the costs look fine.

I have to stop spending time on this, with today’s misery on my own elec sensor I lost 3 hours …and for some sort of reason my manager want some time from me as well :slight_smile:
Do go ahead, I will see when DEV respond

Thanks again for helping, i will investigate, and lets hope a dev responds.

Regards

No feedback from devs but in the discord section there are more things … at least they are active :slight_smile:
For my own situation… the clean/copy that I did yesterday survived the night and mutliple short_term resets.

I got it, and it worked.

making a new entity importing the old data to the new one solves the problem. only the day that its imported need to be manually corrected

insert into statistics (created,start,state,sum,metadata_id) select created, start, 0, sum, MYNEWID from statistics where metadata_id = OLDID and start < ‘2022-12-01’

SO what i did was, create a total entity that uses the total cost, and i just imported it and today it shows the data correct.

Which is what I did too having removed all previous related cost_sensors…this is of course only remediation, not solution

For me it still calculate the correct data this whole day starting from 00:00
I did not even delete other data at all or copy something else.

Instead of using a entity( containing the cost per m³ or kWh) i start using an entity containing the total cost and imported the data from (the entity containing price per m³ or kWh) into the (total price) entity and it worked.

i don’t know if it works if u start importing data from (price per m³ or kwh) to a new entity using (price per m³ or kwh) instead of that i chose a total entity, to it calculated different.

Indeed that would work (as you demonstrate) but I believe it sucks to have something additional …it should work as intended :slight_smile:

You right, i still don’t know whats going wrong…

this is how the db look after importing into new one, i did import only the data till 30-11-2022

This is the day itself, it was messed up ofcourse, but today its corrected.

and now today the data is corrected and working

but anyway for now your solution worked, so atleast people who having this cost issue after converting they can just do this and for now using this temp solution, till the dev fix it.

And it happened again, my elec device had a connection / data issue so I was forced to re-add it via zigbee2mqtt, this created the next mess.
What I now think is that the cost-sensor has a unique id and when resetting, it re-creates.
So, I believe I will take your approach where I create the sensor myself with its own unique id and full control, this one does not change too.
((&(&(*&$@

Damn, well mine still works.
So converting from a static price/price per m³ or kWh to a total entity does fix it.
I did also import the old data and till now still great.

You should try that to, you don’t even have to correct the data, only the data from the day you did import the data, thats it.
be sure you import till the day before it, saves you lot of time.

i know its $%$$#%… but try it out.

Hi just for all of you who are having issues with the wrong cost calculation, I found that in developpers tools you have an entity called: sensor_cost. I have entered there and found the value that was messing my calculation. Changed it and now everything is fine.

image

image