Energy Sankey Chart in Home Assistant

We had solar panels installed at the start of February but we’re still waiting for the accompanying battery to arrive. I put together this Home Assistant dashboard for our Kindle Fire kiosk tablet in the kitchen at home, so we could quickly spot excess solar production (and turn on the washing machine!) – but also to understand where the energy was being consumed in the household.

Most of the elements on the dashboard are Home Assistant energy components, or simple entities and graphs. But I’ve had a few questions about the energy consumption Sankey Chart at the bottom-left.

Data sources:

The Sankey Chart lovelace card is available in HACS.

Zooming in:

Sources

Grid consumption: from smart meter via Hildebrand Glow CAD Zigbee stick, data sent to the Hildebrand cloud service then read back to local MQTT server and into Home Assistant. I know, right? We’ve collectively paid billions for smart meters that could really help people manage their energy consumption. That’s our data. It shouldn’t be this hard.

The zero-carbon data exists within the HA Energy dashboard/integration, but I can’t find a way to access it via sensors or statistics, so I’ve had to reproduce it. This would be much easier if utility_meter supported templates but it doesn’t, so…

First we set up some input_number helpers:

energy_fossil_home:
  name: Home Fossil Energy
  min: 0
  max: 100
  unit_of_measurement: kWh
  mode: box

energy_low_carbon_home:
  name: Home Low Carbon Energy
  min: 0
  max: 100
  unit_of_measurement: kWh
  mode: box

carbon_footprint_home:
  name: Home Carbon Footprint
  min: 0
  max: 20000
  unit_of_measurement: kgCO2eq
  mode: box

Then create an automation which triggers every time the grid consumption sensor updates, to increment the fossil, low_carbon and carbon_footprint helpers using the real-time carbon information from the HA CO2 signal integration:

alias: Home Energy Carbon Split
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.home_energy
condition: []
action:
  - service: input_number.set_value
    data:
      value: >-
        {%- set delta = trigger.to_state.state | float -
        trigger.from_state.state | float -%} {%- set current_energy =
        states("input_number.energy_fossil_home") | float -%} {{ current_energy
        + delta * 0.01 *
        (states("sensor.co2_signal_grid_fossil_fuel_percentage") | float)  }}
    target:
      entity_id: input_number.energy_fossil_home
  - service: input_number.set_value
    data:
      value: >-
        {%- set delta = trigger.to_state.state | float -
        trigger.from_state.state | float -%} {%- set current_energy =
        states("input_number.energy_low_carbon_home") | float -%} {{
        current_energy + delta * 0.01 * (100 -
        (states("sensor.co2_signal_grid_fossil_fuel_percentage") | float))  }}
    target:
      entity_id: input_number.energy_low_carbon_home
  - service: input_number.set_value
    data:
      value: >-
        {%- set delta = trigger.to_state.state | float -
        trigger.from_state.state | float -%} {%- set current_carbon =
        states("input_number.carbon_footprint_home") | float -%} {{
        current_carbon + 0.001 * delta *
        (states("sensor.co2_signal_co2_intensity") | float)  }}
    target:
      entity_id: input_number.carbon_footprint_home

These accumulators are reset to zero at midnight:

- id: '1677545403510'
  alias: Reset Carbon Split Trackers
  description: ''
  trigger:
  - platform: time
    at: 00:00:00
  condition: []
  action:
  - service: input_number.set_value
    data:
      value: 0
    target:
      entity_id:
      - input_number.energy_fossil_home
      - input_number.energy_low_carbon_home
      - input_number.carbon_footprint_home
  mode: single

We need to use template sensors to collect the relevant statistics:

  - name: Home Energy
    unique_id: home_energy
    state: "{{ states('sensor.daily_grid_consumption') | float }}"
    unit_of_measurement: 'kWh'
    state_class: total_increasing

  - name: Home Energy Dirty
    unique_id: home_energy_dirty
    state: "{{ states('input_number.energy_fossil_home') | float }}"
    unit_of_measurement: 'kWh'
    state_class: total_increasing

  - name: Home Energy Clean
    unique_id: home_energy_clean
    state: "{{ states('input_number.energy_low_carbon_home') | float }}"
    unit_of_measurement: 'kWh'
    state_class: total_increasing

  - name: Home CO2
    unique_id: home_carbon_footprint
    state: "{{ states('input_number.carbon_footprint_home') | float }}"
    unit_of_measurement: 'kgCO2eq'
    state_class: total_increasing

and set up some more template sensors to group some of the monitored circuits together:

  - name: Lighting Energy
    unique_id: lighting_energy
    state: "{{ (states('sensor.downstairs_lights_5_1d')| float) + (states('sensor.kitchen_lights_11_1d') | float) + (states('sensor.upstairs_lights_10_1d') | float) }}"
    unit_of_measurement: 'kWh'
    state_class: total_increasing

  - name: Computer Energy
    unique_id: computer_energy
    state: "{{ (states('sensor.attic_sockets_9_1d')| float) + (states('sensor.bedrooms_7_1d') | float) }}"
    unit_of_measurement: 'kWh'
    state_class: total_increasing

  - name: Entertainment Energy
    unique_id: entertainment_energy
    state: "{{ (states('sensor.study_sockets_8_1d')| float) + (states('sensor.living_room_2_1d') | float) }}"
    unit_of_measurement: 'kWh'
    state_class: total_increasing

  - name: Cooking and Washing Energy
    unique_id: cooking_washing_energy
    state: "{{ (states('sensor.kitchen_sockets_1_1d')| float) + (states('sensor.cooker_3_1d') | float) }}"
    unit_of_measurement: 'kWh'
    state_class: total_increasing


  - name: Car Charger Energy
    unique_id: car_charger_energy
    state: "{{ (states('sensor.car_charger_12_1d')| float) }}"
    unit_of_measurement: 'kWh'
    state_class: total_increasing


  - name: Other Energy
    unique_id: other_energy
    state: "{{ (states('sensor.immersion_heater_4_1d')| float) + (states('sensor.hall_6_1d')| float) }}"
    unit_of_measurement: 'kWh'
    state_class: total_increasing

The FoxEss Cloud integration provides foxess_feedin for exported solar energy, and solar production is captured here:

  - name: "Solar Power Daily"
    unique_id: solar_power_daily
    unit_of_measurement: "kWh"
    state: "{{ states('sensor.daily_pv1_sum') | float }}"
    state_class: total_increasing

Power information is coming from three sources – the electricity meter, the inverter cloud service, and circuit-level monitoring using CT clamps with the Emporia Vue cloud service. Errors and timing discrepancies will arise, but I’ve chosen to ignore these on the chart. It’s easier to think of the chart as a breakdown of production (and export) on the left-side of the chart, and consumption just as the sum of the individual monitored circuits, on the right-side. The discrepancy between the two independent approaches produces the small visual step on the “Home” section of the chart.

Full YAML is below:

  - type: custom:sankey-chart
    style: |
      ha-card {
      height: 300px !important;
      }    
    show_names: true
    wide: true
    show_icons: false
    unit_prefix: k
    round: 1
    sections:
      - entities:
          - entity_id: sensor.home_energy_clean
            name: Green
            color: white
            children:
              - sensor.home_energy
          - entity_id: sensor.home_energy_dirty
            name: Fossil
            color: brown
            children:
              - sensor.home_energy
          - entity_id: sensor.solar_power_daily
            name: Solar Production
            color: yellow
            children:
              - sensor.solar_power_daily
      - entities:
          - entity_id: sensor.home_energy
            name: Grid Consumption
            icon: mdi:transmission-tower-import
            children:
              - energy_domestic_consumption
          - entity_id: sensor.solar_power_daily
            type: passthrough
            color: yellow
            children:
              - sensor.foxess_feedin
              - energy_domestic_consumption
      - entities:
          - entity_id: energy_domestic_consumption
            name: Home
            type: remaining_parent_state
            children:
              - sensor.lighting_energy
              - sensor.computer_energy
              - sensor.entertainment_energy
              - sensor.car_charger_12_1d
              - sensor.other_energy
              - sensor.cooking_and_washing_energy
          - entity_id: sensor.foxess_feedin
            name: Solar Export
            color: yellow
      - entities:
          - entity_id: sensor.lighting_energy
            name: Lights
            icon: mdi:lamps
            color: yellow
            children:
              - sensor.downstairs_lights_5_1d
              - sensor.kitchen_lights_11_1d
              - sensor.upstairs_lights_10_1d
          - entity_id: sensor.computer_energy
            name: Computers
            icon: mdi:laptop
            color: blue
            children:
              - sensor.bedrooms_7_1d
              - sensor.attic_sockets_9_1d
          - entity_id: sensor.cooking_and_washing_energy
            name: Cooking/Washing
            icon: mdi:washing-machine
            color: var(--warning-color)
            children:
              - sensor.kitchen_sockets_1_1d
              - sensor.cooker_3_1d
          - entity_id: sensor.car_charger_12_1d
            name: EV
            icon: mdi:ev-plug-type2
            color: orange
            type: passthrough
            children:
              - sensor.car_charger_12_1d
          - entity_id: sensor.entertainment_energy
            name: Entertainment
            icon: mdi:console
            color: red
            children:
              - sensor.living_room_2_1d
              - sensor.study_sockets_8_1d
          - entity_id: sensor.other_energy
            name: Other
            icon: mdi:slot-machine
            color: gray
            children:
              - sensor.immersion_heater_4_1d
              - sensor.hall_6_1d
        sort_by: state
        sort_dir: desc
      - entities:
          - sensor.attic_sockets_9_1d
          - sensor.bedrooms_7_1d
          - sensor.car_charger_12_1d
          - sensor.cooker_3_1d
          - sensor.downstairs_lights_5_1d
          - sensor.immersion_heater_4_1d
          - sensor.kitchen_lights_11_1d
          - sensor.kitchen_sockets_1_1d
          - sensor.living_room_2_1d
          - sensor.study_sockets_8_1d
          - sensor.upstairs_lights_10_1d
          - sensor.hall_6_1d
        sort_by: state
        sort_dir: desc

Comments

Leave a Reply

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