Utility Meter: help requested

I set up the Energy Dashboard with a Shelly EM3 and it is correctly showing the energy taken from and returned to the grid. I’m using a static tariff, but my electric company actually charges a daily fee plus a per kWh tariff. I’ve been trying to set up the utility meter helper to calculate these. This is the configuration.yaml and the sensor it and the energy dashboard use:

utility_meter:
  daily_energy:
    source: sensor.grid_energy_total
    name: Daily Energy
    cycle: daily
    tariffs:
      - daily_fee
      - per_kwh
  - name: "Grid Energy Total"
    unique_id: grid_energy_total
    state: >-
      {{ [ states('sensor.shellyem3_channel_a_energy'), 
           states('sensor.shellyem3_channel_b_energy'),
         ] | map('float') | sum }}
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"
    availability: >-
          {{ [ states('sensor.shellyem3_channel_a_energy'), 
               states('sensor.shellyem3_channel_b_energy'),
             ] | map('is_number') | min }}

but there I’m stuck. I know how I would do it in python or C#: pass the kWh used since midnight to a function that would divide that into the daily fee, add the per kWh tariff and return that as the total tariff. I think I need a sensor template to calculate these charges. I could use some help…thanks for any.

I would have 2 separate utilities meter for channel A and B (i dont think we can have multiple source into one utility meter) and use template sensor to add the two to get total energy. Not very elegant but work fine and seem to quite accurately predict the bill, I am interested to see what others have.

utility_meter:
  energy_daily_1:
    source: sensor.shellyem_channel_1_energy
    name: Energy Daily 1
    cycle: daily
  energy_daily_2:
    source: sensor.shellyem_c45bbe6b70c8_channel_1_energy
    name: Energy Daily 1
    cycle: daily

Then under the sensor i add the two

sensor:
  - platform: template
    sensors:
       energy_total:
        friendly_name: "Energy Total"
        unit_of_measurement: 'kWh'
        icon_template: mdi:counter
        value_template: "{{ (states('sensor.energy_daily_1') | float + states('sensor.energy_daily_2') | float) | round(2) }}"

You just want to create a sensor or input number with the daily fee and per kwh fee, not a utility meter.

Then a sensor to add up all the usage at various rates and add the daily fee.

Here’s how I would do mine if I use your existing total energy sensor

utility_meter:
  grid_energy_total_daily:
    source: sensor.grid_energy_total_daily
    name: Grid Energy Total Daily
    cycle: daily

template:
  - sensor:
      - name: "Daily Fee"
        unique_id: daily_fee
        icon: mdi:cash-plus
        unit_of_measurement: $
        state: 1.03235

      - name: "Electricity Import Rate"
        unique_id: electricity_import_rate
        icon: mdi:cash-minus
        unit_of_measurement: $
        state: 0.12345

      - name: Total Daily Import Cost
        icon: mdi:currency-usd
        state_class: total_increasing
        device_class: monetary
        unit_of_measurement: $
        state: >
          {% set supply = states('sensor.daily_fee') | float(0) %}
          {% set usage = states('sensor.grid_energy_total_daily') | float(0) * states('sensor.electricity_import_rate') | float(0) %}
          {{ (supply + usage) | round(2) }}

That’s what the template sensor does, adds the 2 phases of electricity to get total energy, and it works fine. It’s the utility meter that I need help on.

you can use that sensor for 3rd utility meter… continuing from where i left off

utility_meter:
  energy_daily_total:
    source: sensor.energy_total
    name: Energy Daily Total
    cycle: daily
  energy_monthly_total:
    source: sensor.energy_total
    name: Energy Monthly Total
    cycle: monthly
  energy_quarterly_total:
    source: sensor.energy_total
    name: Energy Quarterly Total
    cron: "0 0 27 3/3 *"

is that what you mean? Since utilities meter is cumulative it doesnt matter, as long as sensor move it will accumulate. And use the two service to adjust you current value.

SERVICE UTILITY_METER.CALIBRATE
SERVICE UTILITY_METER.RESET

Then i got these sensors to calculate cost.

      energy_cost_daily:
        friendly_name: "Daily cost"
        unit_of_measurement: '$'
        icon_template: mdi:file-clock-outline
        value_template: "{{ (states('sensor.energy_daily_total') | float * 0.287716) | round(2) }}"
      energy_cost_monthly:
        friendly_name: "Monthly cost"
        unit_of_measurement: '$'
        icon_template: mdi:folder-clock-outline
        value_template: "{{ (states('sensor.energy_monthly_total') | float * 0.287716) | round(2) }}"
      energy_cost_quarterly:
        friendly_name: "Quarterly cost"
        unit_of_measurement: '$'
        icon_template: mdi:database-clock-outline
        value_template: "{{ (states('sensor.energy_quaterly_total') | float * 0.287716) | round(2) }}"

Two different approaches here. I’ll try each tomorrow and let you know how it goes. Thank you.

They are pretty much the same, just a matter of where you place things.

I’ve made some progress, though it’s difficult to be certain of the final results since they are accumulative. I’d like to know where the database is for the energy dashboard so I could clean it out and start again. These are the energy meters I use:

`utility_meter:
  grid_energy_daily_total:
    source: sensor.grid_energy_total
    name: Grid Energy Daily Total
    cycle: daily
  grid_energy_monthly_total:
    source: sensor.grid_energy_total
    name: Grid Energy Monthly Total
    cycle: monthly
  grid_energy_returned_daily_total:
    source: sensor.grid_energy_returned_total
    name: Grid Energy Returned Daily Total
    cycle: daily

not using the monthly meter, there just for observing. I found out that, even though you might put the energy meter in config.yaml manually, you should not write the corresponding template. Energy meter writes its own and, if you already have a sensor template, will write another template_2. Even if the first template is deleted from the yaml, the sensor remains. I haven’t found a way to delete it or rename the template_2. I’d like to know where the energy meter keeps its files.

These are the sensors I have to calculate costs:

  - name: "Daily Electricity Fee"
    unique_id: daily_electricity_fee
    icon: mdi:cash-plus
    unit_of_measurement: $
    state: .5103

  - name: "Electricity Tariff"
    unique_id: electricity_tariff
    icon: mdi:cash-plus
    unit_of_measurement: $
    state: 0.1342

  - name: "Total Daily Tariff"
    unique_id: total_daily_tariff
    icon: mdi:currency-usd
    state_class: total_increasing
    device_class: monetary
    unit_of_measurement: $/kWh
    state: >
      {% set supply = states('sensor.daily_electricity_fee') | float(0) / 24%}
      {% set usage = states('sensor.grid_energy_daily_total_2') | float(0) * states('sensor.electricity_tariff') | float(0) %}
      {{ (supply + usage) | round(2) }}

  - name: "Total Daily Credit"
    unique_id: total_daily_credit
    icon: mdi:currency-usd
    state_class: total_increasing
    device_class: monetary
    unit_of_measurement: $/kWh
    state: >
      {% set supply = states('sensor.daily_electricity_fee') | float(0) / 24 %}
      {% set usage = states('sensor.grid_energy_returned_daily_total') | float(0) * states('sensor.electricity_tariff') | float(0) %}
      {{ (supply + usage) | round(2) }}

The electric fee and tariff are in units of $, but the energy dashboard says the daily tariff and credit have to be in {currency}/kWh, so I used $/kWh. Not sure that’s correct due to earlier bad database entries, but it parses, at least. Because the energy dashboard polls once per hour (I think!), the daily fee is divided by 24 for each hour it’s polled. then the accumulated value for grid energy is multiplied by the tariff and added to the fee. Energy returned to the grid is handled in like fashion.

I will leave all this way and check the energy dashboard tomorrow. Comments and help welcomed.

Yes $/kWh is right. I missed that mine are actually like that when typing up sorry.

You need to remove the /24 from the supply as it will only ever add one 24th of the supply fee to the total.
Whether you use power all day or not you pay the full supply amount as soon as the day starts.

Also your Total daily credit should not include the supply fee.

so it can be as simple as

  - name: "Total Daily Credit"
    unique_id: total_daily_credit
    icon: mdi:currency-usd
    state_class: total
    device_class: monetary
    unit_of_measurement: $/kWh
    state: {{ (states('sensor.grid_energy_returned_daily_total') | float(0) * states('sensor.electricity_tariff') | float(0)) | round (2) }}

You will note i have used total instead of total increasing. You need to do this for both sensors. This is because total increasing does not allow it to decrease which it will do at the start of each day.

You can purge you entire db by deleting it from the config folder. It will remove all history for all entities.

Thanks; I will make those changes and see what ensues tomorrow.

Regarding the old meter entities, it reside in config/.storage/core.restore_state
Edit this while the HA is stop (as it will write the entity back again if it still running), and make back if you are going edit it.

Thanks; the superfluous entity went away after I deleted the database. One more question about using the costs template in the energy dashboard. Should it be entered as “tracking the total costs” or “with current price”?

Following your existing settings above. If you choose…

Use a static price: 0.1342
(This is where you enter the known price)

Use an entity with current price: sensor.electricity_tariff
(This is where you have an entity with the tariff, in this case the one that you created, in some cases it taken straight from the provider if the provider supported in HA)

Use an entity tracking the total costs: sensor.total_daily_tariff
(This is where you have an entity with the total cost)

(Out of town for a few days) I have total daily tariff set as tracking the total costs, and total daily credit as showing current price. Both are wildly off the mark; at 1800 the grid energy total is 1.1 kWh and costs $0.15 while the energy returned is -6.41 kWh with a credit of $2.33! What can be going on? I’d sure like to know where utility meter keeps its files.

The utilities meter integration and the energy dash board are different, are you talking about the dashboard? The energy dashboard have the Day, week and month button top right. The utility_meter: cycle set as “daily” will reset daily.

I set up mine like this

To calculate those I have a 3 separate Daily, Monthly and Quarterly Utility Meter. The daily one will reset every day, it will not keep data beyond that.

You need to give more information. Nothing you have said indicates and error. Until you provide the sensor data and the prices/tariffs that are used plus your method for calculation and energy dashboard setup we are just guessing.

1 Like

Yes, of course. Here, first, are two screenshots showing how the energy dashboard is set as to its sources.


Here is the code for the energy sensors:


  - name: "Grid Energy Total"
    unique_id: grid_energy_total
    state: >-
      {{ [ states('sensor.shellyem3_channel_a_energy'), 
           states('sensor.shellyem3_channel_b_energy'),
         ] | map('float') | sum }}
    unit_of_measurement: kWh
    icon: mdi:counter
    device_class: energy
    state_class: total_increasing
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"
    availability: >-
          {{ [ states('sensor.shellyem3_channel_a_energy'), 
               states('sensor.shellyem3_channel_b_energy'),
             ] | map('is_number') | min }}

  - name: "Grid Energy Returned Total"
    unique_id: grid_energy_returned_total
    state: >-
      {{ [ states('sensor.shellyem3_channel_a_energy_returned'), 
           states('sensor.shellyem3_channel_b_energy_returned'),
         ] | map('float') | sum }}
    unit_of_measurement: kWh
    icon: mdi:counter
    device_class: energy
    state_class: total_increasing
    attributes:
      last_reset: "1970-01-01T00:00:00+00:00"
    availability: >-
          {{ [ states('sensor.shellyem3_channel_a_energy_returned'), 
               states('sensor.shellyem3_channel_b_energy_returned'),
             ] | map('is_number') | min }}

and the cost sensors:

  - name: "Daily Electricity Fee"
    unique_id: daily_electricity_fee
    icon: mdi:cash-plus
    unit_of_measurement: $
    state: .5103

  - name: "Electricity Tariff"
    unique_id: electricity_tariff
    icon: mdi:cash-plus
    unit_of_measurement: $
    state: 0.1342

  - name: "Total Daily Tariff"
    unique_id: total_daily_tariff
    icon: mdi:currency-usd
    state_class: total
    device_class: monetary
    unit_of_measurement: $/kWh
    state: >
      {% set supply = states('sensor.daily_electricity_fee') | float(0) %}
      {% set usage = states('sensor.grid_energy_daily_total') | float(0) * states('sensor.electricity_tariff') | float(0) %}
      {{ (supply + usage) | round(2) }}

  - name: "Total Daily Credit"
    unique_id: total_daily_credit
    icon: mdi:currency-usd
    state_class: total
    device_class: monetary
    unit_of_measurement: $/kWh
    state: >
      {{ (states('sensor.grid_energy_returned_daily_total') | float(0) * states('sensor.electricity_tariff') | float(0)) | round (2) }}

This is the utility meter in configuration.yaml:

utility_meter:
  grid_energy_daily_total:
    source: sensor.grid_energy_total
    name: Grid Energy Daily Total
    cycle: daily
  grid_energy_returned_daily_total:
    source: sensor.grid_energy_returned_total
    name: Grid Energy Returned Daily Total
    cycle: daily

The amount of kWh being consumed and that being returned are calculated correctly, but the costs and credits shown in energy dashboard do not correspond with the tariffs and credits in the sensors. For example, here are the amounts for a recent hour period:

Let me know what other info I can provide. Thanks again to you both for your continued assistance.

One more screenshot of the output:

Thanks.

Can you please past this in developer tools > template and show the output.

daily electricity fee:  {{states('sensor.daily_electricity_fee') | float(0)}}
electricity tariff:     {{states('sensor.electricity_tariff') | float(0)}}
{% set supply = states('sensor.daily_electricity_fee') | float(0) %}
{% set usage = states('sensor.grid_energy_daily_total') | float(0) * states('sensor.electricity_tariff') | float(0) %}
Total daily tariff:     {{ (supply + usage) | round(2) }}
energy returned daily:  {{states('sensor.grid_energy_returned_daily_total') | float(0)}}
Total daily credit:     {{ (states('sensor.grid_energy_returned_daily_total') | float(0) * states('sensor.electricity_tariff') | float(0)) | round (2) }}

This was the template output:

template output

This was showing in the energy dashboard:

The template output of 1.14 is correct for the 4.65 kWh consumed so far today, but the energy dashboard is showing -0.85. Is the configuration of the energy dashboard grid consumption incorrect? It points to grid_energy_total and total_daily_tariff as an entity tracking total costs (screenshot above).

I’ll run and post again later to show the grid return and credit output.