Live Average Carbon Intensity rating with Octopus Home Mini in Home Assistant
Tags: carbonrating, tutorial, homeautomation, energy
Published: 2024-03-20 00:09:20

As mentioned to Adam Jackson on [Decarbonize Software 2023](https://www.youtube.com/watch?v=fGFeHaYtf5A) the other day, here is a quick note on how to create a live consumption carbon intensity rating widget (sorry a bit of a mouthful) on your Home Assistant dashboard.

Pre-requisites:
- You have an instance of [Home Assistant](https://www.home-assistant.io/installation/) already configured
- HACS is already set up on your Home Assistant instance. Otherwise instructions can be found here: https://hacs.xyz/docs/setup/download
- You are signed up to Octopus Energy provider and have requested a [Octopus Home Mini](https://octopus.energy/blog/octopus-home-mini/) - if you are not yet an Octopus Energy customer, feel free to use my referral code [iron-sun-306](https://share.octopus.energy/iron-sun-306) for £50 off your first bill

![Home Mini](https://octoenergy-production-media.s3.amazonaws.com/images/home_mini_banner.width-1200.png)

I was recently really excited to get an [Octopus Home Mini](https://octopus.energy/blog/octopus-home-mini/). Compared to a smart meter that only reports on aggregated readings over 30 minutes blocks, the Home Mini allows for 10 seconds to 1 minute old readings which can significantly help improve smart home automation in the house. Let's face it, there is nearly no automation relevant over 30 minutes old data when it comes to energy. I have solar panels, and in a country like the UK, the sun really comes and goes and automations would need to be based on 1 to 5 minutes averages to stay relevant.


![Octopus Energy's Live Dashboard enabled by Home Mini](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwm31l90plmfw2hkn9x4.png)



Anyway, getting up to 1 minute old consumption carbon rating can be achieve with these simple steps:

## 1. Install CO2 Signal Home Assistant Integration

This is available via the integration tab in Home Assistant.
[CO2 Signal Home Assistant Integration](https://www.home-assistant.io/integrations/co2signal/)

## 2. Install Octopus Integration and enable the Home Mini

This requires the installation of HACS first. 
[Octopus Home Assistant Integration](https://github.com/BottlecapDave/HomeAssistant-OctopusEnergy)

## 3. Set up a template that combines the values from both

### Configuration
In your configuration.yaml, add the following line (to create your templates in a dedicated file - that is neater)

```yaml
template: !include templates.yaml
```

Create that `template.yaml` file (alongside your `configuration.yaml`) and add the following content:

```yaml
template:
  - sensor:
      - name: "Current hourly Electricity Carbon Emission Rating"
        unit_of_measurement: "gCO2eq/h"
        state: >
            {% set currentco2rating = states('sensor.co2_signal_co2_intensity') | float %}
            {% set liveusage = states('sensor.octopus_energy_electricity_22j0484346_1013001886662_current_demand') | float %}
            
            {% set totalRating = currentco2rating * liveusage/1000 %}
            {% if totalRating > 0 %}
            {{ totalRating | round(1, default=0) }}
            {%- else -%}
              0
            {%- endif %}
```

> Note to create your own template, you can use the Template Editor under the template tab of the Developer Tools to test it.

### Dashboard

You can then set up a dashboard with your current consumption or rating using the sensor card, to help you compare your consumption. The sensor card shows both the current value as well as the history over the past configured period (for me it is set to last 24h).
You could also add gauges to create some red/amber/green zones to at a glance understand how you are _currently_ doing.

Here I am starting the battery charging when the power is at its lowest carbon intensity for the day:

![Starting to charge the battery when the power is at its lowest for the day](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1apj2xnwha8far6cepk6.png)

This shows that when the consumption is negative, we are keeping our rating positive to a minimum of zero.

![Defaults to zero when power is negative](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9ngz8n9lqwfffy3ttky.png)

Finally this show charging the car when the power is at its lowest of the day (similar to charging the battery):
![Starting to charge the car when the power is at its lowest for the day](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ys12we78shli9ikybc7i.png)

## Wrap up

Adding your carbon intensity to your dashboard and combining it to your consumption makes it easy to see whether our consumption is [carbon aware](https://learn.greensoftware.foundation/carbon-awareness), i.e. whether it uses more energy cleaner, in other words, when the electricity production comes primarily from renewable energy.


EDIT 26/03/24: Thank you Ronan Glemain (@ronanglemain) for the correction tips, especially around fixing the unit of measure (from gCO2eq/kWh to gCO2eq/h) - Template has been updated though images still currently the original with the mistaken unit