PVPC Optimizer (only for Spain): Turn on a device only at the cheapest hours of the day

:face_with_monocle: About this blueprint

Type of blueprint: AUTOMATION

Minimum Home Assistant version: 2022.5.0

What does it do?

There are some devices at home that doesn’t matter if they run now or within two hours, right? For example, the Domestic Hot Water (DHW) electric heater, the air conditioning, the dishwasher, the washing machine… Furthermore, you might have some batteries, and you want them to discharge only during the most expensive hours.

This blueprint is ideal for all those who want to save money with the PVPC electricity tariff in Spain. It will automatically turn on the device for the specified number of hours and turn it back off for the rest of the day.

:gear: Configuration


Input fields

  • PVPC sensor :asterisk:: REQUIRED sensor created with the PVPC integration that contains the hourly price of the electricity.

  • Target device :asterisk:: REQUIRED device to turn on only when the electricity is cheap. If the device you want to run cannot be passed to the homeassistant.turn_on service, you can create a script and call this script from the blueprint.

  • How long? :asterisk:: REQUIRED. How many hours per day do you want to turn the device on?

  • When to turn on the device? :asterisk:: REQUIRED. Choose if you want to turn on your device during the cheapest or the most expensive periods of the day.

  • Time gap: Optional. How many hours should there be, at minimum, between consecutive runs of the device?

  • Exclude some hours: Optionally select some hours that you want to exclude from the optimization.

:heavy_plus_sign: My other blueprints

:arrow_down: How to get this blueprint?

Click the badge to import this Blueprint (needs Home Assistant Core 2022.5 or higher):

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Or you can also:

  • Copy-paste the content from this link to a new file inside your configuration’s blueprint folder.


  • 2024-05-20: Added the features to select whether to turn on the device during the most expensive or the cheapest hours. Also, added the possibility to exclude some hours from the calculation. Nice feature requests from @ramos45 and @afternet .
  • 2023-12-03: Added the functionality to define a time gap between consecutive runs. Nice feature request from @dani-sh .
  • 2023-06-19: First version.



This is great, but after using it for a while, I have noticed that usually the 2 cheapest hours of the day are correlative, and this is not optimal for some uses (hot water is a clear example)
I was thinking that for these use cases we need to find the lowest average price of the X hours (2 hours for example) with a delay of at least Y hours between the sampled prices.

Unfortunately I don’t see how to code that into a blueprint. Hopefully you will!

Hi @dani-sh , I think it would be a great new feature! I’ll think about how to implement it robustly and I’ll do it! I’ll post it here when I get a solution (suggestions are accepted :wink: )

1 Like

Just as a thinking starter:

After 21h each day, read the attribute next best at, this is the ordered list of hours from cheapest to most expensive.

Pick first and save it
On_hours[0] = Next_best_at[0]

for each Next_best_at :
if abs(first - next) > Y # if there is a difference of more than Y hours (about 5 for a 100L water heater)
return next # finishes the process and returns the hour

This loop can be repeated for as many hours as one needs wrapped in a for loop.

1 Like

:loudspeaker: BLUEPRINT UPDATE: :arrow_double_up: NEW FEATURE :arrow_double_up:

Hi @dani-sh ,

I just updated the blueprint with your feature request. It is more or less what you proposed, but it runs every hour, as the automation is triggered on every change of the PVPC sensor. Then, it runs a loop to pick up hours of the day when the device should be on:

  1. It selects the cheapest hour.
  2. It “blocks” a number of hours before and after the selected hour, so that the device will be off before and after the cheapest hour. This number is defined in the new input field called “Time gap”. Setting it to 0 omits this step.
  3. It starts again, selecting the cheapest hour from the remaining ones.

The loop runs as many times as it is necessary. For example, if I set up that the device has to turn on 5 times per day, it will run the loop 5 times.

I’ve tested the new code and I think it is working fine, but please tell me about any inconvenience.


To get this new feature, you should just redownload the blueprint code!

1 Like

Downloaded and set up again, although I had to delete the previous automation and instance of the blueprint. As easy to set up as the previous one, I’ll keep you posted.


Nice! Maybe the web interface is not ready to update/replace blueprints. I usually do it from the Samba share complement, which makes files accessible as a network shared folder. I access the folder from the IP address of my Home Assistant instance and the user-password configured by the complement. Then, I just look for the file containing the blueprint code and I replace it with the new version. This avoids having to configure the automations again. However, the result is the same.

I hope it works well!

Working fine so far! Great job. I checked the logic and I liked it. I wouldn’t have thought about this approach.

With release 2023.12 blueprints can be re-imported. Nice.

1 Like

It’s increíble!!

Works perfect. It would be possible to avoid certain hours? For example not running in the night between 23h and 8h

Hi @ramos45 ,

I’m glad you like this blueprint :heart_eyes:

This blueprint is an automation, so, to stop running it at nigh, you can set up a another automation that turns this one off between 23h and 8h, and turns if back on during the day.

Hope it helps!

Yes, actually i’m doing that. The problem is that if i set for example 4h in a day, if there are 4 or more cheap hours in the night, it doesn’t turn on in the day

Would it be possible for you to make a plan like this but just the opposite? I explain.
Activate something but just when the light is most expensive, or the most expensive selected hours of the day.

Sería posible que hicieras un plano como este pero justo al contrario? Me explico.
Accionar algo pero justo cuando la luz esté más cara, o las horas escogidas más caras del día.

:loudspeaker: BLUEPRINT UPDATE: :arrow_double_up: NEW FEATURES :arrow_double_up:

Hi @ramos45 and @afternet , and everyone!

Thanks for your comments! They were inspiring for me, and I decided to modify the blueprint to add these two new features:

  • I added a new selector to exclude some hours from the calculation. I’m not sure how it can be useful, but I’m sure that @ramos45 does :wink:

  • Also, there is a new selector to choose whether to turn on the device during the cheapest hours or during the most expensive hours. It’s nice to have batteries and use them when the electricity is expensive, like @afternet! I wish I had some…

To benefit from these updates, you just need to redownload the blueprint.

I’ve tested the new code and I think it is working fine, and it is backwards compatible, but please tell me about any inconvenience.


Mil gracias! No me ha ido al principio, pero he podido solucionarlo al rato de actualizar. Voy a testear bien ambas opciones con precio bajo y alto, y te cuento como ha ido.
Este PLANO es que es justo lo que estaba buscando desde hacía meses.

Thank you! It didn’t work for me at first, but I was able to solve it after updating. I’m going to test both options with low and high prices well, and I’ll tell you how it goes.
This PLAN is just what I was looking for for months.

1 Like

Thank you very much for the new features! In my case, eliminating night hours is useful for using it with devices that make noise. I think more people will find it useful.

Llevo 2 días testeandolo y me enciende y apaga los dispositivos que elijo cuando la luz está más barata o más cara. Funciona perfecto!!!

I’ve been testing it for 2 days and it turns the devices I choose on and off when the light is cheaper or more expensive. It works perfect!!!

Buenas Marc de nuevo

Crees que sería posible que en el mismo plano añadieras la posibilidad de mandar un mensaje de telegram? Del tipo, se ha puesto en marcha el lavavajillas o algo así, editable como es lógico.

Esa posibilidad dentro del plano sería fabuloso. Mil gracias.

Hello Marc again

Do you think it would be possible for you to add the possibility of sending a Telegram message to the same plan? Like, the dishwasher has started or something like that, editable of course.

That possibility within the plane would be fabulous. Thank you.

Hi @afternet ,

For your purpose, you should create an automation that sends a Telegram message when the dishwasher is turned on or when its consumption is above a certain threshold. This is not the aim of this blueprint.

I hope it helps!