# Energy cost calculations

Is there a way to set your energy cost in stages? For instance, my Utility provider has a per KwH price up to 1400 Kw and then a different price thereafter. Can I set the energy dashboard to handle that on a monthly basis? I am using a Shelly EM to monitor my consumption so I have 4 inputs that it currently calculates but at a single fixed rate.

2 Likes

Create a utility meter with a monthly cycle to keep track of your monthly use:

e.g.

utility_meter:
energy_used_this_month:
source: sensor.your_energy_used_sensor_here
cycle: monthly

Then create a template sensor to select your price perkWh based on the state of this utility meter.

e.g

template:
- sensor:
- name: Energy Price
unit_of_measurement: "\$/kWh"
state: "{{ 0.3 if states('sensor.energy_used_this_month')|float(0) > 1400 else 0.15 }}"

Use sensor.energy_price in your energy dashboard:

5 Likes

Thank you for that info! How would I build that template sensor since I have 4 inputs to deal with? I have a Shelly EM on my house and on my garage. Each Shelly provides a leg1 and leg 2 consumption value. I know I could make a separate template sensor for each leg, but the energy dashboard sensor only accepts one entity.

template:
- sensor:
- name: Total Consumption
unit_of_measurement: "kWh"
icon: mdi:counter
state_class: total_increasing
device_class: energy
state: >
{{  states('sensor.energy_sensor_1')|float(0) +
states('sensor.energy_sensor_2')|float(0) +
states('sensor.energy_sensor_3')|float(0) +
states('sensor.energy_sensor_4')|float(0) +
}}
availability: >
{{  states('sensor.energy_sensor_1')|is_number and
states('sensor.energy_sensor_2')|is_number and
states('sensor.energy_sensor_3')|is_number and
states('sensor.energy_sensor_4')|is_number
}}
2 Likes

Thank you very much. Out of curiosity, does this calculate the first 1400 kWh at one rate and then everything thereafter at another rate? The templates look like it is just an over/under type calculation, for instance if I use 1500 kWh the template calculates all 1500 kWh at .15 per your example.

Yes. Up to and including 1400 it is calculated at 0.15 \$/kWh. Above 1400 it is calculated at 0.3 \$/kWh.

Nope. The energy price is 0.15 until you reach 1400, then the last 100 of the 1500 is calculated at 0.3

Thank you very much!

1 Like

Hi, Tom

Is it possible with 3 way prices?.
From 1kWh to 255kWh - 0.8
From 255 to 300kWh - 1.3
From 301kWh i must pay for all 0 - 300kWh with 1.3 and above.
Thanks!

Just to confirm what you’re saying:

• if you consume 100kWh in a cycle, you pay 100×0.8 = \$80 (assuming \$ and starting from 0kWh rather than the 1 you said);
• if you use 300kWh you pay 255×0.8 + 45×1.3 = \$262.50;
• and if you use 301kWh you pay 301×1.3 = \$391.30 (a very expensive extra kWh!)

Is that really correct? If so, that won’t work with the energy dashboard as the “current price” at the beginning of the cycle might change from 0.8 to 1.3 later, but the cost integration will have already been done.

1 Like

Funny, no dollar here, only RON “Romania”
I have monthly cycle 300kWh
First 255 at 0.8
45 to 300 at 1.3
From 301 above i pay everything with 1.3

Yes, like that.

hey, @tom_l need some help here, following your post I came up with the following solution for my scenario however, I am struggling to add the value calculated in fuel_adjustment to the total amount, could you please help me?

- sensor:
- name: Energy Price
unique_id: energy_price
icon: mdi:counter
state_class: total_increasing
device_class: energy
unit_of_measurement: "\$/kWh"
state: >
{% set consumption=states('sensor.energy_meter_consumption')|float(0) %}
{{ 0.05214 if consumption > 1000 else 0.04989 }}
availability: "{{  states('sensor.energy_meter_consumption')|is_number }}"

basically what I am trying to achieve is:

{{ 0.05214 if states('sensor.energy_meter_consumption')|float(0) > 1000 else 0.04989 }}+{{fuel_adjustment}}

I tried that but get the wrong number:

Just delete the two inner brace pairs to make one template:

{{ 0.05214 if states('sensor.energy_meter_consumption')|float(0) > 1000 else 0.04989 + fuel_adjustment}}
1 Like

Something looks odd to me:

this is my daily usage (for today):

this is my monthly usage (since the day I installed the device and configured the add-on to start receiving data):

why do the values look the same? why \$10351.01? it’s impossible that I would pay 10k for energy in a day

PS: since this post has been answered should I continue having this conversation here or open a new post?

THis is really good example , simple and work .
but what if the price is progressive
for example
0-150 kWh will be charge 1.2 \$/kWh
150-400 kWh will be charge 1.8 \$/kWh
400+ will be charge 2.0 \$/kWh
so if I use 500 kWh for this month
I will be charged (150 x 1.2) + (250 x 1.8) + (100 x 2.0) = 830 \$

how to write condition in template: ? I"m unfamiliar with HA syntax but to write in C should look like this

if (total_energy > 400) {
monthly_charge = (total_energy-400) x 2.0 + (250 x 1.8)+ (150 x 1.2);
} else
if ((total_energy <= 400) && (total_energy >150)) {
monthly_charge = (total_energy-150) x 1.8 + (150 x 1.2);
} else
monthly_charge = total_energy x 1.2;

thank you.

thank. crist

Youre welcome, also Ive updated the sensor

huawei_monthly_grid_price:
friendly_name: "Huawei Monthly Price"
unique_id: sensor.huawei_monthly_grid_price
unit_of_measurement: "Ron"
value_template: >-
{% set grid = states('sensor.huawei_monthly_grid_reset')| float %}
{% if grid|float < 100 %}
{{ (grid * 0.6 )  | float(0)|round(2) }}
{% elif grid|float > 100 and grid|float < 255 %}
{{ (grid * 0.8 )  | float(0)|round(2) }}
{% elif grid|float > 255 %}
{{ (grid * 1.3 )  | float(0)|round(2) }}
{%else%}
off
{% endif %}

there is no more need the sensors price as it can be updated in the template value
Try it

I added 3 template numbers (in templates.yaml) to be changeable price for each electricity used step.

• sensor:

• name: “Current Month Cost”
icon: “mdi:cash”
unit_of_measurement: “THB”
unique_id: sensor.monthly_electric_usage
state: >
{% set p1 = states(‘input_number.1-150’) |float(4) %}
{% set p2 = states(‘input_number.151-400’) |float(4) %}
{% set p3 = states(‘input_number.400+’) |float(4) %}
{% set grid = states(‘sensor.smart_security_pzem_004t_v3_energy’) |replace(’,0’, ‘’) |float %}
{% if grid|float < 151 %}
{{ (gridp1 ) | float(0)|round(1) }}
{% elif grid|float < 401 %}
{{ ( (150
p1)+(grid-150)p2 ) | float(0)|round(1) }}
{% elif grid|float > 400 %}
{{ ( (150
p1) + (250*p2) + (grid-400)*p3 ) | float(0)|round(1) }}
{%else%}
0
{% endif %}
2 Likes

Hello there
i have Shelly pro 3em with 3 phase CT.
i installed the device and integrated to HA perfectly.
in our country if monthly consumption was 450 Kwh/Month the tariff will be 18 IQD/Kwh, but if exceeded that range next 450kwh will be 24 IQD/month,
My question how we can calculate that ??

Hi @tom_l
I am not sure when this broke, but the template no longer seems to be evaluating whether the total monthly consumption is greater than 1400. Currently my consumption is 1706 (sensor.energy_used_this_month) and the cost is being calculated at a flat rate of .125680 for all 1706 KwH. Here is the template:

- sensor:
- name: energy price
unit_of_measurement: "\$/kWh"
state: "{{ 0.125680 if states('sensor.energy_used_this_month') | float(0) > 1400 else 0.134040 }}"

Any help would be appreciated.