# Calculate price of consumed energy in Spain's PVPC system

Hi! It’s my first post on this forum, hope you find it useful .

So, a little bit of context. In Spain we have a electricity tariff called PVPC (precio voluntario al pequeño consumidor), which basically changes the price of electricity every hour depending on the expected consumption in the country. This makes it difficult to calculate the exact amount of money you are paying in home assistant. If the price were static you could just multiply the amount of electricity consumed by the price to get the total money you will be paying in the next bill. I cannot do that so I created a way to fix it up (with a little help from ChatGPT). I did this for the energy consumption of my washing machine, so I’ll detail the steps I followed:

1. The first thing you need is the PVPC integration (Spain electricity hourly pricing (PVPC) - Home Assistant) which gives you the prices for every hour. Also, you will need a way to monitor the energy usage of your desired appliance (washing machine in my case), I used an Aqara smart plug with energy monitoring.

2. Create an input number in which we will store the sum of all the money expenses from the washing machine. You can do so in the helpers section in home assistant. Specify a min of 0, a max of 1000000 (just put something crazy high here so that home assistant doesn’t run into an error) and a step of 0.001. The unit of measure will be € (as I asume you will be doing this in Spain). My input number is “input_number.money_expense_washing_machine”.

3. Create an utility meter in home assistant to cycle the consumed energy of the washing machine hourly. You can also do it in the helpers section. The input sensor should be the one giving you the energy consumption from your washing machine (in my case the Aqara Smart Plug). Cycle it every hour. My utility meter is “sensor.hourly_energy_usage_washing_machine”.

4. Create an automation that updates the value of the input number every hour. The automation would be something like this:

``````trigger:
- platform: time_pattern
minutes: "59"
condition: []
action:
- service: input_number.set_value
data:
entity_id: input_number.money_expense_washing_machine
value: >-
{{ states('input_number.money_expense_washing_machine') | float +
(states('sensor.pvpc_price') | float *
states('sensor.hourly_energy_usage_washing_machine') | float) }}
mode: single
``````

The automation triggers on the last minute of every hour. Then it multiplies the energy usage of the washing machine in that hour by the price given by the PVPC integration and sums this to the current value of the input number. This way, the input number will accumulate the amount of money spent by the washing machine.

1. If you want to see the amount of money spent monthly you need to create a new utility meter to cycle the value in the input number monthly. Home assistant does not allow to create an utility meter with an input number as the input sensor, so first you need to transform the input number into a sensor. You can do so in the file “configuration.yaml” of your home assistant by typing the following (don’t forget to check your configuration before restarting home assistant):
``````- platform: template
sensors:
money_expense_washing_machine:
friendly_name: "Money expense washing machine"
unit_of_measurement: "€"
value_template: "{{ states('input_number.money_expense_washing_machine') }}"
``````

Now create the utility meter with the new sensor as the input sensor and cycle it monthly (you can cycle however you want but I wanted it monthly).

1. Display it in the lovelace dashboard :

Any questions or improvements will be welcome!

If you wanna practice value for value you can send me some lightning sats on this lightning address : [email protected]

3 Likes

Thank you very much I’ll try it, it’s what I was looking for.

1 Like

Good tutorial.
You might be interested in Energy: Cost for the individual devices which allow home assistant to make all of this for you (it does it for your global consumption but not for individual devices yet)

1 Like

I’ll have a look at that, thanks!