Monday, August 30, 2021

Energy Dashboard in Home Assistant: Tracking my electricity consumption


 With the release of Home Assistant Core 2021.8, a new feature called Home Energy Management was added to Home Assistant. This provides a nice dashboard where you can track the energy consumption of your house.

I have been interested in capturing my energy consumption data for a while now and have documented my use of an energy consumption meter before. That project was abandoned in due course because I didn't spend any time in building a nice dashboard.

 Since then, my electricity and gas meter has also been upgraded to a  smart meter and I can read the energy consumption on my energy provider, Octopus energy's website. The data is however delayed by a day. It is possible to obtain the latest energy consumption data but it involves third party access to your energy consumption data which I wasn't very keen on. So I have been thinking of capturing the electricity consumption data in some manner and decided to use the energy consumption meter clamp and my RTL-SDR usb dongle.

The post documents my setup.

My Energy Consumption Dashboard

Hardware used: 

1) Efergy Elite Classic:


I just use the transmitter. The clamp goes onto the live wire feeding into the energy meter. The device sends the current flowing through the wire in A every few seconds over the 433.55 MHz frequency.

2) RTL-SDR Realtek RTL2832U + R820T tuner receiver dongle




The RTL=SDR dongle allows us to capture the data being sent over 433.55 MHz frequency.


The dongle is connected to a Raspberry-Pi and is continuously listening to data being sent by the transmitter. To do this, I used the tool rtl_433 available on github. This is run on a screen session on the raspberry pi.




rtl_433 -f 433550000 -R36 -Fmqtt://192.168.1.10:1883,user=mqtt,pass=mqtt

  • The arg -f 433550000 is used to set the frequency to listen on.
  • The arg -R36 sets the decoder to use. This is specific to this transmitter.
  • -Fmqtt://192.168.1.6:1883,user=mqtt,pass=mqtt sets the mqtt server to send the data on.

The command captures the data and send the current consumption in Amperes to the mqtt server which is also my Home Asssitant server.

 The next steps are all on my Home Assistant Server.

I have split my home assistant configuration file so that the sensors configurations is tracked in a separate file. Similarly, I have also split up the configuration for the new Utility Meter integration. I do this by having the following configuration in the configuration.yaml file.

sensor: !include sensor.yaml
utility_meter: !include utility_meter.yaml

If unlike this, you define the sensors platform and the new utility meter integration within the configuration.yaml file itself, then you will have to modify the yaml configuration slightly.

As my first step, I need to create a sensor based on mqtt to read the incoming data in Amperes sent by the rtl_433 tool over mqtt and convert it into Watts for further consumption my homeassistant. My sensor.yaml file contains the following block.
- platform: mqtt
  name: "Home Electricity Watts"
  state_topic: "rtl_433/pi-hole/devices/Efergy-e2CT/42173/current"
  unit_of_measurement: "W"
  value_template: "{{ value | round(2) * 240 }}"
  device_class: energy
Here, the topic is determined by connecting to the mqtt server and subscribing to the topic rtl_433/#. This shows the various topics being posted to by the rtl_433 tool which can be subscribed to. For my specific transmitter, the topic used is rtl_433/pi-hole/devices/Efergy-e2CT/42173 and within this, the topic we are interested in is "current". So accordingly the state_topic used for the mqtt server is "rtl_433/pi-hole/devices/Efergy-e2CT/42173/current"

I also use value_template to multiply the current(I Amps) with the household voltage in the UK(240V) to obtain the energy consumption in Watts. This is exposed on the device sensor.home_electricity_watts.

However this value is just the value of power consumed at that specific time in Watts. To obtain the energy used in kW-hr, we need to multiply the power value with the timeperiod over which it was measured. The kWhr values for each time period need to be summed up to obtain the energy consumption over an hour. We do this using the Integration - Riemann sum integral. in my sensors.yaml file, I have the following yaml block which does this.

- platform: integration                               
  name: home_electricity_kWh
  source: sensor.home_electricity_watts
  round: 3
  method: left
  unit_prefix: k
This block instructs Home Assistant to read data from sensor.home_electricity_watts and use the iteration function to obtain the energy consumption in kWhr. This is then exposed through the device sensor.home_electricity_kWh.

Now we are ready to create a utility meter device. These for me are defined in utility_meter.yaml.

daily_electricity:
  name: "Daily Electricity Usage"
  source: sensor.home_electricity_kWh
  cycle: daily
  tariffs:
    - peak
    - offpeak
weekly_electricity:
  name: "Weekly Electricity Usage"
  source: sensor.home_electricity_kWh
  cycle: weekly
  tariffs:
    - peak
    - offpeak
monthly_electricity:
  name: "Monthly Electricity Usage"
  source: sensor.home_electricity_kWh
  cycle: monthly
  tariffs:
    - peak
    - offpeak
These three devices are similar and only differ in the cycles used ie. daily, weekly and monthly. At the moment, I only make use of the daily_electricity utility_meter. The consume the energy consumption from sensor.home_electricity_kWh and create a utility_meter which can be used by the Energy dashboard.

Since I use the Octopus Go tariff with Octopus energy, I have two different tariff rates depending on the time of the day. I name these tariffs as peak and offpeak. The tariff costs are set in the energy dashboard. These create multiple devices based on the utlity_meter name and tariff. The ones we are interested in are sensor.daily_electricity_peak and sensor.daily_electricity_offpeak.

To add these utility meters to the energy dashboard. Go to Configuration->Energy. Click on Add Consumption. Select "daily_electricity peak", select "Use a static price" and enter your tariff for peak consumption. For Octopus energy go, I pay 0.1533 GBP/kWh. Similarly, add the consumption for the offpeak periods with it's own tariff(0.05 GBP/kWh).

I have also added individual devices to track their energy consumption. This is done using Wifi smart power plugs which also capture energy consumption.

To complete setup, we need to setup automation to select the right tariff. Unlike the examples given on the Home Assistant page, I use two separate automations. These are

alias: 'Energy: Switch Electricity Tariff - peak'
description: ''
trigger:
  - platform: time
    at: '04:30:00'
condition: []
action:
  - service: utility_meter.select_tariff
    target:
      entity_id: utility_meter.daily_electricity
    data:
      tariff: peak
  - service: utility_meter.select_tariff
    target:
      entity_id: utility_meter.weekly_electricity
    data:
      tariff: peak
  - service: utility_meter.select_tariff
    data:
      tariff: peak
    target:
      entity_id: utility_meter.monthly_electricity
mode: single
and
alias: 'Energy: Switch Electricity Tariff - offpeak'
trigger:
  - platform: time
    at: '00:30:00'
action:
  - service: utility_meter.select_tariff
    target:
      entity_id: utility_meter.daily_electricity
    data:
      tariff: offpeak
  - service: utility_meter.select_tariff
    target:
      entity_id: utility_meter.weekly_electricity
    data:
      tariff: offpeak
  - service: utility_meter.select_tariff
    data:
      tariff: offpeak
    target:
      entity_id: utility_meter.monthly_electricity
mode: single
Which uses the call service method to call utlity_meter.select_tariff to select peak/offpeak tariff for each of the utility meters.


This will also expose new devices

  • sensor.daily_electricity_peak - Consumption in peak hours
  • sensor.daily_electricity_offpeak - Consumption in offpeak hours
  • sensor.daily_electricity_peak_cost - Costs during peak hours
  • sensor.daily_electricity_offpeak_cost - Costs for offpeak hours.


You may have to let the system run for a whole cycle before all these devices are visible.

And finally, you can also add a card to my dashboard to track daily electricity costs.



This is specified by the following yaml block.

type: entities
entities:
  - entity: sensor.home_electricity_watts
    name: Current Consumption
  - entity: sensor.daily_electricity_costs
    name: Daily Cost
  - entity: sensor.daily_electricity_peak
    name: Peak Consumption
  - entity: sensor.daily_electricity_peak_cost
    name: Peak Cost
  - entity: sensor.daily_electricity_offpeak
    name: Offpeak Consumption
  - entity: sensor.daily_electricity_offpeak_cost
    name: Offpeak Cost
title: Daily Electricity Costs

The device sensor.daily_electricity_costs is defined by me in my sensor.yaml file with the following entry

- platform: template
  sensors:
    daily_electricity_costs:
      friendly_name: "Energy Costs"
      unit_of_measurement: ".."
      value_template: "{{ (states('sensor.daily_electricity_peak') | float * 0.1533 + states('sensor.daily_electricity_offpeak') | float * 0.05) | round(2) }}"

I have had this system running for the past few days with various settings before finally settling on this setup.
 
The use of the energy consumption meter gives a pretty good approximation of the actual energy consumed. For example according to the energy dashboard, I consumed 8.1 kWhr while the data provided by the energy provider shows that I consumed 7.98 kWhr for the same day. 

I am still on the lookout to capture the actual data from the smart meter itself. Once this is available to me, I can simply change the source of the data consumed and continue with the rest of the setup.

I hope my setup is of use to others looking to setup their energy dashboard on Home Assistant. My thanks to the Home Assistant team for their development efforts which has made set up and management my smart home systems easy and accessible to the rest of us.


Setting a frost alarm on Home assistant

One of the issues with winter is the possibility of ice covering your windscreen which needs to be cleared before you can drive. Clearing ou...