In seven years, the island nation of Samoa plans to run on 100% renewable electricity. Over the last year, the local utility has worked with Tesla to install a key piece of that plan–battery storage, and also a software system that can control Samoa’s entire electricity supply.

In the past, like many islands, the country ran mostly on imported, expensive, and polluting diesel power. As recently as 2012, the country brought in 95 million liters of diesel. Spurred by the cost and the threat of climate change–Samoa is at particular risk from sea level rise and new outbreaks of climate-related diseases–the country has been ramping up the use of renewables, with five large solar plants, a wind farm, and hydropower plants. But as renewable energy grew, the grid struggled with reliability.

“It had gotten to the point where just the solar, combined, could provide over half of the entire peak demand for the island, but they were having quite a few challenges managing that efficiently,” says JB Straubel, Tesla’s chief technical officer.

Whenever wind and solar power supply more than 20% of a grid’s peak demand, the unpredictability of those sources can make the grid unstable. In Samoa, in the worst cases, it led to power outages. In the best cases, it meant the island was still relying on diesel power more than it wanted to.

Tesla installed two of its “Powerpack” battery systems, and also developed and implemented island grid controller software that can control both the batteries and all of the power plants. “If a big cloud comes over the island and the solar drops very quickly, we can control the battery to make up the difference so we don’t have to start a generator immediately, and we don’t have to keep a generator running even when it might not be needed,” says Straubel.

It’s something that a human can’t do easily, even if someone was continuously monitoring the grid. “You have to respond in fractions of a second and you have to be kind of watching a lot of different data sources at the same time and then responding quickly,” he says. “So it’s sort of the perfect application for a computer to do exactly that.”

Since the batteries were installed earlier in the summer, the island is no longer having outages, and the electricity supply is steady. The local utility has been able to reduce the use of diesel generators, though it’s now working to tweak the system to use even less diesel. “Stability has been achieved, but we’re still working on least cost of operation,” says Fonoti Perelini S. Perelini, the project manager for the utility. “We’d like now to reduce the use of diesel generators right up to no diesel, and we’d like to move away from using diesel at certain times when there’s a lot of renewable available.” Diesel use will drop both as the utility continues to add new renewable sources and as it works to optimize the grid controller.