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

If using tariffs, there is no total kWh / Cost$ sensor for the Cycle Period #38

Closed Roving-Ronin closed 10 months ago

Roving-Ronin commented 1 year ago

Hi @zeronounours,

Could you please confirm a few configuration issues?

1) If using the 'price_entity' option, is it supposed to support using an input or only a sensor ? i.e. Using the following configuration, attempting to call an input into which I can enter the current electricity per kWh export rate, it remains at $0.00, when the inverters/meter are counting exports.

  energy_meter_exported_daily:
    unique_id: energy_meter_exported_daily
    source: sensor.power_meter_exported
    name: Energy Meter - Exported Daily
    cycle: daily
    price_entity: input_number.electricity_export_rate

I note though if I use 'price: 0.07' instead of calling the input_number, it works.


2) When using the tariffs option, the integration automatically creates sensors for each tariff. i.e. energy_meter_imported_daily will create 3 sensors to track the kWh, for example:

and 3 more sensors, to track the cost, for example:

But there is no sensor that shows the total kWh and cost of the above 3 sets of sensors. i.e. If you wanted to see the total kWh or cost$ for imported power for the day/month/year, you would have to manually add up the peak+shoulder+offpeak (kWh or Cost$ sensors) for that period. Is this the correct function of your integration? If so, is there a way to add amend it so using tariffs will still give a 'sum of all tarrif sensors' sensor?


3) I note that your integration, when using tariffs, also creates 'select' entities that need to be updated to tell the energy_meter sensors if/when a tariff has changed between peak/shoulder/offpeak for instance. If these select entities are not updated you end up with only the first tariff defined being activated and the being the only sensors counting kWh or cost$ for a tarrif period.

For newer users of HA, it would be worth including documentation explaining that users with tariffs will need to include an automation that (assuming the tariffs are based upon time periods) will check the time and then update the select entity to the correct peak/shoulder/offpeak tariff.

For example, using these energy_meters:

#########################
# IMPORT ENERGY METERS
#

energy_meter:

  energy_meter_imported_daily:
    unique_id: energy_meter_imported_daily
    name: Energy Meter - Imported Daily
    source: sensor.power_meter_consumption
    source_type: from_grid
    cycle: daily
    price_entity: sensor.electricity_price
    tariffs:
      - peak
      - shoulder
      - offpeak

  energy_meter_imported_monthly:
    unique_id: energy_meter_imported_monthly
    name: Energy Meter - Imported Monthly
    source: sensor.power_meter_consumption
    source_type: from_grid
    cycle: monthly
    price_entity: sensor.electricity_price
    tariffs:
      - peak
      - shoulder
      - offpeak

  energy_meter_imported_yearly:
    unique_id: energy_meter_imported_yearly
    name: Energy Meter - Imported Yearly
    source: sensor.power_meter_consumption
    source_type: from_grid
    cycle: yearly
    price_entity: sensor.electricity_price
    tariffs:
      - peak
      - shoulder
      - offpeak

#########################
# EXPORT ENERGY METERS
#
  energy_meter_exported_daily:
    unique_id: energy_meter_exported_daily
    name: Energy Meter - Exported Daily
    source: sensor.power_meter_exported
    source_type: to_grid
    cycle: daily
    price: 0.07
#    price_entity: input_number.electricity_export_rate

  energy_meter_exported_monthly:
    unique_id: energy_meter_exported_monthly
    name: Energy Meter - Exported Monthly
    source: sensor.power_meter_exported
    source_type: to_grid
    cycle: monthly
    price: 0.07
#    price_entity: input_number.electricity_export_rate

  energy_meter_exported_yearly:
    unique_id: energy_meter_exported_yearly
    name: Energy Meter - Exported Yearly
    source: sensor.power_meter_exported
    source_type: to_grid
    cycle: yearly
    price: 0.07
#    price_entity: input_number.electricity_export_rate

Would require the following automation to be added, to update based upon the following tariff periods:

automation:
### adjust the time triggers below to match when your standard electricity tariff changes (peak, shoulder, offpeak). Adjust the template in the action block as required to match your plan.
  - 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.energy_meter_imported_daily
            - select.energy_meter_imported_monthly
            - select.energy_meter_imported_yearly
    mode: single

4) Will you be seeking to have this energy_meter included in HA as a 'default' sensor/option, same as utility_meter currently is ?

Thanks.

Roving-Ronin commented 1 year ago

One other thing I forgot, the 'Cost' meters that are auto generated also need to have a default number of decimal places added into the integration. Finding that by default they are being created with 17 decimal places (i.e. 0.30109860000017600 ) instead of 2 / 0.00.

zeronounours commented 1 year ago

Hello @Roving-Ronin,

  1. For your first issue, I had the same during tests. The problem comes from the unit which is used by the energy sensor to add the appropriate multiplicator. You need to have an intermediate template_sensor in between:
    template:
    -  sensor:
      - name: Electricity Export Rate
        state: "{{ states('input_number.electricity_export_rate') }}"
        unit_of_measurement: USD/kWh
        device_class: monetary

    You can take a look at the configuration I use for testing purpose: https://github.com/zeronounours/HA-custom-component-energy-meter/blob/main/tests/server/includes/package.yaml


  1. For you second issue, I've decided not to create a total value sensor because this information is already hold within the source energy sensor and the created cost sensor. To get a view of the daily/monthly/yearly energy and cost I rely on a lovelace plugin I've develop: https://github.com/zeronounours/lovelace-energy-entity-row

For instance, here is my configuration:

# configuration.yaml
energy_meter:
  heating_daily_energy:
    source: sensor.all_heating_energy
      price_entity: sensor.energy_price_real_value_usd_kwh
      cycle: daily
      tariffs:
        - day
        - night

# dashboard
    cards:
      - type: entities
        title: Energy usage
        entities:
          - type: custom:energy-entity-row
            entity: sensor.all_heating_energy
            name: Heaters consumption
          - type: custom:energy-entity-row
            entity: sensor.heaters_energy_energy_price_real_value_usd_kwh_cost
            name: Heaters cost

          - type: 'custom:fold-entity-row'
            head:
              label: Tariff details
              type: section
            padding: 0
            entities:
              - type: custom:energy-entity-row
                entity: sensor.heaters_daily_energy_day
                name: Heaters (Day)
              - type: custom:energy-entity-row
                entity: sensor.heaters_daily_energy_night
                name: Heaters (Night)
              - type: custom:energy-entity-row
                entity: sensor.all_heating_energy
                name: Heaters (Total)

I can add an option to also create a template sensor which would add them all.

I'll answer your following questions later (I don't have enough time right now)

zeronounours commented 10 months ago

I'll update documentation with your example to change tariff.

For now, I'll leave this integration as HACS default integration. I doubt it might be integrated within HA core as its only a kind of wrapper around builtin components.

If this integration becomes popular, I might reconsider it.