History Stats for template (costs)

Hello,

I calculate the cost of our underfloor heating by multiplying the running time by the power requirement in watts and the kw/h price:

### Berechnung Kosten Fussbodenheizung Esszimmer
  - platform: template
    sensors:
      fbh_esszimmer_kosten:
        friendly_name: "Fussbodenheizung Esszimmer Kosten"
        unit_of_measurement: "€"
        value_template: "{{ (states('sensor.heizung_esszimmer_laufzeit') | float * 0.4208 * 11.878) | round(2) }}"

This works quite well so far, but I would like to display this statistically in a diagram. However, I cannot select this sensor in the statistics card.
How do I manage to display a history of the daily and monthly values?

Thank you

Dennis

The history of your sensor should be viewable as-is, but you’d need to use the history card, and you’ll only have history stored for as long as you set your recorder database to store it (default of 10 days for the purge_keep_days setting).

To get data into long-term-statistics, you’d need to make the changes required as documented here which essentially is to set the state_class to one of 3 options.

Unfortunately you are using the legacy format for a template sensor which does not support that setting. You need to convert to the modern format first. The legacy format goes under the sensor: heading and the modern goes under the template: heading, in addition to a number of other changes.

This is the section you’ll want to reference to get the new format and to add state_class.

On a slightly different subject, I’m not convinced your cost calculation is correct. To get a running total cost spent on energy, you first need an entity that reports total energy consumed, not power being used. Power is energy per unit time, in other words the rate of energy consumption. If you only have an entity that measures power, you can use the Riemann sum integration to calculate the energy consumed. Or perhaps you are trying to measure the cost rate for heating your floor, and in that case your units would be €/h. Even in that case you’d need to make sure your energy price is €/kWh and your power entity is in kW, or else you can divide by 1000 in your template.

1 Like

Hi @mekaneck

Thank you very much for your reply. I have now converted the sensor to the new format and added “state_class: measurement” to it. Now I can select the sensor in the statistics map.
Now I just have to wait a bit to see if the statistics fill up correctly. Thank you very much.

About my calculation:
I have the running time of the underfloor heating. in hours The power consumption is fixed at (approximately) 11,878 W. So time x W = W/h. The kW/h price was 0.4208€.

In fact, I would still have to calculate everything /1000, you’re right!
But basically that should be correct, right?

Translated with DeepL.com (free version)