zeronounours / HA-custom-component-energy-meter

Provides extended features on top of utility-meter to track costs for each tariff as well as total costs
MIT License
19 stars 2 forks source link

Add cost for gas / water #32

Closed JohanEikelenboom closed 9 months ago

JohanEikelenboom commented 1 year ago

Just started using this great add-on and until now it works like a charm for electricity. What I also would like is to measure gas and water cost. Both work with the utility meter function, but the costs are not calculated. Both are measured in m³

I also noticed for both gas and water, the unit of measurement is not shown

Roving-Ronin commented 11 months ago

@JohanEikelenboom As most gas retailers bill in either MJ or kWh of 'energy', have you tried creating a template sensor that calculates the MJ from the m3 value?

This can be done in HA, or else get the ESP32 device to do it. Below is the yaml for my ESP that does this.

  - platform: pulse_meter
    name: 'Gas Meter - Flow Rate'
    id: "gas_meter_flow_rate"
    unit_of_measurement: "L/min" # "m³/h"
    state_class: measurement
#    device_class: volume
    icon: "mdi:pump"
    accuracy_decimals: 3
    pin:
      number: 32
      inverted: true
      mode:
        input: true
        pullup: true
    internal_filter_mode: PULSE
    internal_filter: 100ms
    timeout: 3min
    filters:
      - lambda: return (x / 0.1);       # 1 pulse = 10 Litres

    #  Uses the above pulse_meter and adds sensor and calculations for tracking m3
    total:    
      name: 'Gas Meter - Reading (m³)'
      id: gas_meter_reading_m3
      unit_of_measurement: "m³"
      icon: "mdi:counter"
      state_class: "total_increasing"
      device_class: gas
      accuracy_decimals: 3
      filters:
        - lambda: return (x / 100.0);    # 100 pulses = 1,000L = 1m3

    #  Adds the calculation for 'Reading Since Reset' 
      on_value: 
        then:
          - lambda: |-
              static float previous_gas_m3_value = 0.0;
              float current_gas_m3_value = id(gas_meter_reading_m3).state;
              id(gas_total_m3) += current_gas_m3_value - previous_gas_m3_value;
              previous_gas_m3_value = current_gas_m3_value;

   # Converts the above m3 reading, applying the 'correction value' from the retailer 
   # and then multiples by 38.15 to convert from m3 to MJ of energy.
  - platform: template
    name: "Gas Meter - Reading (MJ)"
    id: gas_meter_reading_mj
    unit_of_measurement: "MJ"
    device_class: gas
    state_class: "total_increasing"
    icon: "mdi:counter"
    accuracy_decimals: 3   
    lambda: |-
      return (id(gas_meter_reading_m3).state * 0.9961 * 38.15);

ESP ends up showing:

image

FYI the 'reading' is since last booted / flashed / powered on... whilst the other larger m3 reading is since the device was setup or was last 'reset to default'.

JohanEikelenboom commented 11 months ago

@Roving-Ronin Here in the Netherlands, all utility companies are billing based on m3. I'm using a P1 meter from https://www.homewizard.com/nl/ (fot the non-dutch speakers, this is a device that plugs into my utilitiy meter and is able to read the data).

Roving-Ronin commented 11 months ago

@JohanEikelenboom (cc: @zeronounours )

You'll need to modify it to use m3 instead of MJ, but here's the configuration I just got working with energy_meter costings for daily, quarterly and yearly gas consumption. In Australia we are billed quarterly and the rate we pay per MJ is based on the MJ used in that period, with 3-5 (depending upon retailer) tarrif rates (gets cheaper as you use more). i.e. 0 to 1890 MJ, 1890 to 3750 MJ etc.

Stupidly are physical gas meters count in 0.010m3 (10L) increments, that they have a correction factor applied (think its changes depending upon gas density/pressure? and then we then apply a m3 to MJ multiplier (that sometimes changes according to quality of gas?) to get a final MJ figure. Anyhow I have all this configured in the ESP code, so I get a m3 (since last reboot/rest) and a MJ reading (since last reboot/reset), as per the prior screenshot.

Below code is using the 'Gas Meter - Reading (MJ)' data from the ESP to update HA.

Note: The gas_meter_readout sensor is just something I'm still playing with, trying to run a pulse_meter for the above, whilst also adding a pulse_counter (+ HA helper and automations) so I can hopefully also run a seperate sensor that I can align with the m3 reading on the physical gas meter, to compare readings on it to the bill.

If you can't work out how to convert to m3 usage, post me a info on how often you get billed and how they bill on m3 (i.e. 0-100m3, 101m3 - 500m3 or is it just a flat $0.0xxx per m3) and I can see about converting yaml when I have some time spare.

#  Version: 1.4 - Updated to use the energy_meter integration, and converted to using a gas_price sensor that automatically updates to reflect the current MJ cost
#                 as based upon total MJ consumed for the quarter.
#

template: 
  - sensor:
    - name: "Gas - Price"
      unique_id: gas_price
      unit_of_measurement: "$/MJ"
      state: >
        {## Enter cost per MJ rates below ##}
        {% set rate1 = 0.03608 %}
        {% set rate2 = 0.03058 %}
        {% set rate3 = 0.02810 %}
        {% set rate4 = 0.02728 %}
        {% set rate5 = 0.02420 %}
        {% set gas_consumption = states('sensor.gas_consumed_quarterly_mj') | float %}
        {% if gas_consumption <= 1890 %} {{rate1}}
        {% elif gas_consumption <= 3750 %} {{rate2}}
        {% elif gas_consumption <= 8250 %} {{rate3}}
        {% elif gas_consumption <= 250500 %} {{rate4}}
        {% else %} {{rate5}}
        {% endif %}

# ---------------------------------------------------------------------------
#
# Derived sensor. Gas pulse counter uses an automation to increase 'counter.gas_meter' by 10(L) for each impulse.
# This counter by setting the minimum value to the readout on the physical meter, is sync'ed
# This reading however is taken as a full number, i.e. 05846314 is read as 5,846,314 instead of 05846.314 m3 
# So this template sensor is needed to divide the number by 1,000
#
  - sensor:
    - name: "Gas Meter - Readout"
      unique_id: gas_meter_readout
      unit_of_measurement: "m³"
      device_class: "gas"
      state_class: total_increasing
      state: "{{ (states('counter.gas_meter') | int / 1000 | round(1)) }}"

# ---------------------------------------------------------------------------
# 
#   Energy meter consumption totals in megajoules (mj), as converted from gas meter m3 reading, 
#   with retailer correction factor applied.
#

energy_meter:

  gas_consumed_daily_mj:
    unique_id: gas_consumed_daily_mj 
    name: Gas Consumed Daily mj
    source: sensor.kc868_1_gas_meter_reading_mj
    cycle: daily
    price_entity: &entity-price sensor.gas_price

  gas_consumed_monthly_mj:
    unique_id: gas_consumed_monthly_mj
    source: sensor.kc868_1_gas_meter_reading_mj
    name: Gas Consumed Monthly mj
    cycle: monthly
    price_entity: *entity-price

  gas_consumed_quarterly_mj:
    unique_id: gas_consumed_quarterly_mj
    source: sensor.kc868_1_gas_meter_reading_mj
    name: Gas Consumed Quarterly mj
    cycle: quarterly
    price_entity: *entity-price

  gas_consumed_yearly_mj:
    unique_id: gas_consumed_yearly_mj
    source: sensor.kc868_1_gas_meter_reading_mj
    name: Gas Consumed Yearly mj
    cycle: yearly
    price_entity: *entity-price

# ---------------------------------------------------------------------------

As this is using a sensor that is constantly working out the 'current' price of the gas, there are no tariffs or automations (to update the energy_meter sensors for daily/monthly etc) to get them to update the tariff that is active. So we end up with a sensor for daily/monthly etc that shows the combined price for that calendar period with no seperate rate1/2/3/4/5 tariff sensors, and then having to create a Group sensor to add them add together to get a sum total for the period.

Roving-Ronin commented 11 months ago

@zeronounours running this (above) and works except with the Energy Dashboard. I have the Daily Gas MJ enttity selected and then for the track the cost, I have selected the Daily Gas MJ Cost entity, but whilst this entity is working correctly and shows $1.xxx the Energy Dashboard fails to show this figure, instead showing $0.00 for this entity.

BTW you never responded re, will you be looking to get this great work added into the base HA / added into utility_meter?