Calculate a device's theoretical power comsumption by the time it's on

Ok, I have yet another problem I can’t seem to solve on my own xD

I have a couple of Osram Smart+ Plugs in my home that switch heaters, and dehumidifiers.

Since these are relatively power hungry devices, I would like to approximately know how much kWh each of them consume every day/week/month/year.

Theoretically this should be a simple calculation - HA knows how long these switches are on and I know the power comsumption of the devices.

But how can I combine these values so that I can show them for example as graphs?!

I looked into https://www.home-assistant.io/components/history_stats/ but I don’t think I can use that. Also I can’t find a way to actually calculate the power comsumption in the end…

Is there a built in way, or a custom component that does this kind of calculation?

1 Like

Yeah - I found that too - but I don’t have a “source sensor” - Only an on/off plug :thinking:

yeah I see, you know the power consumption rate, rather than it being returned by a sensor, such as a plug that reports power usage.

I think you will need some sort of template sensor that gives a power consumption reading, then use the integration sensor on that.

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

https://www.home-assistant.io/components/history_stats/

That’s what I’m trying to do right now.

Do you know how I can make “current year” and “current month” periods?! Current week is described, but I’m really bad with this templating stuff…

This should theoretically give me the current month, right?!

    start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - now().weekday() * 86400 - now().week() * 254800 }}'
    end: '{{ now() }}'

…assuming now().week() * 254800 is correct for “week of the month” … which it appears to not be :thinking:

EDIT: I believe I have found the solution… it’s

    start: '{{ now().replace(day=1).replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

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 :thinking:

EDIT: don’t use this - go down to the solution

Ok, for anyone also looking for a solution, I solved it like this now :slight_smile:

You need a History Stat Sensor to determine the runtime of a device (switch-on-time):

  - platform: history_stats
    name: 'bedroom heater runtime today'
    entity_id: switch.osram_plug_01
    state: 'on'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ 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:

  1. 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…
  2. 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 :thinking:

I think the problem with this solution is that you’ll only get as many days as you have in your database. I think my last comment is a better way.

I was actually pretty sure this is what I did :thinking:

But now I see what you mean… and true that would make a big difference.

But how would I get a current_power reading?!

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.

1 Like

well that would be a LOT more straight forward and simple … if I get the template right :upside_down_face:

I’ll give it a try

I got It to work! :smiley:

For the fake power meter I use:

  - 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 %}

And this for the integration sensor:

  - platform: integration
    source: sensor.bedroom_heater_power
    name: Bedroom Heater Power Consumption
    round: 2

Thanks for the solution @nickrout !

3 Likes

For some weird reason the integration sensor also counts when the plug is off…

The template sensor bedroom_heater_power does give out the right value - when the plug is on it reports 1.50, if it’s off, 0.

But EVERY TIME I toggle the plug, the intergation sensor adds power as if the plug was on all the time.

so:
plug on for 10min results in +0,25kWh (correct)
BUT
plug off for 10min also results in +0,25kWh on the integration sensor…

I have linked the integration sensor with a Utility Meter like this:

bedroom_heater_utility_meter:
  source: sensor.bedroom_heater_power_consumption
  cycle: daily

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?!)

But I can’t figure out how

I guess the problem must be here somewhere

  - 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 %}

It seems to be completely irrelevant what value I put instead of 0 … the integration sensor always uses the 1.50

Even though the bedroom_heater_power sensor does report the correct value. 0 when off and 1.50 when on. It’s really strange…

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 tried that automation, but it doesn’t seem to do anything. The sensor only updates when I toggle the switch.

- id: 'bedroom heater power refresh '
  alias: bedroom_heater_power_refresh 
  trigger:
  - platform: time_pattern
    seconds: '/5'
  action:
  - service: homeassistant.update_entity
    entity_id: sensor.bedroom_heater_power

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?

Yeah - I checked all that.

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 :tipping_hand_woman:

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)