How to generate a dynamic automation

Hi all,
I can’t seem to find a solution for a problem I like to solve using HA. I have an electric water heater
and I like to activate it during a period of 4 (or 3) hours per day when the electricity price is cheapest
BTW it is a dumb heater.

This means that just before midnight I like to read some data from an server on Internet which will provide me with information about when (at what time of the day) and during how long I should activate the heater. This will be different for each day. One day this could be at 04:00 hours in the morning for a period of 4 (or 3) hours and the next day it could be at 07:00 hours for 4 (or 3) hours.

I wrote a simple Python program to collect the data from Internet, another part of the software is determining when the entity should be activated and for how long.

But what I can’t seem to figure out is how to define a new dynamic automation rule everyday, this rule would include an activation time and de-activation time or alternatively an activation time and a duration.

Any suggestion/pointer is welcome

You can trigger an automation from a sensor with device_class: timestamp: Automation Trigger - Home Assistant

Create two sensors from your data for start and end time, and two simple automations for turn-on and turn-off. Come back with code if you need further help.

This might be of interest, too: Octopus Energy Agile Tariff - #22 by badguy