Calculate device energy costs using dynamic electricity price like Nordpool

One challenge many of us face is accurately calculating the energy cost of individual devices when dealing with dynamic electricity prices that change throughout the day. In places like Estonia, we have the Nordpool price, which updates every hour, making traditional tracking methods less effective.

The Problem

The Energy Dashboard in Home Assistant, while powerful, presents a challenge for users looking to optimize their energy tracking:

  • Compromise Between Device-Specific Tracking and Overall Grid Consumption: The dashboard facilitates tracking individual devices with dynamic electricity prices like Nordpool, offering insights into each device’s energy costs. However, incorporating individual devices with their specific costs means you cannot use the dashboard to monitor overall grid consumption as originally intended. This setup restricts users from having a unified view of both detailed device-level consumption and total household energy usage, forcing a choice between one or the other.
  • Lack of Integrated Price Calculation for Devices: Although it’s possible to monitor specific devices, the dashboard does not natively calculate or display the cost impact of each device based on dynamic pricing.
  • Utilization of Gas Consumption Tracking: Some users might repurpose the gas consumption feature to track electricity, but this is a makeshift solution that can compromise the tracking of actual gas consumption.
  • Limited External Use of Data: Data gleaned from the Energy Dashboard is not easily repurposed for other uses within Home Assistant, such as creating custom templates or additional dashboards, limiting the flexibility in how energy data can be analyzed and acted upon.

These limitations highlighted the need for a solution that offers more flexibility and precision in tracking device-specific energy costs, especially under the conditions of fluctuating electricity prices.

Why I Did This

To address these challenges, I devised a method that bypasses these limitations and provides a reliable way to calculate and track the energy cost of any device, considering the dynamic pricing of electricity.

The Solution

You have two main options for achieving detailed device energy cost tracking with dynamic electricity pricing:

  1. Automation with Input Helpers: This method involves creating automations that read each hour’s energy consumption and corresponding price, calculating the cost, and updating an input helper. However, this approach can be complex and fragile, particularly if a system restart interrupts the automation.
  2. Integration Sensor Method (My Approach): Utilizing an integration sensor with the left method offers a more stable and straightforward solution.

Features

  • Real-time Cost Calculation: Dynamically computes the energy cost of any device based on the current electricity price and the device’s power consumption.
  • Cumulative Cost Tracking: Continuously aggregates the energy costs over time, providing a comprehensive total of expenses.
  • Periodic Cost Breakdowns: Leverages utility meters to systematically segment and track energy costs on a daily, monthly, and yearly basis.

Prerequisites

  • Electricity Price Sensor: A sensor (sensor.total_el_price) that reports the current electricity price in EUR/kWh, ideally reflecting dynamic prices from sources like Nord Pool.
  • Device Power Consumption Sensor: A sensor that measures the power consumption of the device in question, reporting in Watts (W). For the example provided, this would be an EV charger power consumption sensor (sensor.ev_charger_power).
  • Integration Platform: Utilized within Home Assistant to calculate cumulative costs over time effectively.
  • Utility Meter Component: Essential for breaking down the cumulative cost into detailed daily, monthly, and yearly summaries.

Example Application: EV Charger

This setup is demonstrated with an EV charger to showcase how you can apply these principles to track and manage the energy costs of charging your electric vehicle. However, the methodology is versatile and can be adapted to monitor the energy costs of any powered device in your home, allowing for greater control and optimization of your energy expenses.

Configuration.yaml

Here’s the core configuration for my approach:

sensor:
  - platform: template
    sensors:
      ev_charger_cost_right_now:
        friendly_name: "EV Charger Cost Right Now"
        unit_of_measurement: "EUR"
        value_template: >
          {% set total_el_price = states('sensor.total_el_price') | float(default=0) %}
          {% set ev_charger_power = states('sensor.ev_charger_power') | float(default=0) %}
          {{ (total_el_price * ev_charger_power / 1000) | round(2) }}

  - platform: integration
    name: ev_charger_cost_cumulative
    source: sensor.ev_charger_cost_right_now
    method: left

utility_meter:
  ev_charger_cost_daily:
    source: sensor.ev_charger_cost_cumulative
    name: EV Charger Cost Daily
    cycle: daily
  ev_charger_cost_monthly:
    source: sensor.ev_charger_cost_cumulative
    name: EV Charger Cost Monthly
    cycle: monthly
  ev_charger_cost_yearly:
    source: sensor.ev_charger_cost_cumulative
    name: EV Charger Cost Yearly
    cycle: yearly

Installation Instructions

  1. Ensure you have sensors in place for monitoring electricity prices and the power consumption of your device.
  2. Add the provided YAML configuration to your configuration.yaml file in Home Assistant.
  3. Adjust the sensor.total_el_price and sensor.ev_charger_power entity IDs to match those in your Home Assistant setup.
  4. Restart Home Assistant to apply the changes.

Result

You have three entities:

  • EV Charger Cost Daily
  • EV Charger Cost Monthly
  • EV Charger Cost Yearly
12 Likes

I’m reaching out to see if there are developers in our community interested in collaborating to bring this concept to life as a custom component for Home Assistant, accessible through HACS.

  • The aim is to create a component that allows users to easily link their devices with live electricity pricing (e.g., Nordpool rates) to monitor running costs directly within Home Assistant’s UI.
  • Cost entities (daily, monthly, yearly and total) sould be under the device so it would be easier overview and structure
  • Ideally, this component would automatically detect power consumption entities and suggest setting them up for cost tracking, offering detailed insights into energy expenses. Like PowerCalc is doing for devices they have data how to calculate power usage.
6 Likes

Hi Arva,

Thanks for the nice blog.
I’ve created this also for my own environment with also an EV charger with spot prices.

My EV charger has an sensor for the current charge kWh.

Template.yaml

# EV Car Charger Costs
- sensor:
    - name: "EV Charger Cost Current"
      unique_id: "ev_charger_cost_current"
      icon: mdi:cash
      unit_of_measurement: "EUR"
      state: >
        {% set total_el_price = states('sensor.electricity_price') | float %}
        {% set ev_charger_power = states('sensor.ev_charger_energy_usage')| float %}
        {{ (total_el_price * ev_charger_power) | round(2, default=2) }}

The following sensor.yaml is created:

# EV Car Charger Costs
- platform: integration
  name: "EV Charger Cost Cumulative"
  source: "sensor.ev_charger_cost_current"
  unique_id: "ev_charger_cost_cumulative"
  method: left
  round: 2

And as least, the following utility_meter.yaml file has created:

# Utility Meter Helper for EV Charger Cost
ev_charger_cost_daily:
  source: "sensor.ev_charger_cost_cumulative"
  name: "EV Charger Cost Daily"
  unique_id: "ev_charger_cost_daily"
  cycle: daily
ev_charger_cost_monthly:
  source: "sensor.ev_charger_cost_cumulative"
  name: "EV Charger Cost Monthly"
  unique_id: "ev_charger_cost_monthly"
  cycle: monthly
ev_charger_cost_yearly:
  source: "sensor.ev_charger_cost_cumulative"
  name: "EV Charger Cost Yearly"
  unique_id: "ev_charger_cost_yearly"
  cycle: yearly

As test I have created in my dashboard an entities card with above values. I see that the sensor “EV Charger Cost Current” works fine. That is correct.
But the “EV Charger Cost Cumulative” is incorrect. It looks like that the value has been submit as 1/10. Do you have any solution for that?

I’ve tested several thinks, but that is the only thing that is not working well.
The only difference is that you have an EV sensor with W instead of kWh for me.
Als the EV Charger Cost [Daily, Monthly, Yearly and Cumulative] are all showing with EURh.

Thanks for the nice blog and I hope that you have the solution for me.

Hi!

For this method to work you need to feed in Power entity (W) not Energy entity (kwh).

Hi Arva,

Thanks for the reply. My chargerpoint has no Power Entity (W) data. Except the Energy entity (kWh). I will look for another solution for me.
I hope that Home Assistant will fix this asap in the energy dashboard. The framework is already created, but we need only to extend it to individual devices.

1 Like

There is a way how to achieve this with a kwh sensor as well using input helpers and automations but it’s quite complex.

I will copy ChatGPT answer, because i’m a bit lazy writer:

To accurately calculate the daily cost of EV charging with a varying energy price, you need a solution that continuously tracks the hourly energy consumption and multiplies it by the corresponding hourly energy price. This can be achieved through a combination of automation and scripting within Home Assistant. Here’s a general approach:

  1. Hourly Energy Consumption Tracking:

    • Create a new sensor or a set of sensors to log the energy consumed every hour. This can be done by measuring the difference in sensor.circuit_10_daily_energy at the beginning and end of each hour.
    • Store these hourly values in either a set of sensors or a helper entity (like an input_number).
  2. Hourly Price Tracking:

    • You already have sensor.nordpool_kwh_ee_eur_2_10_02 for price. Ensure this sensor updates every hour to reflect the latest price.
  3. Calculation Automation:

    • Create an automation that triggers every hour. This automation should:
      • Calculate the cost for that hour by multiplying the hourly energy consumption (from step 1) by the current energy price (from sensor.nordpool_kwh_ee_eur_2_10_02).
      • Add this hourly cost to a daily total. You can store this daily total in another helper entity or a dedicated sensor.
  4. Daily Reset:

    • At the end of each day, reset the daily total and hourly consumption sensors/helpers to start fresh calculations for the next day.
  5. YAML Configuration Example:
    Here is a simplified YAML configuration example:

    automation:
      - alias: "Hourly EV Charging Cost Calculation"
        trigger:
          - platform: time_pattern
            hours: "/1"
        action:
          - service: input_number.set_value
            entity_id: input_number.hourly_energy
            data_template:
              value: "{{ (states('sensor.circuit_10_daily_energy') | float - state_attr('input_number.hourly_energy', 'previous_value') | float) }}"
          - service: input_number.set_value
            entity_id: input_number.daily_cost
            data_template:
              value: "{{ (state_attr('input_number.daily_cost', 'value') | float + (state_attr('input_number.hourly_energy', 'value') | float * states('sensor.nordpool_kwh_ee_eur_2_10_02') | float)) }}"
          - service: input_number.set_value
            entity_id: input_number.hourly_energy
            data_template:
              attribute: previous_value
              value: "{{ states('sensor.circuit_10_daily_energy') | float }}"
    
      - alias: "Reset Daily Totals"
        trigger:
          - platform: time
            at: "00:00:00"
        action:
          - service: input_number.set_value
            entity_id: input_number.daily_cost
            data: 
              value: 0
          - service: input_number.set_value
            entity_id: input_number.hourly_energy
            data: 
              value: 0
    
    • input_number.hourly_energy stores the amount of energy consumed each hour.
    • input_number.daily_cost accumulates the daily cost.
    • These are just basic templates; they might need adjustments to fit your exact setup and requirements.

This approach will provide you with a continuous calculation of the EV charging cost, accounting for hourly changes in energy price. You can further customize this setup based on your specific needs and the capabilities of your Home Assistant setup.

I’m struggling with same issue as you. It would be great to get the price calculation work also with energy (kwh). Please, let us know if you find good solution.

How would this work if you had solar panels or home batteries? Wouldn’t you run into problems with the energy dashboard, given the fact you only have hourly aggregates available and you don’t know how much energy was actually consumed from the grid per device?

I’m excited to share the first version of my custom Home Assistant integration, designed to seamlessly calculate energy costs based on your device’s power consumption and electricity rates. This integration is perfect for anyone looking to get a detailed insight into their energy usage and costs.

Features

  • Real-time Energy Cost: Tracks the cost of energy consumption in real-time (€/h).
  • Cumulative/Total Cost: Accumulates the total cost over time.
  • Daily, Monthly, & Yearly Costs: Breaks down energy costs into daily, monthly, and yearly totals.

Screenshots

Real-time Energy Cost
Cumulative Cost Overview

Getting Started

To start using this integration, simply add the power entity of your device and the electricity price entity to the configuration. The integration handles the rest, calculating various cost metrics based on your inputs.

Repository

You can find the code and more details on how to install and use this integration on GitHub:
Dynamic Energy Cost Integration

Note

This project is currently in its early beta phase. We encourage early adopters to use it cautiously and share feedback for further improvements.


2 Likes

This is great. I will install and test it. Let you know the results

1 Like

Thank you. I also will give it a try.

1 Like

UPDATE:

Version 0.2.0 (still very early stage) is up.

Now you can input energy (kwh) sensor and electricity price sensot to the integration like you do with energy dashboard, and get daily, monthly, and yearly cost sensors.

I left the possibility to calculate cost from power sensor, but it’s not that precise as fom energy sensor. Only if you don’t have energy sensor for the device you want to calculate cost, use power sensor.

As said, it’s a early beta and its my first custom integration, first python development and probably my first ever proper development project

1 Like

Hi Arva

I have a problem with daily values. It resets the value to zero during the day and starts counting from zero.
Any recommendation ?

I think i fixed it in new release: Release 0.2.4 · martinarva/dynamic_energy_cost · GitHub

Thanks, i will test it

1 Like

Hi Arva,

This looks great! I’ve installed your integration and I’m testing on this moment. If I have some feedback, I will react on this topic.

Thanks for the efford and integration!

1 Like

I did a separate topic for the custom integration: Dynamic Energy Cost

This looks very useful.

I have 1s power data and have been creating lovely views of my accumulated cost with time of use for quite a while.

Recently I got a battery and switched to dynamic pricing, both for import and export. I’ve got entities that report the price from API for both.

Could this be updated to support both load and export price accumulation? Unless it does this already and I didn’t pick up on it.

Well, never mind the above. I can just create two different groups of sensors from the same integration. How neat.

Thanks for making this.

Now just to rebuild my dashboard views…