# Sensor which shows total working time including percentage power

Hello there.

I know topic tells nothing but I don’t know how to describe it in single sentence. If you have any idea after reading this - tell me and I will change the topic (if possible).

So, I have a heat pump to heat my house. Fortunatelly it has pretty cool integration for HA. What you see here:

is a graph of heat pump modulation. In other words it just shows when heat pump was working and at what percentage power level (100% means 8 kWh, 50% is 4 kWh and so on).

I want to make few sensors which based on this sensor shows total consumed power per day / week / month. For simplicity of this example - let’s say that pump was working on 100% on 3 hours and on 50% for another 3 hours. This gives us 8x3 + 4x3 = 24 + 12 = 36 kWh per day. If this is a problem to covert it from time unit to kWh it will be good as well to keep it in time unit (minutes) this way:

Following previous example 100% * 3h + 50% * 3h = 3h + 1,5h = 4,5h.
Since 4,5h * 8kWh = 36 kWh it’s still clear to understand. So whatever option from those two possible but you get my point. At the end there can be additional sensor which just multiplies this first sensor value by 8.

Assuming your integration does not have a power senor, first create a template sensor that converts modulation to power (which is kW not kWh as in your explanation).

``````template:
- sensor:
- name: "Heatpump Power"
unit_of_measurement: "kW"
state: "{{ states('sensor.modulation')|float(0) * 0.08 }}"
device_class: power
state_class: measurement
availability:  "{{ states('sensor.modulation')|is_number }}"
``````

EDIT: fixed %

Feed this sensor to the Riemann Sum integration to get an energy consumed sensor (again, assuming your integration does not have one already):

``````sensor:
- platform: integration
name: Heatpump Energy
source: sensor.heatpump_power
method: left
``````

Feed this energy sensor to Utility meter helpers to track daily, weekly and monthly energy. These can be set up in Settings → Devices & Services → Helpers.

You can also add the heatpump energy sensor to the Energy Dashboard as an individual device.

1 Like

First of all I really appreciate for your reply. Second - sorry for late response but didn’t had time to play with this.

So I made 2/3 points of your instructions so far. Then forced heat pump to work just to collect some data. Honestly it gives me some sick values. After around 5 minutes of heat pump working I get readings like this:

Names of entities are exactly as in your config examples above.
I assume that reading are not incorrect but simply put “comma” `,` in incorrect place. But this is only my assumption. Maybe it’s due to converting percentage to power. Maybe 100% percent is not treated as 100/100 but as like 100. I don’t really know.

That would actually makes sense that simply “comma” is not in right place.

Over those 5 minutes it could consume around 0.23867 kWh with current power consumption at level 4.16kW.

Am I correct?

So maybe in this line of your script:

` state: "{{ states('sensor.modulation')|float(0) * 8 }}"`

it should be more like `float(0) * 0,08 }}"`

I’m not good at HA so I’m just guessing.

Yeah, I forgot to divide by 100 (for percent), sorry.

Try this:

``````state: "{{ states('sensor.modulation')|float(0) * 0.08 }}"
``````

That’s what I did. All works exactly as intended. Thank you very much for your help!

I need to ask you one more thing. Since at the first hour of sensor working it was multiplicated by 8 instead 0.08 sensor gathered some huge numbers. I wanted to reset this so I did the easiest thing possible - meaning delete database However tho every entity history reset EXCEPT this sensor:

How is this even possible??? After deleting DB it shows 0 but once pump starts (modulation > 0) it momentary shows this huge number. Where does he stores that value if I deleted database? It doesn’t make any sense to me. I got rid my whole HA history to get rid of it and this is the only thing I didn’t get rid of.

That was a bit drastic. You should have asked first.

Developer Tools → Statistics. Click on the ramp icon to the right of the entity id. You can edit the history there.

Emmm… even I’m missing something or something is not right because no matter which point in time I pick it shows 0:

If it is in the energy dashboard hover over the huge column in the energy graph to get the exact date and hour.

Yes - now I see that. I selected yesterday and time when I deleted database and there was actually huge number which I corrected to 0.

Thank you again

1 Like

Even tho I’m dumb or it’s not working right. I’ve found yesterday 185kWh entry and changed it to 0. However then today I see somewhere in time -185kWh entry. When I change this one to 0 as well the previous one goes back to 185kWh.

Anyway why deleting database didn’t cleared that? Where HA is keeping those values then?

As you can see graph starts at 23.09 at 19:40. I’ve edited it via developer tools and changed entry from 19:40 from 185 (same as graph begins) to 0. Even tho history graph still starts at 185.

It should have. The LTS (long term stats) are also stored in the database.

How did you delete it?

Did you stop home assistant first then delete `home-assistant_v2.db` and restart?

No - I didn’t stop HA first. I simply deleted `home-assistant_v2.db` and then immediatelly restarted HA. I also did that when heat pump was not working meaning that modulation was 0 to make sure it won’t try to update database meantime. It worked for all other entities

If I have HassOS, Supervisor and all that stuff how do I simply stop HA Core itself without shutting down HassOS just to delete database and then restart?

I think you have to do it from the CLI now.

``````ha core stop
``````

Wait a bit. Delete the file.

``````ha core start
``````

This is absolutelly ridicoulus. I just did what you said - stop core, delete DB, start core. Just after start it’s still showing the same values as just before removing DB.

Where is he getting this data from??? For air?

Maybe they are stored in some log files? They must be stored somewhere.

I’ve just found something. There’s a file:

`\config\.storage\core.restore_state`

and this file has something like this in it:

``````"state": {
"entity_id": "sensor.heatpump_energy",
"state": "208.579",
"attributes": {
"state_class": "total",
"source": "sensor.heatpump_power",
"unit_of_measurement": "kWh",
"device_class": "energy",
"icon": "mdi:chart-histogram",
"friendly_name": "Heatpump Energy"
``````

most interesting is this:

` "state": "208.579",`

This is exactly the number which is “hunting” me.

Can I remove this file along with DB?

Stop HA. Change the value from 208.579 to 0. Start HA.

I just did it along with deleting DB again. As you said I edited file and changed values to 0. After restart everything seems to be finally fine (all values are 0). But now I’m waiting for another heat pump start. After that I can be absolutelly sure it’s fine.

I will let you know once pump starts and will run for a while.

1 Like

VIOLA!!!

Next items says today, this week, this month and overall.

On Monday I will call heat pump guy. If he tells me that 100% modulation is not 8kWh then I will be pissed off

Anyway thanks again for your help! I think topic can finally closed now.
One more question - my HA recorder is set to keep history for 30 days.
However I assume that entity `sensor.heatpump_energy` will keep tracking overall consumption forever just history will be from last 30 days? Is that correct? I mean that after 3 months I will have total number from 3 months but history graphs from last month only?

1 Like