Create an entity to store energy price along the time

Hi,

I’m using a utility meter to track my energy consumption daily/monthly/yearly (monthly_cost), also have a sensor number input to be able to define the current energy cost manually (input_number.energy_cost_kwh).

Created the following template sensor to get the monthly energy cost:

energy_cost:
        unit_of_measurement: '€'
        value_template: "{{ (states('input_number.energy_cost_kwh') |float) }}"
monthly_cost:
        unit_of_measurement: '€'
        value_template: "{{ (states('sensor.monthly_import') |float * states('sensor.energy_cost') |float) | round(1) }}

Not using the input_number.energy_cost_kwh directly I’m creating a template sensor called energy_cost as shown above.

The issue with this implementation is that every time I change the energy cost in the middle of the month the monthly_cost changes according to the new value.

image

I would like it to consider the price along the time. For instance, if I have paid X kwh at 0.1€ and then change the price to 0.2€ would expect to pay the next Y kwh at this price.
Currently, the calculations are applying 0.2€ to Y and X kwh.

Can someone give me some help on this topic, please?

Regards,
HC

Please format you posts correctly for the forum. https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

The way you could implement this is with a daily energy utility meter and a triggered template sensor.

Trigger the template sensor just before midnight, calculate your daily cost by multiplying the daily energy by the current price. Then add this to the running total of the template sensor,

1 Like

Hi Tom,

Thanks for the reply. Already made some changes to the original post to comply with the forum format.

That’s precisely what I have implemented. I added the missing part of the code:

utility_meter:
       monthly_import:
           source: sensor.ImportSensor
           cycle: monthly
energy_cost:
        unit_of_measurement: '€'
        value_template: "{{ (states('input_number.energy_cost_kwh') |float) }}"
monthly_cost:
        unit_of_measurement: '€'
        value_template: "{{ (states('sensor.monthly_import') |float * states('sensor.energy_cost') |float) | round(1) }}

So monthly_import collects the monthly imported kwh then monthly_cost multiplies that by the current energy cost (energy_cost).

I tried to explain it in the picture above. So if I change the price of the kwh in the middle of the month the template sensor gives me a total of 20€ instead of 15€.

Regards,
HC

Nope that is completely different from what I suggested. Read my post again.

I suggested you calculate a daily cost and add that to the monthly total.

That way a changing price does not affect your entire month.

1st Oct: 20 x 0.1
+
2nd Oct: 22 x 0.1
+
3rd Oct: 21 x 0.1
+
4th Oct: 22 x 0.15
+
5th Oct 23 x 0.15
+

utility_meter:
  daily_import:
    source: sensor.ImportSensor
    cycle: daily ### <-----
template:
  - trigger: 
      - id: sum
        platform: time
        at: "23:59:59"
      - id: reset
        platform: template
        value_template: "{{ now().day == 1 }}"
    sensor:
      - name: Monthly Cost 
        device_class: monetary
        state: >
          {% if trigger.id = "sum" %}
            {{ ( this.state|float(0) + states('sensor.daily_import')|float(0) * states('sensor.energy_cost')|float(0))|round(2) }}
          {% else %}
            0
          {% endif %}
        availability: "{{ has_value('sensor.daily_import')|float(0) and has_value('sensor.energy_cost') }}

This will reset to zero at 0:00 on the first day of the month. So you will only see the total on the last day of the month for a second.