You can use history stats to get the total “on” time of the plug from and to specific dates and then use a template sensor to multiply the value of history state sensor with the power consumption of your device
Interestingly, the history stats sensor does work, but only seems to go back like 2 days (estimated by the values it provides)
Right now the current day shows 2.03 (which is definitely correct) but current month and current year both show 5.09 - that’s incorrect, because that heater runs aproximately 2-3h a day right now
and also a Template Sensor to calculate the theoretical power comsumption:
- platform: template
sensors:
bedroom_heater_power_consumption_today:
friendly_name: "Bedroom Heater Power Comsumption Today"
unit_of_measurement: 'kWh'
value_template: '{{ (states("sensor.bedroom_heater_runtime_today")|float * 1.50 )| round(2) }}'
# replace with the kW of your device here ^^^^^^
Honestly, I have no Idea what the |float stands for, but it won’t work without it | round(2) is the decimal places you want the value to be rounded to
Two questions remain:
for now only the last two days are recorded (I want to calculate the yearly power consumption though) - I’ll have to wait if this is only the case because the sensor is new and doesn’t have more data to work with for now…
what happens when the plug is on for more than 24h in sum - will it still count in hours (25-9999999) or will it toggle to days, weeks, months, years, decades, etc
Say the device uses 1.5 kw. Set up a template sensor that had the value 1.5 kw when the switch is on and 0kw when the switch is off. I am in my phone posting this so can’t play about and produce the template right now, but it should be simple enough.
Then integrate that sensor using the power integration sensor.
The readings here are exactly the same (wrong) as on the integration sensor
What could cause this!?
I already thought about making the sensor “update” the number every 5sec or so - maybe that’s what’s wrong (because an acutal power meter would do that, right?!)
The only thing I can think of, is that looking at the code for the integration sensor (here) is that the sensor’s last_updated attribute plays heavily into the calculation, which makes sense for most use cases.
Maybe something about that template sensor isn’t changing the value open enough, or atleast updating the last_updated attribute often enough, for it to correctly get calculated for integration.
The only idea i can think of to test this is to manually call the sensor entity to update every few minutes.
There’s an example automation to call a template sensor to update it’s value in the documentation here (essentially calling the homeassistant.update_entity service on that sensor every 5 minutes with a time pattern).
You can give that a shot and see if it helps any, of i’m just completely off in the woods with what’s causing this issue.
I assume that the obvious stuff like checking that the automation is enabled, and that the last triggered time makes sense is something you did, and it’s just that it didn’t fix the problem.
Maybe it’s something to do with the 0 not being interpreted as a number, but as a string? Not sure why this would happen, since it clearly accepts the 1.5. The only difference is that one has a decimal place in it. It would be too silly to suggest that “0.0” might work instead of just “0”, right?
I also tried to use 0.01 Instead of 0 - this is when I realized, that the second value seems to make no difference…
But I seem to have screwed up somewhere else … now random lights go on when I reload automations or restart HA (actually completely random) … also I can’t seem to make my automations set temperatures on my generic_thermostats any more… I’ll have to look into that
I restored an older Snapshot and HA seems to work correctly again
But the issue with the power meter remains. Every time I toggle the switch, the intergation sensor adds the kWh for the last period (no matter if the switch was on or off - it always adds “hours*kw”)
Can I change this template sensor
- platform: template
sensors:
bedroom_heater_power:
friendly_name: "Bedroom Heater Power"
unit_of_measurement: 'kW'
value_template: >
{% if is_state('switch.your_power_plug', 'on') %}
1.50 # <- replace with the kW of your device
{% else %}
0
{% endif %}
so it updates the value every 5 seconds, without using an automation?
Alternatively it would probably also work if the value loops between something like 1.49 and 1.51 every few seconds indefinitely, as long as the switch is on … (and 0 and 0.01 when it’s off - not ideal, but as long as it gets the job done)