Energy configuration with Entity tracking total cost

Hi All -

I’m having some trouble setting up the Energy Dashboard -

The import cost is being displayed as an incorrect negative amount -

Screenshot from 2022-03-28 09-58-47

I have setup the Consumed Energy with the following -

Both sensors appear to be giving the correct values -

The import sensor is imported from EmonCMS. This sensor appears to work fine with a static cost.

- platform: emoncms
  api_key: ...
  url: http://emonpi.lan
  id: 1

The entity tracking the total cost is imported from Influx as follows -

- platform: influxdb
  api_version: 2
  host: homeassistant.lan
  port: 8086
  ssl: False
  token: ...
  organization: ...
  bucket: telegraf
  queries_flux:
  - range_start: "today()"
    name: import_cost
    unit_of_measurement: "GBP"
    query: |
        filter(fn: (r) => r["_measurement"] == "mqtt_consumer" and
        r["_field"] == "value" and
        r["host"] == "homeassistant" and
        r["topic"] == "emon/power/import")
        |> integral(unit: 1h)
        |> map(fn: (r) => ({_value: r._value * 0.001 * 0.1995 + 0.2438}))

It calculates the total kWh consumed for the current day, multiplies by the day rate and adds the standing charge.

And has a customization entry -

homeassistant:
  customize:
    sensor.import_cost:
      state_class: total
      device_class: monetary

Since the senors appear to be producing the correct values, I assume something in the customization entry is incorrect. I’d appreciate any pointers.

Thanks,
Andrew.

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:

utility_meter:
  natural_gas_daily:
    unique_id: d0740580-f30f-4b61-9ecc-4d3b30ddd18a
    source: sensor.meter_gas
    name: Natural Gas Daily
    cycle: daily

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)

Hopefully this helps!

This looks like the perfect solution to my monetary/total_increasing log warnings. Nice use of last_reset with total. thanks.

1 Like