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

Cost sensors are created with unit_of_measurement as suffix, instead of as a prefix #70

Open Roving-Ronin opened 7 months ago

Roving-Ronin commented 7 months ago

Version of the custom_component

v1.1.0

-->

Configuration

# Updated 1 July 2023 for new Red Energy Rates
# Updated 1 October 2023 for new Red Energy Rates
# Updated 4 January 2024 to replace static FIT rate with a sensor that allows for use with variable FIT providers

template:
  - sensor:

#  Current_Price_RedEnergy_TOU provides a backup method to allow selecting the 'Use an entity with current rate' in Energy Dashboard.
#  If you set the prices here, remember to ALSO set them in the 'electricity_import_rate_???' and daily supply charge sensors.

    - name: "Electricity - Price" 
      unique_id: electricity_price
      unit_of_measurement: "$/kWh"
      device_class: monetary
      state:  "
        {%set Peak = 0.41745 %}
        {%set WeekdayShoulder = 0.35695 %}
        {%set OffPeak = 0.26928 %}
        {%set H = as_timestamp(now())|timestamp_custom ('%-H')|float(0) %} {##Hour (24-hour clock) as a decimal number. 0, 1, 2, ... 23##}
        {%set w = as_timestamp(now())|timestamp_custom ('%w')|float(0) %} {##HWeekday as a decimal number, where 0 is Sunday and 6 is Saturday. 0, 1, …, 6##}    
        {##0 for weekday, 1 for weekend##}
        {% if (w == 0) or  (w == 6) %} {% set weekday = 0 %}
        {% else %} {% set weekday = 1 %}
        {% endif %}
        {% set charge = 0 %}
        {##peak##}
        {% if (weekday == 1) and (H >= 7) and (H < 9) %} {{Peak}}
        {% elif (weekday == 1) and (H >= 17) and (H < 20) %} {{Peak}}
        {##weekday shoulder##}
        {% elif (weekday == 1) and (H >= 9) and (H < 17) %} {{WeekdayShoulder}}
        {% elif (weekday == 1) and (H >= 20) and (H < 22) %} {{WeekdayShoulder}}
        {##off peak##}
        {% else %} {{OffPeak}}
        {% endif %}
        "

#  Current FIT rate, rate 1 & 2 allows setting a second rate if variable FIT rates (i.e. $0.15 for first 10kWh, then $0.07 thereafter)

    - name: "Electricity - FIT"
      unique_id: electricity_fit
      unit_of_measurement: "$/kWh"
      device_class: monetary
      icon: mdi:cash-plus
      state: >
        {## Enter compensation per kWh rates below ##}
        {% set rate1 = 0.07 %}
        {% set rate2 = 0.07 %}
        {% set fit_exported_today = states('sensor.hs_grid_exported_daily') | float %}
        {% if fit_exported_today <= 10 %} {{rate1}}
        {% else %} {{rate2}}
        {% endif %}

####################################
# IMPORT UTILITY METERS
# See: https://github.com/zeronounours/HA-custom-component-energy-meter

# utility_meter:
energy_meter:

  hs_grid_imported_daily:
    unique_id: hs_grid_imported_daily
    name: HS Grid - Imported Daily
    source: sensor.power_meter_consumption
    source_type: from_grid
    cycle: daily
    price_entity: &electricity-price sensor.electricity_price
    tariffs:
      - peak
      - shoulder
      - offpeak

  hs_grid_imported_monthly:
    unique_id: hs_grid_imported_monthly
    name: HS Grid - Imported Monthly
    source: sensor.power_meter_consumption
    source_type: from_grid
    cycle: monthly
    price_entity: *electricity-price
    tariffs:
      - peak
      - shoulder
      - offpeak

  hs_grid_imported_yearly:
    unique_id: hs_grid_imported_yearly
    name: HS Grid - Imported Yearly
    source: sensor.power_meter_consumption
    source_type: from_grid
    cycle: yearly
    price_entity: *electricity-price
    tariffs:
      - peak
      - shoulder
      - offpeak

#########################
# EXPORT UTILITY METERS
# See: https://github.com/zeronounours/HA-custom-component-energy-meter

  hs_grid_exported_daily:
    unique_id: hs_grid_exported_daily
    name: HS Grid - Exported Daily
    source: sensor.power_meter_exported
    source_type: to_grid
    cycle: daily
    price_entity: &fit-rate sensor.electricity_export_rate

  hs_grid_exported_monthly:
    unique_id: hs_grid_exported_monthly
    name: HS Grid - Exported Monthly
    source: sensor.power_meter_exported
    source_type: to_grid
    cycle: monthly
    price_entity: *fit-rate

  hs_grid_exported_yearly:
    unique_id: hs_grid_exported_yearly
    name: HS Grid - Exported Yearly
    source: sensor.power_meter_exported
    source_type: to_grid
    cycle: yearly
    price_entity: *fit-rate

#########################
#
# GROUP - TO SUM UP THE DAILY / MONTHLY / YEARLY IMPORT KWH and $ SENSORS
#         TO GIVE A TOTAL OF EACH
#

sensor:

# DAILY
  - platform: group
    name: HS Grid - Imported Daily - Total kWh
    unique_id: hs_grid_imported_daily_total_kwh
    device_class: energy
    type: sum
    entities:
      - sensor.hs_grid_imported_daily_peak
      - sensor.hs_grid_imported_daily_shoulder
      - sensor.hs_grid_imported_daily_offpeak

  - platform: group
    name: HS Grid - Imported Daily - Total Cost
    unique_id: hs_grid_imported_daily_total_cost
    unit_of_measurement: '$'
    device_class: monetary
    type: sum
    entities:
      - sensor.hs_grid_imported_daily_cost_peak
      - sensor.hs_grid_imported_daily_cost_shoulder
      - sensor.hs_grid_imported_daily_cost_offpeak

# MONTHLY
  - platform: group
    name: HS Grid - Imported Monthly - Total kWh
    unique_id: hs_grid_imported_monthly_total_kwh
    device_class: energy
    type: sum
    entities:
      - sensor.hs_grid_imported_monthly_peak
      - sensor.hs_grid_imported_monthly_shoulder
      - sensor.hs_grid_imported_monthly_offpeak

  - platform: group
    name: HS Grid - Imported Monthly - Total Cost
    unique_id: hs_grid_imported_monthly_total_cost
    unit_of_measurement: '$'
    device_class: monetary
    type: sum
    entities:
      - sensor.hs_grid_imported_monthly_cost_peak
      - sensor.hs_grid_imported_monthly_cost_shoulder
      - sensor.hs_grid_imported_monthly_cost_offpeak

# YEARLY
  - platform: group
    name: HS Grid - Imported Yearly - Total kWh
    unique_id: hs_grid_imported_yearly_total_kwh
    device_class: energy
    type: sum
    entities:
      - sensor.hs_grid_imported_yearly_peak
      - sensor.hs_grid_imported_yearly_shoulder
      - sensor.hs_grid_imported_yearly_offpeak

  - platform: group
    name: HS Grid - Imported Yearly - Total Cost
    unique_id: hs_grid_imported_yearly_total_cost
    unit_of_measurement: '$'
    device_class: monetary
    type: sum
    entities:
      - sensor.hs_grid_imported_yearly_cost_peak
      - sensor.hs_grid_imported_yearly_cost_shoulder
      - sensor.hs_grid_imported_yearly_cost_offpeak

#######################
#
# Checks the time and based upon TOU rate times, updates the 'hs_grid_imported_daily/monthly/yearly' entities to use Peak / Shoulder / OffPeak tariff
#

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 Electricity Tariff
    description: 'Set Electricity Tariff (Huawei Solar) depending upon the day of week and time'
    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:
#  Selectors used by utility_meter / energy_meter
            - select.hs_grid_imported_daily
            - select.hs_grid_imported_monthly
            - select.hs_grid_imported_yearly
    mode: single

#######################

Describe the bug

Hi @zeronounours / Gauthier,

Whilst the 'cost' sensor that energy_meter creates/uses correctly shows the sensor value with the $ (or as an Australian user in my case, the A$ prefix), as per:

image

I note that by default any energy_meter sensors are being created with a suffix of the currency (AUD), such as:

image

Is there any way to get the energy_meter to create these sensors (tariff rates in this case) with the correct $ or even A$ prefix ?

Assuming this a 'bug' (more of an overlooked situation), same recently added feature for 'source_type', would there be a way to add a cost_sensor or is_currency feature (default: no) so when you set it to 'is_currency: yes' it will check the default currency sign of the HA installation (i.e. $ / £ / € etc) and prefix that on the displayed sensor value?

zeronounours commented 6 months ago

Hello @Roving-Ronin, the issue comes from utility_meter themselves. The unit is not showed properly because the device_class: monetary is not defined like it is for the cost entity.

The builtin utility_meter defines only energy device class, which is defined from units:
https://github.com/home-assistant/core/blob/dev/homeassistant/components/utility_meter/sensor.py#L99-L102

I can extend the builtin utility meter to prevent that. However, I doubt it is the best way to tackle this problem. I'll try to do a PR on the main component.

As a temporary workaround, you can create a template sensor which takes the same value, but for which will be able to change the class.

Roving-Ronin commented 6 months ago

Hi @zeronounours

If you could PR the main (utility meter) it would be great. Just replacing utility_meter with energy_meter would be ideal ;-)

Thanks !