Calculate cost of energy, with unpredictable, variable tariff?

Yet another question about variable electricity costs, but I think different to the others…

My electricity tariff (Intelligent Octopus) has an low rate period over night (at the same times every night). However, they occasionally have other low rate periods during the day - depending on how much electricity they have.

The Octopus integration correctly reads the current rate from the API, so I have the “right now price”.

I’ve got a smart switch on our washing machine, dishwasher and EV charger. I have automations which correctly figure out when the appliance starts and stops, and correctly works out the kWh used.

I’m stuck on how to calculate the cost though - since the rate can change mid-way through the usage (perhaps more than once), and possibly at times I’m not expecting, how can I calculate cost?

One idea I had was to have an automation that runs (say) every 5 minutes. It looks to see if an appliance is running. If it is, calculates the consumption over the last 5 minutes, looks up the current price and adds that to a counter. This all seems very clunky though, is there a better way?

Example:

1.

utility_meter:
  washing_machine_energy:
    source: sensor.washing_machine_power
    cycle: daily

Restart HA

/
from the API (example)

sensor.octopus_energy_current_rate
  1. New sensor
template:
  - sensor:
      - name: "Washing Machine Cost"
        unit_of_measurement: "GBP"
        state: >
          {% set price = states('sensor.octopus_energy_current_rate') | float(0) %}
          {% set usage = states('sensor.washing_machine_energy') | float(0) %}
          {{ (price * usage) | round(2) }}

This updates in real-time, avoiding the need for automations

Or

This will calculate the cost based on the kWh used by the washing machine and the current rate of electricity.

template:
  - sensor:
      - name: "Washing Machine Cost"
        unit_of_measurement: "GBP"
        state: >
          {% set price = states('sensor.octopus_energy_current_rate') | float(0) %}
          {% set usage = states('sensor.washing_machine_kwh') | float(0) %}
          {{ (price * usage) | round(2) }}
1 Like

OMG - that’s so easy! (As an OpenHab escapee, I’m maybe over-thinking things!)

I’ve run this just about enough times to see it’s all working correctly (although it’ll get full testing this week!) - I love how HA ‘watches’ things on your behalf and then triggers things for you too - no need to poll things.

For anyone stumbling here to find this, I created a “Utility Meter” for each appliance. It essentially replicates the “power summation” of the smart switch. I don’t use any of the auto-reset, or the electricity tariff features of the Utility Meter.

I have the template sensors as @krskrab suggests above. For example:

template:
  - sensor:
      - name: "Washing Machine Cost"
        unit_of_measurement: "GBP"
        state: >
          {% set price = states('sensor.octopus_energy_electricity_1234_current_rate') | float %}
          {% set usage = states('sensor.washing_machine_electricity_meter') | float %}
          {{ (price * usage) | round(2) }}

I’ve got automations that detect the start and end of an appliance cycle (the trigger is on the smart switch ‘power’ sensor, which is usually zero when it’s switched off, and over 1W when it’s running).

The actions for these ‘start’ automations reset a Utility Meter (you can’t ‘reset’ it, but you can calibrate it to zero). It’s actually possible I could reset the actual smart switch here and not have the Utility Meter resource at all - both would work functionally the same way. Either way, when reset to zero, the ‘cost’ template sensor also resets to zero.

At the end of a cycle, the stop automation’s notification just reads the current value of the Utility Meter for energy consumed, and the current value of the ‘cost’ template sensor for the cost of the energy.

I spoke a bit too soon… This solution does work quite well, but it doesn’t cope with a change of electricity price mid-way through a cycle.

I ran the dishwasher last night, the energy usage is all at the start of the cycle, which was during ‘cheap rate’ which was correctly reflected in the cost calculation at that time. However, the cheap rate ended at 05:30, and the cost calculation jumped up at that point - even though there was no further consumption until the end of the cycle at 6.51.

(if it’s not clear, the consumption was largely flat after about 4am, but price jumped up at 5.30am).

This feels like the template sensor isn’t quite right. Maybe I need some sort of ‘accumilator’ and have the template sensor add increments to it? I’m not sure what’s possible.

I think I’ve solved this - I found Dynamic Energy Cost. It’s maybe a bit rough around the edges, but it specifically counts up cost, using a sensor for cost and a sensor for consumption.

The important point here is that it does accumilate cost, rather than making an instantaneous calculation of it.

I haven’t yet seen it all working with all the notifications and automations, but I did see last night the accumilated cost graph while there was some constant consumption across the peak to low rate. I’ve just put in all the other bits and pieces, so will get to try it all out over the weekend.

I had the first good outcomes last night, with the washing machine running inside the low rate electricity and the dishwasher mostly running on low rate, but finishing in peak rate - both calculated the cost correctly :slight_smile:

For anyone looking at doing the same, the setup I have is:

  • Utility Meter ‘connected’ to a smart plug collecting the kWh consumed for the device (this isn’t strictly necessary if you can reset the smart plug to zero)
  • Dynamic Energy Cost also connected to the smart plug AND connected to the current rate of electricity. This accumulates the cost of the appliance over time, using the actual rate of electricity at the time it is consumed.
  • Automations that detect the start and end of use of the appliance. In my case, triggering ‘start’ when current power usage goes over 5W, and striggering stop when it goes below 5W seems to work
  • The ‘start’ automation ‘calibrates’ the Utility Meter to zero, and reset the ‘manual’ Dynamic Energy Cost sensor to zero. It also sets an input helper to the current time.
  • The ‘stop’ automation uses the current time and the start time to work out how long the appliance was on for. It uses Utility Meter to get the kWh consumed and Dynamic Energy Cost to work out how much that cost. In my case I also get it to send me a Telegram notification with all that information in it, but it’s all saved off in graphs if you prefer.

Thanks krskrab for getting me on the right track here!

1 Like