Energy calculation not over time but per state change

I would like to do an energy calculation not over time like PowerCalc, but when the state changes. This means that if a binary sensor goes on, I would like to add x kwh to the energy sensor (template option would be important).

Is there anything ready? Or can I you use PowerCalc?

You can use powercalc - this uses a binary sensor to indicate the boiler is on and the input_number is amount of watts it uses in an hour.

  sensors:
    - entity_id: binary_sensor.gas_boiler
      fixed:
        power: "{{states('input_number.combi_boiler_power_usage')}}"```

Yes I know, but this is over time you have to calculate watt over one hour.
In my case the watt relates from different temperature states + different programs duration.

So it would be easier, binary sensor on → Add x kwh.

Instead of:
temperature a → x kwh / program duration
temperature b → x kwh / program duration

Currently I’m not sure whether I have the duration as a sensor

That doesn’t really make sense; you’re not going to have an instantaneous increase in kWh.

It doesn’t need to be an hour, but it does need to be over some time period. Not knowing exactly what you’re doing makes it tricky, but it seems like this should be possible either natively in Powercalc or by using an automation and some helpers.

Actually the application is simple: I have a binary sensor and I know exactly how much power it consumes depending on the setting (temperature) as soon as it turns on. I know the power consumption directly from the temperature. But when the device is switched on, you can evaluate the temperature and know the power consumption until it is switched off again. The duration doesn’t matter.

Based on what you’re describing, I’d probably go with a template sensor (and probably use a trigger of your switch being turned on). I’ve never had a need for a trigger template sensor, so I can’t help you off the top of my head. If that seems tricky, honestly even an automation with a helper number should get you what you’re after.

1 Like

Thanks for your assessment. If I had the program duration as a sensor, I would simply have to divide duration by that, right?

 sensors:
    - entity_id: binary_sensor.xyz
      fixed:
        power: "{{1100 / duration}}"

I see you just want to increase an energy sensor (kWh) with X amount whenever a state changes.
You could create a template sensor with correct device_class and increase that with a service.

Or use powercalc daily fixed energy sensor. Daily fixed energy — Powercalc documentation
Set it to 0 per day and just use the powercalc.increase_daily_energy service from an automation to increase it with the amount you want.
Your trigger in automation would be state.

1 Like

Ok I will try that thanks, when the division is not working.

I have a similar need in my environment, but I also want to decrease the value of another sensor when I increase the device sensor.

IE, the dishwasher completed it’s cycle, add 2.5 kWh to the Dishwasher Energy sensor, but I also want to remove those 2.5 kWh from the Unaccounted Energy sensor that is calculated based on power usage of multiple devices subtracted from the total power consumption of the house.

The service doesn’t seem to like a negative value since I get an error when I try to run it

service: powercalc.increase_daily_energy
metadata: {}
data:
  value: "-2.5"
target:
  entity_id: sensor.unaccounted_power_energy_daily

It makes sense since the function is to specifically add, but, how can I subtract?