How to make a sensor that tracks energy cost of single device that takes peak- and offhours energy prices into account?

Hi all,

Searching for a solution, I found this feature request:

Since it has not been implemented yet, I’d like some advice on how to work around it (without adding each device as a “energy source” instead of an “individual device”.

I have an electric heater that pulls 1800W connected via a smart plug. I used the ‘powercalc’ integration to make power (W), energy (kWh) and utility (daily, weekly, monthly energy) sensors for it, which works nice.

Now the next step is to add cost (€) sensors (daily, weekly, monthly) for it. But that is where I get stuck. If I had a fixed energy price, I would just make template sensors multiplying the utility meter sensors by the energy cost. But I have separate prices for peak hours and off hours depending on the time of day. How would I go about this? Can I somehow make a template sensor that multiplies energy by cost, but uses a different ‘cost’ value depending on the time? Like the Energy Dashboard does?

Thanks in advance for any tips, hints and shoves in the right direction!

EDIT: Of course (as usual), after writing this, a solution comes to mind:
I could make a separate template sensor “current_energy_cost” that switches its €/kwh value based on the time of day. And then use that sensor to multiply the utility sensors.
Is this the (best) way to go, or is there a cleaner/better solution?

You can find examples in the Utility Meter docs:

I indeed came across that part, but I have to stare at it for a bit longer to understand how to use it to get cost sensors. I thought it was only for manually making separate kwh sensors per tariff. As opposed to using the gui.

Yes, you get sensors (per timeframe and traiff) for the consumption, which you can use in the energy dashboard or multiply the consumption with the price in a template sensor.
You can do everything that is in the yaml example in the GUI aswell.

Utility meter integration has this tariff feature but it is not very flexibile and seems to assume fixed set of fixed tariffs.

When you have prices for electricity that is made from a sum of market prices per hour and tariffs that change several times per day and these again change every month, then you need something more flexible.

For the exact example of how to feed a consumption into a utility_meter see Nordpool integration - setup of sensor for Denmark with 3 tarif rates per day changing month by month - #29 by KennethLavrsen

The entire thread is focussed on special Danish circumstances but the template sensors we use can be used in similar contexts

I use utility sensors and change the tariffs based on time with an automation. See below for an example:

alias: Elektrik Tarife Ayarı
description: Elektrik Utility meterleri için gündüz, puant, gece ayarını yap
  - platform: homeassistant
    event: start
    id: start
  - platform: time
    at: "06:00"
  - platform: time
    at: "17:00"
  - platform: time
    at: "22:00"
condition: []
  - service: select.select_option
      entity_id: >
        {% set electric_meters = |
        selectattr("attributes.options", "==", ['Gündüz', 'Puant', 'Gece']) |
        map(attribute='entity_id') | list | join(", ") %} {{electric_meters}}
      option: |-
        {% if now().hour < 6 %}
        {% elif now().hour < 17 %}
        {% elif now().hour < 22 %}
        {% else %}
        {% endif %}
mode: parallel
max: 5

Hi, Did you find a solution for this?

Greetings, John

I have the same issue as mentioned in the first post of this thread but a quite different
I have several €/kWh price depending the period of hour of the day and the day in month.
I’m right at now.
My issue is to compute cost. I have a simple template sensor for each device with this type of state :

state = Utility_meter_energy * input_number.cost_€/kwh

And I save with Long term statistics this sensor in a cost utility_meter .

The issue that when the energy provider change the cost tarif (€/kwh) twice years, my cost sensors and utility_meter was updated with the new tarif provider price…
When i change theses tarifs, normaly, the yearly, monthly, weekly, hourly cost mustn’t change.

I would like to have then no cost change on old data of utility_meter but just take into account the new one for incoming energy cunsumption…

Do you have an idea ?