Seeking Advice on Automating Battery Charging Based on Solar Forecast

Hi everyone, I’m trying to figure out how to automate the charging of my house battery based on the solar forecast for the next day. I have a PV array, and I want to optimize the battery charge level to make the most of the predicted solar yield. I’ve managed to create a function in the template editor that calculates the desired battery percentage based on the forecast, but I’m stuck on how to integrate this into an automation that actually sets the battery charge level.

Here’s what I’ve done so far: I created a template sensor that calculates the target battery percentage using the forecast data. I also set up an automation that triggers at 10 PM each day, which is supposed to look at the forecast and set the battery charge level accordingly. The issue is that while I can specify which entity I want to change in the automation, I can’t figure out how to pass the calculated value from the template sensor to the action. It seems like the automation only accepts a fixed number, not a dynamic value from another entity.

I’ve spent a lot of time searching for solutions, but I haven’t found anything that directly addresses this. Maybe I’m missing something obvious, or perhaps there’s a different approach I should be taking. Should I be using a different type of sensor or maybe a rule instead of an automation? I’m not sure.

If anyone has experience with this or knows of a tutorial or forum post that covers this exact scenario, I would really appreciate the help. I’m excited about the potential to optimize my energy usage, but I’m hitting a wall with the implementation. Thanks in advance for any guidance you can offer!