How do I get a total cost of electricity in a sensor card?

Hi,

I’d like to get the total cost of electricity (today) in a sensor card.

The energy dashboard is setup correctly - I’ve got both a standing charge and a pence per KwH cost.

The cost per KwH I’m running of a Shelley EM - I’ve set a static price (of 0.2292). That’s working fine.

For the standing charge - I’ve got an entity with the total costs and an automation setup which resets at midnight.

I’ve then setup a template sensor to pull the daily cost (combining both of the above), but I’ve not configured it right as the cost doesn’t match the total cost in the energy dashboard:

{% set kwh = states('sensor.shellyem_channel_1_energy') | float(0) / 1000 %}
{% set price_per_kwh = 0.2292 %}
{% set standing_charge = states('input_text.tariff_import_standing_charge_daily') | float(0) %}
{{ (kwh * price_per_kwh + standing_charge) | round(2) }}

Any thoughts?

I would use a utility meter instead of the text input and automation. Not sure if the text works as intended for calculations.

Try adding the following to your configuration.yaml

utility_meter:
  energy_in_daily:
    source: SAME.SENSOR_AS_ENERGY_DB
    cycle: daily
  energy_in_weekly:
    source: SAME.SENSOR_AS_ENERGY_DB
    cycle: weekly
  energy_in_monthly:
    source: SAME.SENSOR_AS_ENERGY_DB
    cycle: monthly
  energy_in_yearly:
    source: SAME.SENSOR_AS_ENERGY_DB
    cycle: yearly