Strange behavior of Energy Dashboard (bug?)

I use energy dashboard to track grid usage, solar production, and gas (LPG) consumption, and it tracks usage accurately. For gas, I pay a set amount per cubic foot, and for solar, I am reimbursed a set amount per kWh fed back into the grid. Those are set to static prices in the dashboard settings, and are displayed accurately on the dashboard page. It is a different matter when it comes to tracking grid costs.

For grid usage, I pay a set amount per kWh plus a daily access fee. (In the summer, that fee is almost as much as the kWh charge!) I point the dashboard to “an entity tracking the total costs.” Here is the code for the template sensors that make up that entity:

  - 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
    device_class: monetary
    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 %}
      {% set usage = states('sensor.grid_energy_daily_total') | float * states('sensor.electricity_tariff') | float %}
      {{ (supply + usage) | round(2) }}

As you see, the total daily tariff adds the daily fee to the per kWh charge times the kWh used. Placed in the template section of developer tools, the code produces output such as this:
tariff
This output is correct. 4.95 kWh times .1342 plus .5103 is $1.17 (rounded to two places). However, the dashboard is presently showing this:


It’s not always zero; often the dashboard displays a negative cost.

Is there anyone in the community who has ideas on why the dashboard won’t display what is being correctly calculated? Or, is there anyone with code for “an entity tracking total costs” that the dashboard correctly displays? Are there developers of the energy dashboard around? I remain hopeful that somehow this could work.

Which sensor are you using as the cost sensor in the energy dashboard?

The energy dashboard does not support a daily charge. Only $/kWh. So if you are including the daily charge it will be charging you that for every kWh, not just once per day.

As the cost sensor in the energy dashboard, I am using the Total Daily Tariff template, as shown in the posted code. This template correctly calculates in the Developer Tools Template Editor. it does not add the daily charge to every kWh, only once when it is called. As far as I can tell, energy dashboard runs a cost calculation once each hour and displays that. If it takes the sum calculated in the cost sensor and displays that, it would be correct. Is there any way to examine the energy dashboard code?

That’s what I am trying to tell you. That is not how it works.

It takes the value in the cost sensor (in $/kWh) and multiplies it by the energy used for that hour.

That calculation, kWh used times cost per kWh, is contained in the “entity tracking total costs,” on the next to last line in code above. If all the dashboard can do is take a figure in a template and multiple it internally, then what is the point of an “entity tracking total costs?” That makes no sense.

I put this question to the community at large. Is there anyone who uses the energy dashboard to track electric costs, and who incurs charges in addition to a per kWh cost, that the dashboard will include? If so, may I see the code?

Some people are charged different rates at different times during the day, or even charged different rates depending on their total consumption.

This can be easily automated to change a cost sensor.

Quite a few people judging by this:

To be clear, the dashboard takes whatever price is contained in the cost sensor and multiples that figure by the kWh used in the preceding hour, totaling this through the day? I am left wondering why there are two separate entries, one “tracking total costs” and one giving the “current price”, if they are handled exactly the same way by the dashboard.

Not sure what you are talking about here.

Oh now I see. This?

TBH I have not noticed that before and have not used it.

After reading the threads you linked, I went with the low-usage sensor (.001 kWh) outlined there. I implemented this as a separate consumption and it works. I’ve marked your reply with that link as the solution. Here is the code:

  - name: "Grid Acess Charge"
    unique_id: grid_access_charge
    unit_of_measurement: "kWh"
    device_class: energy
    state_class: total_increasing
    state: >-
       {{ (((as_timestamp(states('sensor.date_time_iso')) / 3600)-462184) | int) / 1000}}
    attributes:         
        last_reset: '1970-01-01T00:00:00+00:00'

Thanks again for your help.