How should i use "Use an entity tracking the total costs"? (Currently comes through negative)

I have been using energy tracking for a while now and it works well but i am struggling to get the costs to work properly in the Dashboard. Like many others i have a Day Rate + $/kWh. Therefore i have a template sensor to calculate this:

  - sensor:
      - name: "Electricity Consumption Cost"
        state: "{{ (states('sensor.daily_electrical_consumption') | float * 0.22418 + 0.9350 ) | round(2)  }}"
        unit_of_measurement: '$'
        state_class: total
        icon: mdi:home-import-outline
        unique_id: "Electricity_Consumption_Cost"

This works well, and gives me the daily cost as i would expect. However if i use this in the Energy Dashboard Config as below:

I get negative numbers in the Dashboard:

I tried putting a cumulative sensor in here to track the costs but this didn’t work either even whilst left for a few days.

Any ideas what i am doing wrong? What type of entity should i put putting in the dashboard config field?

I have also tried creating an entity which is a continuous sum of the Daily Consumption cost i.e. it never gets reset back to 0 at midnight.
This still doesn’t work in the Energy Dashboard the cost is much lower than the correct daily cost for the day.

Does anyone use the Use and Entity tracking to total costs option? How have you set this up and used it?

Yes I am also looking for an answer. I have my calculation working correctly:

But the energy dashboard:

I provided my solution to tracking daily costs for a utility meter here: Energy configuration with Entity tracking total cost - #2 by blalor

I think the solution to negative values in the energy dashboard totals is due to a missing last_reset attribute on the cost entity.

Trouble is last_reset is no longer used as fas as I can tell. Certainly I am getting an error if I try and set it in a template. I am having to use a markdown card to show the calculations. It is very tedious… Surely there must be an obvious reason for this?

What’s the error? The developer docs suggest it is still supported:

Logger: homeassistant.config
Source: config.py:864
First occurred: 10:13:42 AM (1 occurrences)
Last logged: 10:13:42 AM

Invalid config for [template]: [last_reset] is an invalid option for [template]. Check: template->sensor->19->last_reset. (See /config/packages/energy.yaml, line 1).

It goes under attributes.

Ah. I’ll try that…now that you mention it I think I did know that in the past!

Unfortunately this doesn’t seem to have fixed the issue for me:

image

I am expecting that HA does nothing with that sensor once it is entered as the entity tracking total costs. Therefore, I expect that the value shown in the dashboard should be the same as the state for the sensor? Also, the entity I am providing ONLY is calculating the imported energy cost. I am expecting that this value minus the grid export will equal the Grid Total. Are any of these assumptions incorrect?

Edit: The only other thing I can think of at the moment is in regards to last_reset is the value of the sensor does not go to zero as I am adding in the daily fixed cost so at midnight the sensor drops back to that value…?

Please share your complete config for sensor.total_daily_import_cost.

      - unique_id: "total_daily_import_cost"
        name: Total Daily Import Cost
        icon: mdi:currency-usd
        device_class: monetary
        state_class: total
        unit_of_measurement: $
        attributes:
          last_reset: >
            {{ (now().timestamp() ) | timestamp_custom('%Y-%m-%dT00:00:01+10:00') }}
        state: >
            {% set supply = states('input_number.electricity_supply_charge') | float(0) / 100 %}
            {% set offpeak = states.sensor.daily_energy_offpeak.state | float(0) / 1000 * states.input_number.daily_energy_offpeak.state | float(0) / 100 %}
            {% set peak = states.sensor.daily_energy_peak.state | float(0) / 1000 * states.input_number.daily_energy_peak.state | float(0) / 100 %}
            {% if states('sensor.daily_energy_offpeak') | is_number and states('sensor.daily_energy_peak') | is_number %} 
            {% set demand = (states('input_number.electricity_demand_max') | float (0) / 1000) * 2 / 100 * (states('input_number.electricity_import_rate_high_demand') | float(0)) %}
              {{ (supply + offpeak + peak + demand) | round(2) }}
            {% endif %}

Using now() for your last_reset is suspicious. I don’t know when that attribute gets rendered, but it is likely to be every time the state changes. If the state is supposed to reflect the total cost for the day, it should probably be reset at midnight, otherwise maybe take the last_reset from sensor.daily_energy_offpeak, which presumably also resets daily.

I am forcing the reset time to be midnight by specifying the time as T00:00:01+10:00

I think I might try opening an issue on GitHub as there are a number of reports of this…