Calculating Average Electricity Rates in Home Assistant

After switching to the Octopus Intelligent Go tariff – which consists of a low night rate for EV charging, and a much higher day rate – I wanted to show the average rate for the day (so far) on my dashboard.

Why is the average rate useful?

The average rate show you how efficiently you used the night rate vs the day rate, and lets you compare against other single rate tariffs like Octopus Tracker.

I never take action based on the average, but it’s very satisfying to see a day coming to an end with an average rate under 15p/kWh!

Solution

The high level solution is as follows:

  • Record the kWh usage for both day and night rates.
  • Get the cost of both day and night rates.
  • Use a template sensor to calculate the total cost for the day (day cost + night cost), and then divide that by the total kWh used.

Steps

1. Add utility meters

A pair of utility meters, each with a daily reset, can be used to track the day and night usage.

From the helpers screen, add a new utility meter and configure two tariffs and a daily reset. You will need an input sensor that is tracking the overall kWh usage (e.g. a smart meter, or CT clamp).

To add the tariffs, type any text into the supported tariffs input and press enter.

After you submit, Home Assistant will create two utility meters and a select entity to switch between them. Whichever meter is “selected” will increment when the input sensor increments. At the end of each day, both meters will reset to zero.

2. Add an automation to control the select entity

For tariffs that are strictly time based the automation can just use time triggers, but Octopus Intelligent Go is more complicated than that, so I monitor the current rate sensor from the Octopus Energy integration, and switch the select when the rate moves above or below 10p/kWh.

alias: Electricity Peak vs Off Peak
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.octopus_energy_electricity_XXX_YYY_current_rate
    below: 0.1
    id: Start Off Peak
  - trigger: numeric_state
    entity_id:
      - sensor.octopus_energy_electricity_XXX_YYY_current_rate
    id: Start Peak
    above: 0.1
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Start Off Peak
        sequence:
          - action: select.select_option            
            data:
              option: Off Peak
            target:
              entity_id: select.electricity_peak_vs_off_peak
      - conditions:
          - condition: trigger
            id:
              - Start Peak
        sequence:
          - action: select.select_option            
            data:
              option: Peak
            target:
              entity_id: select.electricity_peak_vs_off_peak
mode: single

3. Add a template sensor

Finally, add a template sensor:

- name: "Octopus Energy Electricity Average Rate"
  unique_id: "octopus_energy_electricity_average_rate_pence"
  icon: mdi:lightning-bolt
  unit_of_measurement: "p/kWh"
  state: >
    {% set rates = [ 
      states('sensor.octopus_energy_electricity_XXX_YYY_current_rate'),
      states('sensor.octopus_energy_electricity_XXX_YYY_next_rate')
    ] %}

    {% set off_peak_rate = rates|min|float %}
    {% set peak_rate = rates|max|float %}

    {{ 
      (
        (states('sensor.electricity_off_peak_utility_meter') | float * off_peak_rate) +
        (states('sensor.electricity_peak_utility_meter') | float * peak_rate)
      ) / 
      (
        states('sensor.electricity_off_peak_utility_meter') | float + 
        states('sensor.electricity_peak_utility_meter') | float
      ) * 100 
    }}

The calculation in the sensor works as follows:

  1. Create an array that contains the current and next rates from the Octopus Energy integration. For Octopus Intelligent Go, there are two rates and they will switch positions depending on which one is active.
  2. Find the lowest and highest values in the array. i.e. the day (peak) and night (off peak) rates).
  3. Multiply the rates by the consumption recorded in the utility meters and add together to calculate the total cost for the day.
  4. Divide by the total consumption for the day.
  5. Multiple by 100 to convert from £/kWh to p/kWh.