Track Home Power Usage & Cost with Home Assistant

I’ve been tracking the power usage in my home for years. To reduce costs, I wanted to understand what uses power in my house and to reduce electricity waste. This year, after moving my Z-Wave whole-home energy monitoring device natively to Home Assistant, I decided to take power usage monitoring to the next level. Throughout all my efforts I’ve been able to reduce my energy usage by 20% over the last year by:

  • Automating high usage lights.
  • Automating and coordinating my 2 separate HVAC systems to work efficiently.
  • Discovering and eliminating/controlling all “vampire” devices. I control some vampire devices with smart switches. They only have power during the parts of the day when they would be used.
  • Cutting the cord. A couple of Rokus use a lot less energy than the set-top boxes I used to have. Also, I had built up a pretty big and power-hungry MythTV infrastructure that I have since retired.

Read on for how I track realtime and daily home power usage and costs as well as track power usage over time.

Home Assistant Power Usage Graphs
Home Assistant Power Usage Graphs

What I needed to get started tracking power usage

Here are the things I used to track my power usage. You can substitute different items, and I’ve made some suggestions below:

  • Aeotec Home Energy Meter 1E. This is an older Z-wave home energy meter that they don’t make anymore. You will definitely find it cheaper than on Amazon. There are a few other options for newer devices like:
  • You need to know what it takes to install a whole home energy montior. They aren’t hard, just scary to some. Depending on your device, you may need to modify the approach I used for the same graphs and tracking, but you can do it!
  • Access to your utility meter rates. Rates often change with the season and are different for different “blocks” of usage. I came up with a way to handle these changes and still accurately calculate costs.
  • Home Assistant. You can visualize the data using many different systems and automation controllers. This article will focus on how I did it with Home Assistant. If you aren’t using Home Assistant, I highly recommend it. It’s pretty easy to get going.
  • MQTT. If you aren’t using MQTT for your home automation you’re missing out on a powerful and easy-to-use smarthome hub technology. Here’s how I set MQTT up with Home Assistant.

Overview of the problems I had to solve

Since my Home Energy Monitor reports power usage via Z-wave, half of my job was done already. For storing short term historical information, I simply needed to use the recorder functionality of Home Assistant. However, things get more complicated when I store monthly power usage over time or keep track of daily totals. The home energy monitor just reports a continuous amount of kilowatt-hours used (KWh) until you reset the meter to 0. It also reports Watts used at a point in time. But it doesn’t have a daily meter.

READ  6 Places To Get Smart Home Help

The second problem I had to solve was converting usage to dollars (cost). Obviously, the meter has no idea how much electricity costs in my area. It also doesn’t know all the nuances of rates at different times of the year and blocks of usage.

Overview of how I solved the problems

I solved the energy tracking and cost problems by:

The details of my power usage implementation

First, let me give a caveat. This is certainly not the only way to do what I accomplished. I don’t even know that it is the best way (probably isn’t). I do know that it works and gives me all the information I want to track. My goal here is to give an example implementation that you can modify and improve upon for your implementation. Okay, with that said, here we go…

Step 1: Store monthly power usage in a file

One of the things I want to do is keep track of my power usage over time. This will help me notice long term trends and compare to what the power company says I used in case there are errors. There are lots of ways to store the data, but saving to a CSV file monthly is simple and sufficient for my needs.

Home Assistant natively supports this via the notify file platform. To set up the file writing, I added the following to the “notify:” section of my configuration.yaml file:

- platform: file
  name: filenotify
  filename: /home/homeassistant/.homeassistant/energy_usage.csv
  timestamp: true

This allows the notify.filenotify service to write data to the energy_usage.csv file. The “timestamp: true” line tells Home Assistant to timestamp each entry into the file. Now, I just need a monthly automation to write the aggregate power usage to this file:

- id: reset_home_energy_monitor
  alias: Reset Home Energy Monitor
  initial_state: 'true'
  trigger:
    - platform: time
      at: '00:00:00'
  condition:
    - condition: template
      # Change the number here to get whatever day of the month you want.
      value_template: "{{ now().day == 1 }}"
  action:
  - service: notify.filenotify
    data_template:
      message: ", {{ states.sensor.aeon_labs_dsb09104_home_energy_meter_energy.state }}"
  - service: script.reset_hem_kwh

This automation writes the value of the of my home energy sensor (sensor.aeon_labs_dsb09104_home_energy_meter_energy) to the file I specified in the configuration.yaml at midnight on the first day of each month. At the end of this automation I run the reset_hem_kwh script which resets the kWh counter back to 0 to start a new month:

reset_hem_kwh:
  alias: Reset Home Energy Monitor
  sequence:
  - service: zwave.reset_node_meters
    data:
      node_id: 51

The result is an energy_usage.csv file that looks like:

Home Assistant notifications (Log started: 2019-06-01T07:00:00.013334+00:00)
--------------------------------------------------------------------------------
2019-07-01T07:00:00.008753+00:00 , 944.28
2019-08-01T07:00:00.027159+00:00 , 887.46
2019-09-01T07:00:00.012449+00:00 , 873.92
2019-10-01T07:00:00.009933+00:00 , 878.74
2019-11-01T07:00:00.062430+00:00 , 905.13
2019-12-01T08:00:00.020522+00:00 , 923.04

Step 2: Create sensors for daily tracking

The whole home energy monitor reports realtime watt usage and kWh usage since the monitor was reset. It doesn’t keep track of the total usage today or how much was used yesterday. In order to keep track of daily power usage, I created two new MQTT sensors:

- platform: mqtt
  name: "kWh usage yesterday"
  unit_of_measurement: "kWh"
  state_topic: "power/wholehome/yesterday"
  qos: 2
- platform: mqtt
  name: "midnight kWh running total"
  unit_of_measurement: "kWh"
  state_topic: "power/wholehome/midnight"
  qos: 2

To make this work I use MQTT via an automation that runs right before midnight to update the kWh usage for yesterday and to capture what the kWh usage was at midnight:

- id: update_kwh_usage
  alias: Update kWh Usage
  initial_state: 'true'
  trigger:
    - platform: time
      at: '23:59:45'
  action:
  - service: mqtt.publish
    data:
      topic: power/wholehome/yesterday
      payload_template: "{{ (states('sensor.aeon_labs_dsb09104_home_energy_meter_energy') | float - states('sensor.midnight_kwh_running_total') | float) | round(2) }}"
      retain: true
      qos: 2
  - service: mqtt.publish
    data:
      topic: power/wholehome/midnight
      payload_template: "{{ states('sensor.aeon_labs_dsb09104_home_energy_meter_energy') | float }}"
      retain: true
      qos: 2

kWh usage yesterday is equal to the current running kWh total from the whole home sensor minus what that total was at midnight yesterday. Then I update the midnight total for the new day.

READ  How to Create a Graylog Container in Docker

I also created one template sensor in the platform: template sensors section of my configuration.yaml:

kwh_usage_today:
  friendly_name: "kWh usage today"
  unit_of_measurement: "kWh"
  value_template: "{{ (states('sensor.aeon_labs_dsb09104_home_energy_meter_energy') | float - states('sensor.midnight_kwh_running_total') | float) | round(2)}}"

This sensor updates it’s value to the current daily usage in realtime by subtracting the total kWh at midnight from the current kWh. To make sure all this works, I also have to remember to reset the midnight kWh sensor value to 0 when I reset the whole home sensor. This requires me to add a publish command to the reset_home_energy_monitor automation I showed earlier. The final automation looks like:

- id: reset_home_energy_monitor
  alias: Reset Home Energy Monitor
  initial_state: 'true'
  trigger:
    - platform: time
      at: '00:00:00'
  condition:
    - condition: template
      # Change the number here to get whatever day of the month you want.
      value_template: "{{ now().day == 1 }}"
  action:
  - service: notify.filenotify
    data_template:
      message: ", {{ states.sensor.aeon_labs_dsb09104_home_energy_meter_energy.state }}"
  - service: script.reset_hem_kwh
  - service: mqtt.publish
    data:
      topic: power/wholehome/midnight
      payload: 0
      retain: true
      qos: 2

And now I have the following in my Home Assistant frontend:

Home Assistant Power Usage

Along with a couple of charts:

Realtime Power Usage
Daily power usage

Step 3: Find and use power cost rates

At this point, I had good ways of tracking usage, but I also wanted to quantify the costs in real-time. I was able to find my electrical rates online (and in my statements). A couple of complications are that my power company has different rates at different times of the year, and the rates change depending on how much electricity is used. For example, my rate changes after I’ve used 480 kWh in a month, and it also includes a set daily service charge. Effectively, my electrical usage for less than the first block size (currently 480 kWh) is the result of the following equation:

Power cost this month = (First block rate * kWh so far this month) + (Daily Service charge * number of days)

If I’m at more than 480 hours then it’s:

Power cost this month = (First block rate * 480) + (Second block rate * (kWh this month - 480)) + (Daily Service charge * number of days)

My electrical cost so far today, if my electrical use is less than the first block size, can be calculated with the following equation:

Power cost so far today = (First block rate * kWh so far today) + (Daily Service charge)

I can make adjustments to my calculations for power costs today and yesterday depending on how many kWh are in each block. I won’t bore you with all the equations here. The key thing is that these blocks and rates change, and I don’t want to hardcode them into the Home Assistant equations so I created a few input_number fields:

###Inputs for Electricity usage calculations###
first_block:
  name: "First Block Rate per kWh"
  min: 0.0000
  max: 5.0000
  mode: box
  step: .001
end_block:
  name: "End Block Rate per kWh"
  min: 0.0000
  max: 5.0000
  mode: box
  step: .001
daily_service_charge:
  name: "Daily Service Charge"
  min: 0.0000
  max: 5.0000
  mode: box
  step: .001
current_first_block_size:
  name: "Current First Block Size"
  min: 0
  max: 1000
  mode: box
  unit_of_measurement: kWh

These fields allow me to enter the values as they change in the frontend of Home Assistant:

Electrical cost inputs

I can then use these fields in template sensors that automatically calculate costs:

platform: template
    sensors:
      electricity_cost_yesterday:
        friendly_name: "Electricity Cost Yesterday"
        unit_of_measurement: '$'
        icon_template: mdi:currency-usd
        value_template: >-
          {% if states('sensor.kwh_usage_yesterday') | float < states('input_number.current_first_block_size') | float /30 %}
            {{ ((states('sensor.kwh_usage_yesterday') | float) * (states('input_number.first_block') | float) + (states('input_number.daily_service_charge') | float)) | round(2) }}
          {%- else -%}
            {{ (((states('sensor.kwh_usage_yesterday') | float) - states('input_number.current_first_block_size') | float /30) * (states('input_number.end_block') | float) + (states('input_number.daily_service_charge') | float) + (states('input_number.current_first_block_size') | float /30 * (states('input_number.first_block') | float))) | round(2)}}
          {%- endif %}
      electricity_cost_today:
        friendly_name: "Electricity Cost So Far Today"
        value_template: >-
          {% if states('sensor.kwh_usage_today') | float < states('input_number.current_first_block_size') | float /30 %}
            {{ ((states('sensor.kwh_usage_today') | float) * (states('input_number.first_block') | float) + (states('input_number.daily_service_charge') | float)) | round(2) }}
          {%- else -%}
            {{ (((states('sensor.kwh_usage_today') | float) - states('input_number.current_first_block_size') | float /30) * (states('input_number.end_block') | float) + (states('input_number.daily_service_charge') | float) + (states('input_number.current_first_block_size') | float /30 * (states('input_number.first_block') | float))) | round(2)}}
          {%- endif %}
        unit_of_measurement: '$'
        icon_template: mdi:currency-usd
      electricity_cost_month:
        friendly_name: "Electricity Cost So Far This Month"
        value_template: >-
          {% if states('sensor.aeon_labs_dsb09104_home_energy_meter_energy') | float < states('input_number.current_first_block_size') | float %}
            {{ ((states('sensor.aeon_labs_dsb09104_home_energy_meter_energy') | float) * (states('input_number.first_block') | float) + (states('input_number.daily_service_charge') | float)*30) | round(2) }}
          {%- else -%}
            {{ (((states('sensor.aeon_labs_dsb09104_home_energy_meter_energy') | float) - states('input_number.current_first_block_size') | float) * (states('input_number.end_block') | float) + (states('input_number.current_first_block_size') | float * (states('input_number.first_block') | float)) + (states('input_number.daily_service_charge') | float)*30) | round(2)}}
          {%- endif %}
        unit_of_measurement: '$'
        icon_template: mdi:currency-usd

And now my Home Assistant frontend has these costs automatically updated:

Power usage costs

Final thoughts

Now I have realtime and historical information on my electricity usage and costs. As I mentioned earlier, this has helped me to pinpoint and reduce waste and costs. Next up, when I get into something like Grafana, I’ll start making more sophisticated graphs and perhaps notifications. I’m also considering some type of LED display that lets my family watch the costs go up and up so they are more aware.

READ  My Smarthome Evolution: Part 3 - Current State

What projects have you done to monitor power usage? Let me know in the comments or on Twitter!

This page contains affiliate links. If you purchase an item using an affiliate link I will receive a small commission at no cost to you. Affiliates do not influence my recommendations. Read my disclosures for more information.

Share this:

3 thoughts on “Track Home Power Usage & Cost with Home Assistant”

    • In truth, because I didn’t even know about it until you mentioned it :). I’ll look into redoing some of this with that integration. I don’t think it does all I want it to do, but it certainly takes care of a few things. I didn’t even think to look for a component because I kind of built this in pieces over time, and never really looked at it from a whole implementation standpoint. Thanks!

Comments are closed.

Track Home Power Usage & Cost with Home Assistant

by HomeTechHacker time to read: 9 min