Home

Awesome

Sankey Chart Card

A Home Assistant lovelace card to display a sankey chart. For example for energy/power/water consumption.

GitHub Release License hacs_badge

Project Maintenance GitHub Activity

"Buy Me A Coffee"

This card is intended to display connections between entities with numeric state. It is not a general graph card.

Example card

Installation

Install through HACS

Options

NameTypeDefaultDescription
typestringcustom:sankey-chart
autoconfigobjectExperimental. See autoconfig
sectionslistRequired unless using autoconfig. Entities to show divided by sections, see sections object for additional options.
layoutstringautoValid options are: 'horizontal' - flow left to right, 'vertical' - flow top to bottom & 'auto' - determine based on available space (based on the section->min_witdh option, which defaults to 150)
energy_date_selectionbooleanfalseIntegrate with the Energy Dashboard. Filters data based on the energy-date-selection card. Use this only for accumulated data sensors (energy/water/gas) and with a type:energy-date-selection card. You still need to specify all your entities as HA doesn't know exactly how to connect them but you can use the general kWh entities that you have in the energy dashboard. In the future we may use areas to auto configure the chart.
titlestringOptional header title for the card
unit_prefixstringMetric prefix for the unit of measurment. See https://en.wikipedia.org/wiki/Unit_prefix . Supported values are m, k, M, G, T
roundnumber0Round the value to at most N decimal places. May not apply to near zero values, see issue #29
heightnumber200The height of the card in pixels. Only matters while in horizontal layout. Vertical layout height is dynamic based on content
widebooleanfalseSet this to true if you see extra empty space on the right side of the card. This will expand it horizontally to cover all the available space. Only relevant in horizontal mode.
show_iconsbooleanfalseDisplay entity icons
show_namesbooleanfalseDisplay entity names
show_statesbooleantrueDisplay entity states
show_unitsbooleantrueDisplay unit of measurement
min_box_sizenumber3Minimum size of an entity box
min_box_distancenumber5Minimum space between entity boxes
min_statenumber>0Any entity below this value will not be displayed. Only positive numbers above 0 are allowed. The default is to show everything above 0.
throttlenumberMinimum time in ms between updates/rerenders
static_scalenumberState value corresponding to the maximum size (height for horizontal layout and width in vertical) of the card. For example, if this is set to 1000, then a box with state 500 will take up half of its section. If some section exceeds the value of static_scale, the card will dynamically rescale overriding this option. See (#153)
convert_units_tostringIf entities are electricity (kWh) or gas (ft³) usage, convert them to energy (MJ), cost (monetary) or carbon (gCO2). For cost, you must also specify electricity_price and/or gas_price, as well as the monetary_unit of the price(s). For gCO2, all kWh values will be multiplied by the varying grid CO2 intensity, as with the Energy Dashboard.
co2_intensity_entitystringsensor. co2_signal_co2_intensityEntity providing carbon intensity of electricity (gCO2eq/kWh). If you have solar or storage, you may wish to create a template sensor to convert grid CO2 intensity to consumption CO2 intensity.
gas_co2_intensitynumber66.6 g/ft³ or 2352 g/m³Carbon intensity of gas, e.g. in gCO2eq/ft³. Default value depends on locale; units must match those of gas entities.
electricity_pricenumberUnit price of electricity, e.g. in USD/kWh. Automatic conversion does not support varying electricity prices like the Energy Dashboard does.
gas_pricenumberUnit price of gas, e.g. in USD/ft³.
monetary_unitstringCurrency of the gas or electricity price, e.g. 'USD'
sort_bystringSort the entities. Valid options are: 'state'. If your values change often, you may want to use the throttle option to limit update frequency
sort_dirstringdescSorting direction. Valid options are: 'asc' for smallest first & 'desc' for biggest first

Sections object

NameTypeRequirementDefaultDescription
entitieslistRequiredEntities to show in this section. Could be just the entity_id as a string or an object, see entities object for additional options. Note that the order of this list matters
sort_bystringOptionalSort the entities in this section. Overrides the top level option
sort_dirstringOptionaldescSorting direction for this section. Overrides the top level option
sort_group_by_parentbooleanOptionalfalseGroup entities by parent before sorting. See #135
min_widthnumberOptionalMinimum section width in pixels. Only relevant while in horizontal layout

Entities object

NameTypeRequirementDefaultDescription
entity_idstringRequiredEntity id of the sensor
attributestringOptionalUse the value of an attribute instead of the state of the entity. unit_of_measurement and id will still come from the entity. For more complex customization, please use HA templates.
typestringOptionalentityPossible values are 'entity', 'passthrough', 'remaining_parent_state', 'remaining_child_state'. See entity types
childrenlistOptionalList of entity ids (strings or childred objects) describing child entities (branches). Only entities in subsequent sections will be connected. The last section must not contain children:
namestringOptionalentity name from HACustom label for this entity
iconstringOptionalentity icon from HACustom icon for this entity
unit_of_measurementstringOptionalunit_of_measurement from HACustom unit_of_measurement for this entity. Useful when using attribute. If it contains a unit prefix, that must be in latin. Ex GВт, not ГВт
colorstringOptionalvar(--primary-color)Color of the box. Example values: 'red', '#FFAA2C', 'rgb(255, 170, 44)'
color_on_statebooleanOptionalfalseColor the box based on state value
color_limitstringOptional1State value for coloring the box based on state value
color_abovestringOptionalvar(--paper-item-icon-color)Color for state value above color_limit
color_belowstringOptionalvar(--primary-color)Color for state value below color_limit
add_entitieslistOptionalExperimental. List of entity ids. Their states will be added to this entity, showing a sum.
subtract_entitieslistOptionalExperimental. List of entity ids. Their states will be subtracted from this entity's state
tap_actionactionOptionalmore-infoHome assistant action to perform on tap. Supported action types are more-info, zoom, navigate, url, toggle, call-service, fire-dom-event
children_sumobjectOptionalreconcile config. Determines how to handle mismatches between parents & children. For example if the sum of the energy from all rooms shouldn't exceed the energy of the whole house. See #37 and its related issues
parents_sumobjectOptionalreconcile config. Determines how to handle mismatches between parents & children. For example if the sum of the energy from all rooms shouldn't exceed the energy of the whole house. See #37 and its related issues

Children object

NameTypeRequirementDefaultDescription
entity_idstringRequiredEntity id of the child box
connection_entity_idstringOptionalEntity id of the sensor to that determines how much of the parent flows into the child

Reconcile config

NameTypeRequirementDefaultDescription
should_bestringRequiredValid options are 'equal', 'equal_or_less', 'equal_or_more'
reconcile_tostringRequiredWhich value to display in case of inconsistency. Valid options are 'min', 'max', 'mean', 'latest

Entity types

- entity_id: sensor.child_sensor
  type: passthrough
  # Note that passthrough entities have no children as they always connect to their own entity_id in the next section
- entity_id: whatever # as long as it is unique
  type: remaining_parent_state
  name: Other
- entity_id: whatever # as long as it is unique
  type: remaining_child_state
  name: Discrepancy

Autoconfig

This card supports automatic configuration generation based on the HA energy dashboard. It will set default values for some config parameters and populate the sections param. This is meant to show energy data and assumes you have configured your Energy Dashboard in HA. Use it like this:

- type: energy-date-selection # you can put this anywhere you want but it is required for energy dashboard integration
- type: custom:sankey-chart
  # ...any other options
  autoconfig:
    print_yaml: false # optional
    # any additional autoconfig options (listed below)
NameTypeRequirementDefaultDescription
print_yamlbooleanOptionalfalsePrints the auto generated configuration after the card so you can use it as a starting point for customization. It shows up like an error. Don't worry about it.

Examples

Simple

Simple example card

- type: custom:sankey-chart
  show_names: true
  sections:
    - entities:
      - entity_id: sensor.power
        children:
          - sensor.washing_machine_power
          - sensor.other_power
    - entities:
      - sensor.washing_machine_power
      - sensor.other_power

Energy use

Energy example card

- type: custom:sankey-chart
  show_names: true
  unit_prefix: k
  round: 1
  wide: true
  sections:
    - entities:
        - entity_id: sensor.solar
          color: var(--warning-color)
          children:
            - sensor.total_energy
        - entity_id: sensor.grid
          children:
            - sensor.total_energy
        - entity_id: sensor.battery
          color: var(--success-color)
          children:
            - sensor.total_energy
    - entities:
        - entity_id: sensor.total_energy
          children:
            - sensor.floor1
            - sensor.floor2
            - sensor.garage
    - entities:
        - entity_id: sensor.garage
          color: purple
          children:
            - sensor.ev_charger
            - garage_other
        - entity_id: sensor.floor1
          children:
            - sensor.living_room
            - entity_id: sensor.washer
              connection_entity_id: sensor.washer_energy_net
        - entity_id: sensor.floor2
    - entities:
        - entity_id: sensor.ev_charger
          tap_action:
            action: toggle
        - entity_id: garage_other
          type: remaining_parent_state
          name: Other
        - sensor.living_room
        - sensor.washer

Reconcile state

Example config where the state of the children must not exceed their parent. reconcile_to: max means to take the bigger of the two values. In other words, if the sum of the children is more than the state of the parent, the parent state will be set to the sum of its children within the card. reconcile_to: min would do the opposite and reduce the state of the children so they fully fit within the parent. latest just takes whichever value was updated last.

- type: custom:sankey-chart
  show_names: true
  sections:
    - entities:
      - entity_id: sensor.power
        children_sum:
          should_be: equal_or_less
          reconcile_to: max
        children:
          - sensor.washing_machine_power
          - sensor.other_power
    - entities:
      - sensor.washing_machine_power
      - sensor.other_power

You can find more examples and help in the HA forum https://community.home-assistant.io/t/anyone-using-the-sankey-chart-card/423125

Energy Dashboard

This card supports partial Energy dashboard integration. You still need to specify the entities and connections for now. See energy_date_selection option.

Currently this chart just shows historical data based on a energy-date-selection card. It doesn’t know/care if your entities are in the default energy dashboard.

FAQ

Q: Do my entities need to be added to the energy dashboard first?

A: This card doesn't know/care if an entity is in the energy dashboard. Unless you use autoconfig because that relies entirely on the energy dashboard.

Q: How do I get total [daily] energy?

A: There isn’t a general Consumed Energy sensor in the HA Energy dashboard AFAIK. HA calculates it based on all the in/out kWh values. I can’t tell you exactly how to calculate it because it depends on what values you can monitor. Some people already have a Total Consumption sensor, others have a Current Consumption and create an integration sensor from that, etc.

Q: Can I group/sum entities in the chart?

A: The easiest way is to do it with a template sensor in HA. However it can be done in the chart without a new HA entity. If you have an entity with type: remaining_parent_state and it is the only child of its parents, it will just be a sum of all the parents. Similarly if you have an entity with type: remaining_child_state and it is the only parent of all its children, it will be a sum of all the children.

Q: How do I zoom back out after using the zoom action?

A: Tap the same (currently top level) entity again to reset the zoom level.

Q: My (template) sensor doesn't work with energy_date_selection!?

A: Sensors should have a proper state_class in order for statistics to work. Most commonly state_class: total. See https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics

Q: Can you add feature X, so I don't have to create template entities in HA?

A: Most probably no. There is nothing wrong with template entities and they should be the prefered approach instead of duplicating HA functionality in every card. I do make exceptions for often required features like remaining_parent_state but prefer to rely on HA functionality whenever I can.

Development

  1. npm i
  2. npm start
  3. The compiled .js file will be accessible on http://127.0.0.1:5000/ha-sankey-chart.js.
  4. On a running Home Assistant installation add this to your Lovelace resources:
- url: 'http://127.0.0.1:5000/ha-sankey-chart.js'
  type: module