solar forecast

My House Has a Brain Now: Building a Solar Battery Optimiser From Scratch

solar-forecast

There is something deeply satisfying about a piece of software that runs quietly in the background, makes genuinely useful decisions on your behalf and then sends you a little email at one in the morning to let you know exactly what it has done and why. That is not the kind of thing most people expect from a home automation project. But that is precisely what Solar Forecast does, and building it turned out to be one of the more rewarding little engineering adventures I have had in a while.

The problem it solves is specific but real. I have solar panels on the roof and a battery in the garage, and every night that battery needs to be charged to the right level before the cheap overnight electricity rate kicks in. Too low and the household runs out of stored power before morning. Too high and you have wasted money filling a battery that the sun was going to top up anyway the next day. Getting that balance right manually means checking weather forecasts, doing maths in your head at midnight and generally being more engaged with your electricity supply than any sensible person wants to be.

Solar Forecast takes all of that off my plate. Every night at quarter past one in the morning, a Cloudflare Worker wakes up, fetches the day's solar generation forecast from the Forecast.Solar API, checks the current state of charge from the LuxPower inverter via its web API and then runs a fairly involved algorithm to figure out the right charge target for the night. It pushes that target directly to the inverter, writes the decision to a database and then sends me a beautifully formatted email explaining exactly what it decided and why. By the time I wake up the battery is at whatever level makes sense given how sunny tomorrow is going to be. I have not had to think about it once.

The algorithm itself is where things get interesting. It accounts for the fact that not all of the solar generation goes into the battery. A fair chunk of it gets consumed by the household during the day before any surplus can actually charge anything. It also adjusts for the season, because in winter the sun disappears around half past three in the afternoon and the battery then has to carry the household all the way through to cheap rate at half past midnight, whereas in summer there is still solar coming in at eight in the evening and the evening period to cover is much shorter. Getting those numbers wrong in either direction is expensive, so the algorithm errs on the side of slightly overcharging rather than running short, with a five percent safety buffer on top of whatever the raw calculation suggests.

solar-forecast

The technical stack is perhaps overkill for what is essentially a household gadget, but it is good fun. The web dashboard is a SvelteKit progressive web app running on Cloudflare Pages, so it installs cleanly on a phone home screen and works without any fuss. The database is Cloudflare D1, which is SQLite living at the edge, and it stores every charge decision along with the reasoning behind it, every solar reading captured at half past ten each night before the inverter resets its daily counter, and all the battery state readings that accumulate throughout the day whenever the dashboard is open. The history chart on the main page shows thirty days of solar generation, grid import and export, and charge targets plotted together, which turns out to be a genuinely satisfying way to see how the system has been performing over time.

Email delivery goes through Cloudflare Email Workers, which is a feature I had not used before but which turned out to be remarkably straightforward. No third party service, no API keys to manage, just a raw MIME message constructed in code and handed off to Cloudflare to deliver. The email itself is designed to match the dashboard: rose and violet gradients, a circular gauge showing the charge target percentage, three stat tiles for forecast generation, expected solar gain and current battery level, and a gradient progress bar. It looks properly polished in a way that a one in the morning automated email really has no business looking.

Push notifications go out at the same time as the email, so if you have the app installed on your phone you get a little nudge alongside everything else.

The thing that has surprised me most about running this in production is how much I actually look at it. The dashboard is genuinely interesting to check throughout the day, watching the live solar generation number tick up and down as clouds pass over. It turns out that making something look good and giving it real data to display is most of the battle when it comes to building something you will actually keep using. Solar Forecast is not solving a huge problem. But it is solving one real problem elegantly, and that feels like enough.

← back to projectsnext: Forge