DIY Homelab Energy Monitor with HassIO

Just once will not hurt, I’d like to start this week with a lighter topic which might give you an idea for a future DIY initiative: create a Homelab Energy Monitor, with the help of Home-Assistant (HassIO).

Indeed, having a clear report on your lab’s energy cost, may also help to keep peace at home… At the beginning of my Homelab project, one of my concerns was on the energy needed and associated cost.  I wanted an historical view, considering my energy plan (€/kWh) in addition to the different tariffs (peak, off peak). 

As always, Home-Assistant has the solution!

What do you need to get started?

  • Home-Assistant (HassIO) running instance,
  • Power reporting compatible device (my example will use a Z-Wave plug, energy meter enabled),
  • Your electricity supplier’s cost details (price, peak/off-peak).

https://www.home-assistant.io/hassio/installation/

Quick Introduction to Home-Assistant

Home-Assistant (HassIO) is an open-source home automation (& IoT) system powered by a strong community. It runs on multiple systems, on this example, my setup, in a Docker image running on a RPi 3B+.

I won’t go through a complete description of the system, simply because it is really too huge for a single article.  The list of compatible systems is truly indecent and keeps growing every day. However, if some folks are interested by a more “deep dive” topic on how things work with HassIO, let me know in the comments below.

It just works!

Todd Howard

Home-Assistant does not need add-on, or any rocket science skills to start piloting your devices.  It just works, almost out of the box, by activating what is called an “Integration” for each component you want to use.  (Other advanced features are possible with add-ons from the community.)

Integrations include some “guy next door” brands like Belkin WiMO, Chacon Plugs or what I call “mid-range” tech like Z-Wave Relays to professional systems like KNX protocol-based devices.

The major differences between these protocols (Z-Wave, KNX, ZigBee or WiFi/Bluetooth) are statefulness, robustness, ease and price. 

For example, WiFi protocol (used by many supermarket wireless plug) is simply not made for this purpose. WiFi is subject to multiple disruptions, have a high latency,… however, it’s considerably cheaper, it works and can be handy in some situations…

My home setup is using Z-Wave for home automation devices communication, let’s call it the “Data Plane” and WiFi or LAN for devices (smartphones, computers, …) which need to control these devices, our “Control Plane”.

Now, this is exactly where HassIO comes into play.  It acts as “bridge” where you can centralize all the components you need to work with and, most of all, enable them to interact with each other.

For the folks who want to know more: https://www.home-assistant.io/integrations/

To get started: Getting Started with Home Assistant on Raspberry Pi

Z-Wave is a standard (like ZigBee), which can be used by accessory manufacturers. It is robust and relies on a “dedicated” mesh low energy network.

A word about Privacy

With “mister everyone technologies” like Amazon Alexa, Apple HomeKit or Google Home democratising on people’s premises, enabling anyone to setup “Home Automation” devices, comes the question of the privacy. Despite it is a truly important topic, is still very relative for some of the majors tech actors, no need to mention them.

If it’s free, you are the product

“Somebody, not employed by a search engine company”

I think it is a good reason for the techies we are, to build a self-managed private” home-automation solution, keeping it unexposed from internet or from data monetization companies.

Let’s Get it Started!

Homelab Energy Monitor

To begin, I’m using a Fibaro Wall Plug (Z-Wave version) as the entry point to power my Homelab elements, no big deal (3 Intel NUC, 1 Switch).

Of course you can use any other supported component, no matter the protocol, as soon as it reports energy consumption value (depending on the vendor, you may need to adjust the value, Wh, kWh, etc.)

This smart outlet, in addition to its remote-switch capabilities, has a couple of sensors:

Homelab Energy Monitor

Get values from sensor.fibaro_system_fgwpe_f_wall_plug_gen5_energy:

Manually getting state of it:
node_id: 11
value_index: 0
value_instance: 1
value_id: '72057594227490818'
power_consumption: 61.5
unit_of_measurement: kWh
friendly_name: FIBARO System FGWPE/F Wall Plug Gen5 Energy

Home Assistant Configuration

To start, as each configuration task performed into Home-Assistant, we need to edit our main configuration file: configuration.yaml

1. Create History Meters

I used “Utility Meter” platform to create our (6) different meters based on cycle period (last day, last monthly, last yearly) and the power supplier plan (peak or off-peak).

configuration.yaml

# Utility Meter Settings
utility_meter:
    daily_energy:
        source: sensor.fibaro_system_fgwpe_f_wall_plug_gen5_energy
        cycle: daily
        tariffs:
            - peak
            - offpeak
    monthly_energy:
        source: sensor.fibaro_system_fgwpe_f_wall_plug_gen5_energy
        cycle: monthly
        tariffs:
            - peak
            - offpeak
    yearly_energy:
        source: sensor.fibaro_system_fgwpe_f_wall_plug_gen5_energy
        cycle: yearly
        tariffs:
            - peak
            - offpeak

This gives us three objects to work with, daily_energy, monthly_energy and yearly_energy, each of them having two tariffs, “peak” and “offpeak“, so 6 meters.

I used the sensor “sensor.fibaro_system_fgwpe_f_wall_plug_gen5_energy”, coming from the Fibaro Wall Plug, as this guy is reporting the measured energy consumption value in kWh (kiloWatt per hour).

Getting values from sensor.daily_energy_peak history meter we’ve just created (possible after HassIO services restart):

source: sensor.fibaro_system_fgwpe_f_wall_plug_gen5_energy
status: paused
last_period: '0'
last_reset: '2020-04-19T00:00:00.027285+02:00'
meter_period: daily
tariff: peak
unit_of_measurement: kW
friendly_name: daily_energy peak
icon: 'mdi:counter'

This one has a “paused” status and a “0” value, because at the time I’m writing this post, I’m running an off peak plan (sunday).

2. Define Custom Sensors

Next, we need to calculate and store our cost value into sensors we’ll use for reporting. A simple math is made using the (6) meters we’ve defined above and your energy cost, using template and sensor platforms:

In my example, the peak price is 0.1146 € (11.46 eurocents) and the offpeak 0.0883 € (8.83 eurocents), rounded to 2 decimal places.

configuration.yaml

#Custom Sensor Definition
sensor:
  	platform: template
    sensors:
        daily_cost:
            friendly_name: "Daily Cost"
            unit_of_measurement: '€'
            value_template: "{{ (states('sensor.daily_energy_peak') | float * 0.1146 + states('sensor.daily_energy_offpeak') | float * 0.0883) | round(2) }}"
        monthly_cost:  
            friendly_name: "Montlhy Cost"
            unit_of_measurement: '€'
            value_template: "{{ (states('sensor.monthly_energy_peak') | float * 0.1146 + states('sensor.monthly_energy_offpeak') | float * 0.0883) | round(2) }}"
        yearly_cost:  
            friendly_name: "Yearly Cost"
            unit_of_measurement: '€'
            value_template: "{{ (states('sensor.yearly_energy_peak') | float * 0.1146 + states('sensor.yearly_energy_offpeak') | float * 0.0883) | round(2) }}"

This results in 3 sensors, supplied by the calculation between our (6) different meters and associated costs:

  • daily_cost
  • monthly_cost
  • yearly_cost

As each configuration change, a service restart is needed:

ha core restart

Getting state from on them, returns the current value and attributes:

Homelab Energy Monitor

3. A touch of Automation

There’s still a final step needed in order for our values to be relevant. We need to automatically switch between peak and off peak tariffs. This will be realized through another powerful feature from HassIO which is: “Automations“.

My energy plan consist in:

  • A peak tariff every weekdays, from 7am to 10pm
  • An Off peak tariff from every weekday, from 10pm to 7am + Saturday & Sunday

I have defined two automations to match these requirements, using a service provided by “utility_meter” integration which is “select_tariff“:

peak.yaml

alias: Change Utility Tariffs to peak on Weekdays
trigger:
    platform: time
    at: '07:00:00'
condition:
    condition: time
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
action:
    service: utility_meter.select_tariff
    data:
      entity_id:
      - utility_meter.daily_energy
      - utility_meter.monthly_energy
      tariff: peak

offpeak.yaml

alias: Change Utility Tariffs to offpeak on weekdays
trigger:
    platform: time
    at: '22:00:00'
condition:
    condition: time
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
action:
    service: utility_meter.select_tariff
    data:
      entity_id:
      - utility_meter.daily_energy
      - utility_meter.monthly_energy
      tariff: offpeak

Don’t forget to reload the automation (no need to restart the services), Configuration > Server Control > Reload Automations:

Homelab Energy Monitor

4. Let’s make it handy

We are now able to report the values, but this will be more visual with a dashboard. Using Configure UI feature from Home-Assistant web gui, we can:

  • Create a new Manual Card including the following entities (up to you):
Homelab Energy Monitor
  • Create a Gauge Card per custom sensors (Daily, Monthly, Yearly)
Homelab Energy Monitor
Homelab Energy Monitor
  • Finally, create a real time power Card, reflecting the value reported from our initial sensor, sensor.fibaro_system_fgwpe_f_wall_plug_gen5_energy:

I’m using a “dark theme” for HassIO and a HACS community add-on called Mini-Graph-Cards, this is not mandatory, but makes the UI even more modern looking.

Final result:

Final words

This may look tricky for folks who are not familiar with HassIO. But with a little bit of curiosity and a real goal to achieve, the community will make the rest.

With this configuration, I’ve been able to reach my start goal which was to informed on my homelab energy consumption.

Obviously, this setup can suit to any other power report you may need. My next steps will be to include some more plugs and build a “global” dashboard with all those metrics.

If you guys liked this DIY: “Homelab Energy Monitor”, let me know, I will write some other articles the same taste from time to time!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code