Skip to content

Provides extended features on top of utility-meter to track costs for each tariff as well as total costs

License

Notifications You must be signed in to change notification settings

zeronounours/HA-custom-component-energy-meter

Repository files navigation

Energy Meter

GitHub Release License hacs_badge

icon

The Energy Meter integration provides functionality to track consumptions of various utilities, like the builtin Utility Meter. But on top of it, it adds entities to track costs for each tariffs.

Provides extended features on top of the builtin utility meter and energy sensors to track costs for each tariff as well as total costs. It is possible to achieve the same using templates, but it is long and error-prone to do it for every single energy entity you want to track

Example

Table of Contents

Installation

HACS - preferred

This repository is compatible with HACS. This is the preferred way to install the custom component.

Manual

  1. Download the release zip from releases pages
  2. Copy it within <HA config dir>/custom_components/energy_meter
  3. Unzip in place
  4. Restart Home Assistant

Configuration Variables

All configuration should be added to the configurations.yaml file with the energy_meter domain. No GUI configuration is currently supported.

The configuration of the component is mostly the same as the builtin utility_meter integration: https://www.home-assistant.io/integrations/utility_meter/

Take a look at its configuration variables for your current Home assistant version, as they may differ from the below description.

Utility meter configuration variables


source string Required

The entity ID of the sensor providing utility readings (energy, water, gas, heating).


name string (Optional)

The friendly name to use in the GUI.


unique_id string (Optional)

An ID that uniquely identifies the utility_meter. Set this to a unique value to allow customization through the UI.


cycle string (Optional)

How often to reset the counter. Valid values are quarter-hourly, hourly, daily, weekly, monthly, bimonthly, quarterly and yearly. Cycle value bimonthly will reset the counter once in two months.


offset integer (Optional, default: 0)

Cycle reset occur at the beginning of the period (0 minutes, 0h00 hours, Monday, day 1, January). This option enables the offsetting of these beginnings. Supported formats: offset: 'HH:MM:SS', offset: 'HH:MM' and Time period dictionary (see utility meter configuration page).


cron string Required

This option is mutually exclusive of cycle and offset. It provides an advanced method of defining when should the counter be reset. It follows common crontab syntax but extended to support more advanced scheduling. See the croniter library.


delta_values boolean (Optional, default: false)

Set this to True if the source values are delta values since the last reading instead of absolute values. When this option is enabled, each new value received will be added as-is to the utility meter instead of adding the difference between the new value and previous value. For example, you should enable this when the source sensor returns readings like “1”, “0.5”, “0.75” versus “1”, “1.5”, “2.25”.


net_consumption boolean (Optional, default: false)

Set this to True if you would like to treat the source as a net meter. This will allow your counter to go both positive and negative.


tariffs list (Optional, default: [])

List of tariffs supported by the utility meter.


periodically_resetting boolean (Optional, default: true)

Enable this if the source sensor state is expected to reset to 0, for example, a smart plug that resets on boot. When this option is disabled (for example, if the source sensor is a domestic utility meter that never resets during the device’s lifetime), the difference between the new value and the last valid value is added to the utility meter, which avoids the loss of a meter reading after the source sensor becomes available after being unavailable.


⚠ Warning
When using the offset configuration parameter, the defined period must not be longer than 28 days.

Energy meter specific configuration variables


price float (Optional)

The static price of the tariff (in currency per source unit, e.g. USD/kWh or USD/m³)


price_entity string (Optional)

The entity ID of a sensor giving the current price of the tariff (in currency per source unit, e.g. USD/kWh or USD/m³)


source_type string (Optional, default: from_grid)

The type of energy being followed as source. These are the same as the builtin energy dashboard. It can be of 4 types:

  • from_grid: this is the default. To be used if source tracks grid consumption.
  • to_grid: to be used if source tracks energy returned to grid.
  • gas: to be used if source tracks gas consumption.
  • water: to be used if source tracks water consumption.

create_utility_meter boolean (Optional, default: true)

Whether to create a utility meter for the energy and energy costs. If set to false only the energy cost entity will be created. Defaults to true.


Configuration examples

Basic configuration

The main difference with utility meters are price and price_entity configurations. The former define a static price while the latter points to an entity which may vary with time.

# in configurations.yaml
energy_meter:
  daily_energy:
    source: sensor.energy
    name: Daily Energy
    cycle: daily
    price_entity: sensor.current_energy_price
    tariffs:
      - peak
      - offpeak

  monthly_energy:
    source: sensor.energy
    name: Monthly Energy
    cycle: monthly
    price: 0.20
    tariffs:
      - peak
      - offpeak

Only one of price or price_entity should be given. If both are given, price_entity would have precedence. If none is defined, this integration will act as a basic utility meter, with no cost tracking.

Energy cost type (Gas, Water, Return to grid)

The configuration can contain the optional source_type option to define the type of energy being monitored. The integration supports the same source type as the builtin energy dashboard.

Keep in mind that depending on its value, the allowed units for the source will differ. from_grid and to_grid needs an electrical energy, while gas and water expect a volume.

# in configurations.yaml
energy_meter:
  monthly_gas:
    source: sensor.gas_consumption
    name: Monthly Gas
    cycle: monthly
    price_entity: sensor.current_gas_price
    source_type: gas
    tariffs:
      - peak
      - offpeak

Energy cost sensor only

If the use of utility meter is unwanted and you only want energy costs, it is possible to set option create_utility_meter to false

# in configurations.yaml
energy_meter:
  energy_costs_only:
    name: Energy Costs
    source: sensor.energy
    price: 0.20
    create_utility_meter: false

Using tariff

Tariff are like virtual counter associated to your meter. See the official documentation utility meters to understand them. They are only used to increment a different index on the meter. They won't change price of energy.

Tariffs are usually changed through automation based on time of state from another sensor. The following example shows how to set a different tariff:

# in configurations.yaml
automation:
  - id: switch_peak_offpeak_tariff
    alias: "Switch peak/offpeak tariff"
    initial_state: true
    trigger:
      - platform: time
        at: "05:00:00"
        variables:
          tariff: peak
      - platform: time
        at: "20:00:00"
        variables:
          tariff: offpeak
    action:
      - service: select.select_option
        target:
          entity_id:
            - select.daily_energy
            - select.monthly_energy
            - select.yearly_energy
        data:
          option: "{{ tariff }}"

The following example provides another way to set tariff with a more complex logic:

# in configurations.yaml
automation:
  - alias: Set Energy Meter Rate
    description: "Set Energy Meter Rate"
    trigger:
      - platform: time
        at: "07:00:30"
      - platform: time
        at: "09:00:30"
      - platform: time
        at: "17:00:30"
      - platform: time
        at: "20:00:30"
      - platform: time
        at: "22:00:30"
      - platform: homeassistant
        event: start
    condition: []
    action:
      - service: select.select_option
        data:
          option: >-
            {% set t = now() %} {%- if (( t.hour >= 7 and t.hour < 9 ) or (
            t.hour >= 17 and t.hour < 20 )) and
            is_state('binary_sensor.workday_sensor', 'on') %}
              peak
            {%- elif (( t.hour >= 9 and t.hour < 17 ) or ( t.hour >= 20 and
            t.hour < 22 )) and is_state('binary_sensor.workday_sensor', 'on')
            %}
              shoulder
            {%- else -%}
              offpeak
            {%- endif -%}
        target:
          entity_id:
            - select.daily_energy
            - select.monthly_energy
            - select.yearly_energy
    mode: single

Override utility meter entity_id, icon or precision

Like any other entity, it is possible to override entity_id, icon, display precision and even name through the GUI, as soon as it contains a unique_id:

# in configurations.yaml
energy_meter:
  daily_energy:
    source: sensor.energy
    unique_id: daily_energy_meter
    name: Daily Energy
    cycle: daily
    price_entity: sensor.current_energy_price
    tariffs:
      - peak
      - offpeak

Services

This integration works on top of the builtin Utility Meter. It does not provide additional services, but all builtin services are compatible with it.

Reset

To reset the energy meters, rely on service utility_meter.reset:

service: utility_meter.reset
target:
  entity_id: select.daily_energy

The reset will reset all sensors which relies on the same select, including:

  • the energy sensor of each tariff
  • the energy cost sensor of each tariff

Note: like with builtin utility meter, it is not possible to reset a meter which do not have tariff. Instead, use the service Calibrate

Calibrate

To reset the energy meters, rely on service utility_meter.calibrate:

service: utility_meter.calibrate
data:
  value: "3"
target:
  entity_id: sensor.daily_energy_cost_offpeak