Monthly energy use from a kWh meter

Hello all,

I have a kWh meter which sends it data via modbus - ESP/Tasmota to HA. All works fine and I added the sensor to my energy dashboard.

image

The energy dashboard is able to calculate the monthly usage.

My question is if it is posible to send an message (notify) via Email every 1st of the month with the begin, end and total usage values of the previous month?

I tried to understand Jupyterlab, but this is to much for my brain to grasp. ChatGTP proposed a template sensor, but also here no luck. Perhaps a script which queries the database… Since the energy dashboard is able to get the values, it should be doable?

Does anyone has a clue how I can solve this problem?

Thanks in advance!

Cheers,
Kees

Don’t use ChatGPT. It is trained on out of date information and will only confuse you.

The energy dashboard calculates these values from the statistics database when they are needed for display. There are no actual entities that hold these values. You will have to create an entity to track the value. The Utility Meter helper with a monthly cycle can track how much energy was used each month but will not be able to tell you the starting value.

2 Likes

Hi tom, thanks for your reply!

I am gonna play with this integration. Is there any other way to get the begin and end values of the meter?

Can I create an sensor which is triggered by an automation, which stores the value of the meter on the first of each month at 0:00?

A better idea would be a triggered template sensor.

I’m doing something similar for every month.
in templates.yaml I use the following template to write the consumed energy value to an input_number helper, every first day of the month at midnight.
I made a helper and a template for every month.
the value comes from an attribute from a utility meter with monthly reset.
this attribute is called “last_period” and holds the value from the previous month.
this way you have the end (and start) value for every month.
with these values you can do anything you want, make further calculations, send notifications, etc…
the trigger does not have to be at midnight because the value is available during one month. (I tend to do this one minute after midnight)
the example below is for the month november:

# template vastlegging consumptie november
  - trigger:
      - platform: template
        value_template: "{{ now().strftime('%m-%d-%H:%M') == '12-01-00:00' }}" 
    action:
      - service: input_number.set_value
        entity_id: input_number.energy_consumed_november
        data:
          value: "{{ state_attr('sensor.utilitymeter_consumption_month','last_period') }}"                 

EDIT:
I just realized you don’t have to make a template for each month if you use a dynamic entity_id name.

# template vastlegging consumptie per maand
  - trigger:
      - platform: template
        value_template: "{{ now().strftime('%d-%H:%M') == '01-00:00' }}" 
    action:
      - service: input_number.set_value
        data:
          value: "{{ state_attr('sensor.utilitymeter_consumption_month','last_period') }}"                 
        target:
          entity_id: input_number.energy_consumed_{{now().month}}

you still need to create 12 number helpers like input_number.energy_consumed_11
(_1 to _12)

Here’s how to do it without the input number or automation:

template:
  - trigger:
      - platform: template
        value_template: "{{ now().strftime('%m-%d-%H:%M') == '12-01-00:00' }}" 
    sensor:
      - name: November Energy
        state: {{ now().strftime('%m-%d-%H:%M') == '12-01-00:00' }}"          
        unit_of_measurement: kWh
        device_class: energy

I do not understand this part:

shouldn’t this be

state: "{{ state_attr('sensor.utilitymeter_consumption_month','last_period') }}"

?

Doesn’t this give _12 instead of _11? (not that it matters, difference is that it gives the startvalue of thatmonth)

Can I also use

- name Energy {{now().month}}

And trigger just on day 1 @ 0:00?

No, the name option does not accept templates.

the next time this template will trigger will be at the 1st second of the new month (december in this case).
so yes the value will be written to input_number.energy_consumed_12.

if you want to store it in the previous month, you can add -1 at the end:

entity_id: input_number.energy_consumed_{{now().month-1}}

this way when the template triggers at the beginning of december, the value will be stored in the input_number of november.

Hi Tom,

I think you missed this post

I do not understand this part:

shouldn’t this be

state: "{{ state_attr('sensor.utilitymeter_consumption_month','last_period') }}"

And what is the difference between the sensor approach and input_number approach?

It was just an example to show how the previously posted automation would be used as a triggered template sensor.

So, I want to give a small follow-up of my journey.

After some fiddling around in HA, tried en after that decided to go the node-red route. Every month Node-red pulls the data from the kWh meters and stores it in a sqlite database.

After that It creates a PDF with all data for my administration.

Could you post your solution here. I’m wanting to do the same.

Hi Kees, can let us know how you did this ? Rg Frank

For those who want to replicate this, I can provide the json flow for node-red. I think that the only node-red-contrib-pdfmake2 and node-red-contrib-moment have to be installed (hamburger-menu - manage pallete).

PM me if you want the json

Cheers, Kees