Energy Dashboard - How to add monthly electricity subscription?

Not sure that I understand you here. Could you post a screenshot, please?

@gdesfeux gdes, thank you, that’s what I was looking for. I’m at the same point as the previous speaker. I have added the two sensors like @phischmi, the sensor “Grundpreis” also shows the correct cumulated value, but only the daily value is displayed in the energy dashboard. I have understood that I have to place this value at the end of the formula when changing tariffs. Now I would also like to see the basic costs for this year in the energy dashboard. The newly installed sensors have been running since yesterday, but the consumption measurement has been running since the end of April. Does anyone know how I can transfer previous monthly costs to the dashboard?

I don’t think you can. Statistics are computed and stored but I don’t think you can edit those as you would like to do it here.

There is a tool in “Developer Tools / Statistics” to adjust sums of total increasing entities but I never figured out how to use it exactly.

On the bright side, now it works and it will be working for 2024. Do remember to change the formula on December 31st to get the right amount from Jan 1st if you rate is changing.

@gdesfeux Okay, I could of course increase the value somewhere in the already written statistics, but then everything is on one day, which then looks clean again in the year. But thank you again, so if I change the formula on 31 December, I only have to adjust the year and set the days to =, right? I don’t have a rate adjustment.

One thing will not be clean in the formula in relation to the months. For example, I have a monthly fixed amount of €10, but this is not always displayed on a monthly basis, because sometimes the month has 30, 31 or even only 28 days, so it will look clean again in the year, but not in the months. Is it not possible to write something into a sensor in an automation, i.e. every month on the 1st (10€) and the value simply keeps accumulating, then you only need to change the value when changing tariffs.

If you don’t have a rate adjustment, you don’t need to adjust anything unless you set your total increasing to “reset” on a yearly basis.

I had to take the days of the year (365) and not the month because it varies depending on the month. Home Assistant calculate the value per day and if you go to your energy dashboard showing by month, you will be getting the right aggregates.

If you have a monthly fee of 10 EUR, then you need to do 10x12/365 to get it by day which will become your daily increment. The fixed value next to the formula is to calculate to offset from the 1st January of a given year.

Hey, thank you, but I understood the maths, but it still remains exactly the “problem” I mentioned. The months will not be correct in your example. You won’t even get to 10 euros in a month with the formula. But I wanted to achieve a fixed and correct fixed value of 10 euros per month. In my opinion, we can only get there if the formula records the days of the months and calculates by the days of the current month and the monthly daily value is dynamic based on the current days within a month or a sensor that receives a value once a month, but then the monthly amount is uncoupled from the daily values.

Ok I understand now. There is no way for you to get the monthly amount right then… You can only get days and years.

@gdesfeux

state: "{{ ((now().strftime('%j')|float - 138) + (now().year - 2023) * 365) * (11.40/((now().replace(day=1) + timedelta(days = 32)).replace(day=1) -timedelta(days=1)).day) }}"

With the basic monthly amount of €11.40/month, it should work, right?

Now put the €11.40 into an Input_Nuber helper and you no longer have to use the formula. Can you check this?

Edit: if I were to change it today 09 January.
I would have to adjust it according to the “old” logic, wouldn’t I?

state: “{{ ((now().strftime(‘%j’)|float - 8) + (now().year - 2024) * 365) * (11.40/((now().replace(day=1) + timedelta(days = 32)).replace(day=1) -timedelta(days=1)).day) }}”

Best regards
Seger

Perhaps an additional question for everyone, do you also have a note in the logs? If so, how did you get rid of it, I don’t want my sensor to stop working afterwards :slight_smile

This is how it looks for me:

Entity sensor.grundpreis (<class 'homeassistant.components.template.sensor.SensorTemplate'>) is using state class 'total_increasing' which is impossible considering device class ('monetary') it is using; expected None or one of 'total'; Please update your configuration if your entity is manually configured, otherwise create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+template%22
    - name: "Grundpreis"
      unique_id: "grundpreis"
      state_class: total_increasing
      unit_of_measurement: EUR
      device_class: monetary
      state: "{{ ((now().strftime('%j')|float - 8) + (now().year - 2024) * 365) * (11.40/((now().replace(day=1) + timedelta(days = 32)).replace(day=1) -timedelta(days=1)).day) }}"

I’m a bit scared if I now:

state_class: total

that it then no longer builds up, how is it with you?

I can report back in February at the latest whether my formula with the basic price works.

Best regards
Seger

Hi there,

For the formula to work, you need to compute to compute your daily subscription cost (11.40*12/365) then you need to adjust the starting amount with the other variables.

As discussed with another HA friend, this way of computing won’t allow you to get accurate monthly subscription in HA. Only daily and yearly will be correct.

In my case, I don’t really as I use HA values as a “guide” for my electricity spending. The billed values are usually close enough.

Cheers,

G.

I have the feeling you don’t understand me, my formula gives the exact daily value and then it is the monthly fixed amount. So it is daily, monthly and yearly. I pay 11.40 per month and it is billed on a daily basis.

Your formula says
11.40*12/365 = 0.3747945205479452 for each day in January, if I multiply it up to the month of January, you come up with a monthly value of:
11.62€, that only fits when calculated over the year

My current test formula for January says the following:
(11.40/((now().replace(day=1) + timedelta(days = 32)).replace(day=1) -timedelta(days=1)).day) }}" = 0.36774193548387096 for each day in January, if I multiply it up to the month of January, I get a monthly value of: 11.40 - dot landing

And next month, my formula will calculate 0.3931034482758621 per day, as the month has 29 days and so every day of February costs that. Extrapolated to the month, this results in 29*0.3931034482758621 = €11.40

So as I understand it, the day, the month and then the year based on the €11.40 will be correct.

In my formula, the fixed amount 11.40 is always fixed, the formula only divides each day by the number of the respective month, so that it gives the correct daily value based on the monthly fee.

all days in:
Jan 11,40€/31
Feb 11,40€/29
March 11,40€/31

Am I making such a big mistake, throw the formula into a template to check it.

Thank you
Seger

Hi Seger,

To have a variable daily rate by month (because from your last post it is what I understand you want to do), you would need another sensor that refers as the daily cost for the month. Then you would need a total increasing that adds it up everyday. That’s my theory. In practical words, I don’t know exactly how I would implement.

Thanks,

G.

1 Like

Okay, now we have created the same basis, great and thank you very much for your tireless feedback and tinkering with it!

I believe that your formula is already doing exactly that with my extension. The sensor already adds up today, every day the defined daily rate is added up so that it fits in the annual view today. The only thing I did was adjust the daily rate to the respective month. I’ll let it run, at the end of January and February we’ll see what’s going on as a monthly value. My opinion should always total 11.40. I also looked at my electricity provider again, where I had a change last year. The change was in the middle of the month, the month had 30 days, and I cancelled on the 17th. The basic fee for the month was 12.90€/30*17days.

Hello,

First of all, thank you @gdesfeux for this solution! I was looking for something exactly like this; I hope this will become native in Home Assistant in a future release.

I used your setup but modified it a bit to fit my needs. I find it a little more elegant:

  • I don’t have any date-dependant formulas in my configuration
  • I can change the monthly subscription cost at any time. It will be taken into account the next day for the daily cost calculation, and there’s no need to change any formula.
  • The daily cost of the monthly subscription is calculated based on the number of days in the month (the difference is litterally less than one cent per day, but it really bothered me :sweat_smile: ).

The key thing is to use a helper to track the total increasing cost of the subscription, because we can set its value at any time, and it survives a reboot.


So here’s my setup:

1. Sensors :

In my configuration.yaml, I declared two sensors:

template:
# First one is a total_increasing energy sensor, which will stay at 0kWh
    - sensor:
        - name: "Abonnement Mensuel Electricité 0kWh"
          unit_of_measurement: kWh
          state_class: total_increasing
          device_class: energy
          state: 0

# Second one is a total_increasing monetary sensor, which replicate our helper, an input number      
    - sensor:
        - name: "Total coût abonnement"
          unit_of_measurement: EUR
          device_class: monetary
          icon: mdi:currency-eur
          state: "{{ states('input_number.edf_total_cout_abonnement_helper') }}"
          state_class: total_increasing



2. Input_number:

Then, in config → Devices → Helpers, I created 2 input_number :

  • The first one will handle the monthly subscription cost
  • The second one will track the cumulative day-to-day cost since i’ve made this configuration.



3. Automation:

Of course, we need to increase our helper every day, so I declared an automation, running every day at 00:01 to add the daily cost :

alias: Increase total subscription cost
description: >-
  Add the cost of one day of elecricity subscription to the input number edf_total_cout_abonnement_helper 
trigger:
  - platform: time
    at: "00:01:00"
condition: []
action:
  - service: input_number.set_value
    target:
      entity_id:
        - input_number.edf_total_cout_abonnement_helper
    data:
      value: >
        {% set dt = now() %}  
        {% set nb_days = (dt.replace(month=dt.month % 12 + 1, day=1) - timedelta(days=1)).day %} 
        {% set price_per_day = (((states('input_number.tarif_edf_tempo_abonnement_mensuel')|float) / nb_days)|float) %}
        {{(states('input_number.edf_total_cout_abonnement_helper')|float) + price_per_day}} 
mode: single


Basically, I get the number of days in the current month by calculating the date of the first of next month, substracting one day, then extracting the day, hence the number of days in the current month.
Then I calculate the price per day by using the monthly subscription cost helper, dividing by the number of days in the month.

Finally, I add this cost to the current value of my input number. The value of the sensor is then replicated from the input number !

3 Likes