I think this issue has come up in a couple different places. When looking into how to set up “Use an entity tracking the total costs” (which is effectively un-documented, near as I can tell) I ended up finding this solution to working around the unsupported total_increasing state class for monetary device class entities. Ultimately what’s required is setting the last_reset attribute when the entity returns to zero. The entity I set up to do this has been working correctly (non-negatively) since I set it up a couple of days ago.
My config is something like this: sensor.gas_meter is an entity created by rtlamr2mqtt. sensor.natural_gas_daily comes from the utility_meter integration:
The sensor.natural_gas_daily_cost template sensor uses two helper entities I manually update to set the per-unit costs charged by the utility company:
---
name: Natural Gas Daily Cost
unique_id: 66b8c2a0-b401-4d6e-8f04-81d6efbff816
unit_of_measurement: USD
state_class: total
device_class: monetary
# as with the monthly cost, charges are given in USD/CCF, 100 CCF per ft³. daily
# excludes base fee.
state: >-
{{
(
(states("sensor.natural_gas_daily") | float)
* (
(states("input_number.gas_distribution_charge") | float) +
(states("input_number.purchased_gas_cost_charge") | float)
)
/ 100
) | round(2)
}}
availability: >-
{{ has_value("sensor.natural_gas_daily") }}
attributes:
# required for use as "total" in energy dashboard
last_reset: >-
{{ state_attr("sensor.natural_gas_daily", "last_reset") }}
base fee: >-
{{ states("input_number.gas_residential_base_fee") }}
distribution charge: >-
{{ states("input_number.gas_distribution_charge") }}
purchased gas charge: >-
{{ states("input_number.purchased_gas_cost_charge") }}
I peg the template sensor’s last_reset attribute to the same attribute on the sensor.natural_gas_daily entity.
Finally, my gas meter configuration in the energy dashboard looks like this:
Gas usage: “Gas Meter” (sensor.gas_meter)
Use an entity tracking the total costs: “Natural Gas Daily Cost” (the template sensor I created)