Smart water heating PV excess energy

Hi all!

I am rather new to automation and can’t seem to figure out how to do the following:
I have a HomeWizard P1 energy meter placed, which monitors power consumption.
Solar panels are installed and I have a variable energy contract with prices changing every hour.
When the solar panels produce more than I consume (see image), the power will be exported to the grid and billed within the hour. At this time, the energy price is very low, because there is a lot of solar energy at those times.

Therefor I would like to have an automated system that toggles a Wifi switch to activate a water heater that heats up a 300 L buffer tank for domestic water (3.000 watt - more than the solar panels should generate). I made a small script myself to get an idea (not working, just a concept):


execute = function(wifi switch ON)

at (whole hour xx:00) check consumption.today (P1 meter)
every minute check consumption.currenttotal;
loop()
if (consumption.today > consumption.currenttotal) switch heater on 
if (consumption.today < consumption.currenttotal) switch heater off
delay(60 seconds)
if (consumption.today > consumption.currenttotal) switch heater on
if (consumption.today < consumption.currenttotal) switch heater off 

What I would like to do is read the powercounter at every whole hour as a starting point, and then use the water heater to consume energy as long as the consumption is lower than the starting value. I need to do this every hour, since the eletric company charges me for every hour of consumption.

Is there anyone who could help me creating a script for this? I think it is not that comlicated, but I am not able to figure it out myself.

Thanks a million!

Can’t you just try this with basic automations? It is a powerful tool that allows exactly what you want and you get excellent debugging tools if it does not work as expected.

What do you mean? Do you have an example?

No clou on what to do with the P1 reading, it is live; i need to store values first and then compare. Could you help me on how to do so?

Add the P1 meter in configuration.

Then all data is stored in the database. You can automate based on the ‘active power’. Start to make a simple automation like ‘turn on a light when power goes below zero’. Extend this until you have what you need.

There are quite a few topics about automations. And you can ask about the HomeWizard integration here: HomeWizard Energy (Wifi P1 meter, kWh meter and Energy Socket)