Automation using input numbers, variables and calculations for Solar Battery Charging

Hi All,

I have been an HA user for a little while now but have managed to all but avoid writing Yaml. I want to start getting a bit more adventurous with my automation but I have hit my coding limit already! :frowning:

For background I have a SunSynk Solar installation that is connected to HA via Solar Assistant’s MQTT integration. This allows me to monitor and control the inverter from HA (Yay!)

However, the native methods in Sola Assistant and on the inverter itself for controlling battery charging are clunky at best - so I want to use HA to build my charging rules

I have set up a basic dashboard with some input helpers and template sensors (to use as global variables) as follows:

Screenshot 2024-04-18 at 23.57.35

The idea is to create an automation that uses the start and stop times of the cheap rate electricity and the current and desired states of battery charge to calculate the charging current (Amps) needed to hit the desired charge % in the calculated time window. The automation itself will be triggered by the start time.

As I know every one battery percent equates to 0.2048kW, if I need to get from 20% charge to 70% (10.24kW) charge in three hours i need to charge at 3.413kW which is 66.66 amps at 51.2V. I want to do this so as to charge the batteries as slowly as I can.

The bit I am struggling with is the syntax/filters format for calculating the time window (duration) value by subtracting the start time from the end time and subtracting the desired and current battery charge percentages to give the % charge required to use in the calculation to derive the charge current. I have had a read through the template instructions and about states & filters, but I don’t really understand it fully. :frowning:

Once I have the value calculated - setting it in the automation itself will be easy!

Any guidance would be hugely appreciated

Duration, this will be a timedelta object so you can easily perform datetime calculations without resorting to timestamps:

{{ today_at(states('input_datetime.chaep_rate_stop')) - today_at(states('input_datetime.chaep_rate_start')) }}

Charge required, limited to only positive numbers (e.g. returns 0 if SOC > target):

{{ [0, states('input_number.charge_target')|float - states('sensor.battery_state_of_charget')|float(100)]|max }}

Note that I set the default SOC to be 100 if the sensor is unavailable. This will set your charge required to 0. Which I thought would be wise if the sensor is unknown. Feel free to change that to whatever you think is appropriate by changing this bit |float(100). You do need a default for this or it will generate errors if the sensor ever becomes unavailable. The input helpers will never be unavailable so they don’t need defaults.

Thanks @tom_l! I found another thread where you and @aceindy showed how to convert the duration to a decimal as well. so thanks both!

Hopefully i have everything I need to get the automation working!

It is truly great how helpful people are in this community :star_struck:

1 Like