Strategy for working out charging times

I wrote an automation which charges my battery storage on a scale according to the solar forecast from solcast (obtained in a seperate automation)

alias: >-
  At 6pm look at the forecast for tomorows Sun then adjust the battery max
  charge helper according to built in scale
description: ""
trigger:
  - platform: time
    at: "18:00:00"
condition: []
action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.battery_max_charge
      value: >
        {% set forecast = states('sensor.solcast_pv_forecast_forecast_tomorrow')
        | float %} {% if forecast <= 2 %}
          100
        {% elif forecast <= 3 %}
          90
        {% elif forecast <= 4 %}
          77
        {% elif forecast <= 5 %}
          66
        {% elif forecast <= 6 %}
          55
        {% elif forecast <= 7 %}
          44
        {% elif forecast <= 8 %}
          32
        {% else %}
          10
        {% endif %}
  - service: persistent_notification.create
    data:
      message: Battery Set
mode: single

So once the forecast has been obtained I’ll have a value stored in the battery_max_charge which determines how much charge my batteries will get overnight during the cheap period of electricity.

I use other automations to control the charging routine essentially taking the charge at the beginning of charging i.e 55% up to the battery_max_charge value so every day the amount of charge is different (as is the time it takes to get there).

Now I get cheaper electricity between 2am and 5am so at 2am charging starts. What I want to do is to “time shift” the automation so that the charging starts as close to 5am as possible and gives the batteries enough time to reach the target so that at 5am the target is effectively reached.

I know that recorder keeps data and I did try and create a 2nd influx DB to try and store some data for reuse but along the way I couldn’t get to where I needed to be as I can’t find a stopwatch entity in HA only a countdown timer, a 2nd connection to influx didn’t seem to work.

The outcome I’m looking for is for me to have an entity in HA which shows how long from the current state to the target charge would take and from there I should be able to build into my automation. I did try and do some maths around the inverter \ battery capacity but it got confusing.

I created a helper (number) and an automation to increment the helper every minute which I thought was a good starting point:

alias: "Charge time increment  "
description: ""
trigger:
  - platform: time_pattern
    seconds: "0"
condition:
  - condition: state
    entity_id: select.battery_first_grid_charge
    state: Enabled
    for:
      hours: 0
      minutes: 0
      seconds: 1
action:
  - service: input_number.increment
    data: {}
    target:
      entity_id: input_number.charge_time_minutes
mode: single

Has anyone already done this? or have good ideas on how I can achieve this please.