Energy consumption, last month and predicted this month

I have a couple of rental units.
Each have a home made power meters that sends data to HA.
I have to let tenants know the consumption every month.
I do this by checking the energy tab in HA, but I then have to go into HA, to Energy, choose per month and then check the value.

I have made a HA link to each unit, which just shows power and accumulating kWh for them to check themselves. But it is not all that intuitive.

What I would like is two numbers, well four, kWh and $ for each of:

  1. Last months consumption, no graph, no selecting dates, just a static number that will be replaced next month.

  2. This month predicted consumption, same here, just a number. Calculation for this would be a simple prediction. kWh until now / hours until now * hours this month.

Thanks yall!

This months consumption and costs:

Create a monthly utility meter for each energy sensor:

Create template sensors that multiplies the utility meter sensors by the cost.

Prediction is more difficult. You need to get a running average of the daily cost and know the days left.

This is some of my template calculations for this:

- name: "Average Daily Cost"
  unique_id: 598d8b20-ce47-487c-9d6d-4a277ec020f5
  icon: "mdi:currency-usd"
  unit_of_measurement: "$"
  state_class: 'measurement'
  state: >
    {% set days_used = (now()|as_timestamp - states('input_datetime.last_reset')|as_timestamp)/86400 %}
    {% set use_per_day = (states('input_number.cumulative_energy_cost')|float(0) + states('sensor.total_cost_today')|float(0)) / days_used %}
    {% if is_state('binary_sensor.update_freeze','off') and days_used > 0.5 %}
      {{ use_per_day|round(2, default = 0) }}
    {% else %}
      {{ this.state }}
    {% endif %}
  availability: "{{ has_value('input_datetime.last_reset') and has_value('input_number.cumulative_energy_cost') and has_value('sensor.total_cost_today') }}"

- name: "Next Bill Estimate" 
  unique_id: 0c12ea8e-b5ad-47b5-a491-25cb9959baff
  icon: "mdi:currency-usd"
  unit_of_measurement: "$"
  state_class: 'measurement'
  state: > 
    {% set days_used = (now()|as_timestamp - states('input_datetime.last_reset')|as_timestamp)/86400 %}
    {% if now().day < 18 %}
      {% set remaining_days = 18 - now().day %}
    {% else %}
      {% set next_month = now().month+1 if now().month+1 < 13 else 1 %}
      {% set remaining_days = (now().replace(month=next_month, day=18) - timedelta(days=now().day)).day %}
    {% endif %}
    {% if is_state('binary_sensor.update_freeze','off') and days_used > 0.5 %}
      {{ (states('sensor.cumulative_energy_cost')|float(0) + states('sensor.average_daily_cost')|float(0) * remaining_days)|round(2, default = none) }}
    {% else %}
      {{ this.state }}
    {% endif %} 
  availability: "{{ has_value('sensor.cumulative_energy_cost') and has_value('sensor.average_daily_cost') }}"

Thanks you so much!
I could not really figure out how to use yours but I used it to figure it out.
Using helpers, not YAML:

  1. Optional, Helper input number to set the tariff, $/kWh
  2. Helper utility meter with the specific kWh as input, this will create a value of the kWh that resets every month.
  3. Helper template sensor with this as template: {{state_attr(‘sensor id from 1.’, ‘last_period’)}}, this will create a value of last months kWh.
  4. Helper template sensor with this as template: {{states(‘sensor id from 2.’) | float * states(‘input_number from 0 (or just use the actual $/kWh here’) | float }}, this will create a value of last months cost of energy

Prediction of current month was actually not as hard as I expected, it seems to be correct so far. (I am using the newly created utility meter as input so it does not have much history yet, but seems correct.)

  1. Helper template sensor with this as template: {{states(‘sensor id from 1.’) | float / (now().day *24 + now().hour + now().minute/60) * (((now().replace(day=1) + timedelta(days = 32)).replace(day=1) -timedelta(days=1)).day) *24}}, this will take the consumption for the current month until now, divide by hours of this month until now times the number of hours this month, which will equal the predicted consumption.
  2. Helper template sensor with this as template: {{states(‘sensor id from 4.’) | float * states(‘input_number from 0 (or just use the actual $/kWh here’) | float }}, this will create a value of predicted cost of energy for this month